From 584788add3152e7dd15070a70f289f2f7e563532 Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 2 May 2024 16:01:09 +0900 Subject: [PATCH] =?UTF-8?q?:bug:=20FIX.=20login=20=EC=9D=91=EB=8B=B5=20?= =?UTF-8?q?=EC=98=A4=EB=A5=98=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../security/jwt/handler/JwtLoginFailureHandler.java | 11 +++++++++-- .../security/jwt/handler/JwtLoginSuccessHandler.java | 8 +++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/main/java/fairytale/tbd/global/security/jwt/handler/JwtLoginFailureHandler.java b/src/main/java/fairytale/tbd/global/security/jwt/handler/JwtLoginFailureHandler.java index 3c2776d..b2b6c0f 100644 --- a/src/main/java/fairytale/tbd/global/security/jwt/handler/JwtLoginFailureHandler.java +++ b/src/main/java/fairytale/tbd/global/security/jwt/handler/JwtLoginFailureHandler.java @@ -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()); } } diff --git a/src/main/java/fairytale/tbd/global/security/jwt/handler/JwtLoginSuccessHandler.java b/src/main/java/fairytale/tbd/global/security/jwt/handler/JwtLoginSuccessHandler.java index d17f2a4..69b957d 100644 --- a/src/main/java/fairytale/tbd/global/security/jwt/handler/JwtLoginSuccessHandler.java +++ b/src/main/java/fairytale/tbd/global/security/jwt/handler/JwtLoginSuccessHandler.java @@ -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; @@ -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을 생성한 후 반환 @@ -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); }