From d595741bce69b273caadb3f1a8a6ca9779ba2296 Mon Sep 17 00:00:00 2001 From: taejin1221 Date: Tue, 10 Sep 2024 14:13:03 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EC=B9=B4=EC=B9=B4=EC=98=A4=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EC=9D=B4=ED=9B=84=20=EC=BF=A0=ED=82=A4=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=EC=A3=BC=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/response/LoginSucessResponse.java | 14 ++++++++++++++ .../domain/handler/LoginSuccessHandler.java | 19 ++++++++++++++----- 2 files changed, 28 insertions(+), 5 deletions(-) create mode 100644 src/main/java/kaboo/kaboo_auth/domain/dto/response/LoginSucessResponse.java diff --git a/src/main/java/kaboo/kaboo_auth/domain/dto/response/LoginSucessResponse.java b/src/main/java/kaboo/kaboo_auth/domain/dto/response/LoginSucessResponse.java new file mode 100644 index 0000000..d8b096b --- /dev/null +++ b/src/main/java/kaboo/kaboo_auth/domain/dto/response/LoginSucessResponse.java @@ -0,0 +1,14 @@ +package kaboo.kaboo_auth.domain.dto.response; + +import java.io.Serializable; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class LoginSucessResponse implements Serializable { + private String username; + private String accessToken; + private String refreshToken; +} diff --git a/src/main/java/kaboo/kaboo_auth/domain/handler/LoginSuccessHandler.java b/src/main/java/kaboo/kaboo_auth/domain/handler/LoginSuccessHandler.java index 46f798b..995c8c9 100644 --- a/src/main/java/kaboo/kaboo_auth/domain/handler/LoginSuccessHandler.java +++ b/src/main/java/kaboo/kaboo_auth/domain/handler/LoginSuccessHandler.java @@ -7,9 +7,12 @@ import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler; import org.springframework.stereotype.Component; +import com.fasterxml.jackson.databind.ObjectMapper; + import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; +import kaboo.kaboo_auth.domain.dto.response.LoginSucessResponse; import kaboo.kaboo_auth.domain.jwt.JwtTokenProvider; import kaboo.kaboo_auth.domain.jwt.entity.JwtAccessToken; import kaboo.kaboo_auth.domain.jwt.entity.JwtRefreshToken; @@ -28,7 +31,7 @@ public class LoginSuccessHandler extends SimpleUrlAuthenticationSuccessHandler { private final int refreshTokenValidTime = 10 * 24 * 60 * 60; // 유효기간 : 10일 @Value("${AUTH.REDIRECT_URL}") - String redirectURL; + private String redirectURL; private Cookie createCookie(String key, String value, int maxAge) { Cookie cookie = new Cookie(key, value); @@ -50,9 +53,15 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo jwtAccessTokenRepository.save(new JwtAccessToken(username, accessToken)); jwtRefreshTokenRepository.save(new JwtRefreshToken(username, refreshToken)); - response.addCookie(createCookie("Username", username, refreshTokenValidTime)); - response.addCookie(createCookie("Authorization", accessToken, accessTokenValidTime)); - response.addCookie(createCookie("RefreshToken", refreshToken, refreshTokenValidTime)); - response.sendRedirect(redirectURL); + LoginSucessResponse loginSucessResponse = new LoginSucessResponse(username, accessToken, refreshToken); + ObjectMapper objectMapper = new ObjectMapper(); + String jsonResponse = objectMapper.writeValueAsString(loginSucessResponse); + + // 응답 설정 + response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); + + // JSON 응답을 출력 스트림에 작성 + response.getWriter().write(jsonResponse); } }