Control response entity

This commit is contained in:
hou27
2022-06-11 00:32:06 +09:00
parent baa59f24a5
commit b033747ec3
7 changed files with 22 additions and 13 deletions

View File

@@ -5,6 +5,7 @@ import demo.api.user.domain.User;
import demo.api.user.dtos.UserSignInRequest;
import demo.api.user.dtos.UserSignUpRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
@@ -43,7 +44,7 @@ public class AuthController {
}
@PostMapping
public TokenDto signIn(@Validated UserSignInRequest signInReq) {
public ResponseEntity<TokenDto> signIn(@Validated UserSignInRequest signInReq) {
return authService.signIn(signInReq);
}
}

View File

@@ -4,6 +4,7 @@ import demo.api.jwt.dtos.TokenDto;
import demo.api.user.domain.User;
import demo.api.user.dtos.UserSignInRequest;
import demo.api.user.dtos.UserSignUpRequest;
import org.springframework.http.ResponseEntity;
public interface AuthService {
/**
@@ -18,5 +19,5 @@ public interface AuthService {
* @param signInReq 유저의 이메일과 비밀번호
* @return json web token
*/
TokenDto signIn(UserSignInRequest signInReq);
ResponseEntity<TokenDto> signIn(UserSignInRequest signInReq);
}

View File

@@ -1,6 +1,7 @@
package demo.api.auth;
import demo.api.exception.CustomException;
import demo.api.jwt.JwtTokenFilter;
import demo.api.jwt.JwtTokenProvider;
import demo.api.jwt.dtos.TokenDto;
import demo.api.user.domain.User;
@@ -9,7 +10,9 @@ import demo.api.user.dtos.UserSignUpRequest;
import demo.api.user.repository.UserRepository;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
@@ -40,7 +43,7 @@ public class AuthServiceImpl implements AuthService {
}
@Override
public TokenDto signIn(UserSignInRequest signInReq) {
public ResponseEntity<TokenDto> signIn(UserSignInRequest signInReq) {
try {
Authentication authentication = authenticationManager.authenticate(
new UsernamePasswordAuthenticationToken(
@@ -48,8 +51,12 @@ public class AuthServiceImpl implements AuthService {
signInReq.getPassword()
)
);
TokenDto tokenDto = new TokenDto(jwtTokenProvider.generateToken(authentication));
return new TokenDto(jwtTokenProvider.generateToken(authentication));
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Authorization", "Bearer " + tokenDto.getAccess_token());
return new ResponseEntity<>(tokenDto, httpHeaders, HttpStatus.OK);
} catch (AuthenticationException e) {
throw new CustomException("Invalid credentials supplied", HttpStatus.UNPROCESSABLE_ENTITY);
}

View File

@@ -33,7 +33,8 @@ public class SecurityConfig {
@Bean
public AuthenticationManager authenticationManager(
AuthenticationConfiguration authenticationConfiguration) throws Exception {
AuthenticationConfiguration authenticationConfiguration
) throws Exception {
return authenticationConfiguration.getAuthenticationManager();
}
@@ -43,12 +44,9 @@ public class SecurityConfig {
http
.csrf().disable()
.formLogin()
.loginPage("/user/signIn")
.loginProcessingUrl("/user/signInProc")
.usernameParameter("email")
.passwordParameter("password")
.loginPage("/auth/signIn")
.defaultSuccessUrl("/")
.failureUrl("/user/signIn?fail=true");
.failureUrl("/auth/signIn?fail=true");
//
http

View File

@@ -29,6 +29,6 @@ public class JwtAuthenticationEntryPoint implements AuthenticationEntryPoint {
) throws IOException {
System.out.println(request.getRequestURI());
log.error("UnAuthorized -- message : " + e.getMessage()); // 로그를 남기고
response.sendRedirect("/user/signIn"); // 로그인 페이지로 리다이렉트되도록 하였다.
response.sendRedirect("/auth/signIn"); // 로그인 페이지로 리다이렉트되도록 하였다.
}
}

View File

@@ -1,8 +1,10 @@
package demo.api.jwt.dtos;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public class TokenDto {
private String access_token;
}