Skip to content

Commit

Permalink
Merge pull request #14 from Fairy-Taless/fix/#12-cors
Browse files Browse the repository at this point in the history
🐛 FIX. login 응답 오류 해결
  • Loading branch information
junhaa authored May 2, 2024
2 parents 3f9a7d7 + 584788a commit 60ae2b0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,30 @@
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

import fairytale.tbd.global.enums.statuscode.ErrorStatus;
import fairytale.tbd.global.response.ApiResponse;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;

@Component
@RequiredArgsConstructor
/**
* JWT 로그인 필터 Failure Handler
*/
public class JwtLoginFailureHandler implements AuthenticationFailureHandler {
private static final Logger LOGGER = LogManager.getLogger(JwtLoginSuccessHandler.class);

private final ObjectMapper objectMapper;
@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
AuthenticationException exception) throws
IOException, ServletException {
response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
response.getWriter().write("AUTHENTICATION FAILED.");
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(objectMapper.writeValueAsString(ApiResponse.onFailure(ErrorStatus._BAD_REQUEST.getCode(),ErrorStatus._BAD_REQUEST.getMessage(), "로그인에 실패했습니다.")));
LOGGER.info("Jwt Login fail :: error = {}", exception.getMessage());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;

import fairytale.tbd.global.enums.statuscode.SuccessStatus;
import fairytale.tbd.global.response.ApiResponse;
import fairytale.tbd.global.security.jwt.JwtService;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
Expand All @@ -23,6 +27,7 @@
public class JwtLoginSuccessHandler implements AuthenticationSuccessHandler {
private final JwtService jwtService;
private static final Logger LOGGER = LogManager.getLogger(JwtLoginSuccessHandler.class);
private final ObjectMapper objectMapper;

/**
* 인증에 성공하면 Access Token과 Refresh Token을 생성한 후 반환
Expand All @@ -33,7 +38,8 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo
UserDetails principal = (UserDetails)authentication.getPrincipal();
String loginId = principal.getUsername();
response.setStatus(HttpServletResponse.SC_OK);
response.getWriter().write("AUTHENTICATION SUCCESS.");
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(objectMapper.writeValueAsString(ApiResponse.onSuccess("로그인에 성공했습니다.")));
LOGGER.info("Jwt Login Success :: Login ID = {}", loginId);
loginSuccess(response, loginId);
}
Expand Down

0 comments on commit 60ae2b0

Please sign in to comment.