From 4e0ac4025394e11cd2773bc5ab26598083cc232a Mon Sep 17 00:00:00 2001 From: kwongwangjae <121513336+kwongwangjae@users.noreply.github.com> Date: Fri, 19 Apr 2024 17:18:17 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=ED=98=95=EC=8B=9D=20json=20->=20cookie=20(#70)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit json 전달형식에서 cookie 형식으로 변경 --- build.gradle | 4 ++++ .../handler/CommonLoginSuccessHandler.java | 24 ++++++++++++------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/build.gradle b/build.gradle index 47d2a19..be06ecc 100644 --- a/build.gradle +++ b/build.gradle @@ -90,10 +90,14 @@ dependencies { runtimeOnly "io.jsonwebtoken:jjwt-jackson:0.12.3" implementation 'com.google.code.gson:gson:2.10.1' + implementation 'jakarta.servlet:jakarta.servlet-api:6.0.0' + implementation 'org.springframework.boot:spring-boot-starter-data-redis:3.2.2' + implementation 'org.springframework.boot:spring-boot-starter-cache:3.2.3' implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.2.0' + } tasks.named('test') { diff --git a/src/main/java/com/example/dreamvalutbackend/config/handler/CommonLoginSuccessHandler.java b/src/main/java/com/example/dreamvalutbackend/config/handler/CommonLoginSuccessHandler.java index 5720ab3..6bc2ddd 100644 --- a/src/main/java/com/example/dreamvalutbackend/config/handler/CommonLoginSuccessHandler.java +++ b/src/main/java/com/example/dreamvalutbackend/config/handler/CommonLoginSuccessHandler.java @@ -7,6 +7,7 @@ import com.example.dreamvalutbackend.redis.repository.TokenRepository; import com.google.gson.Gson; import jakarta.servlet.ServletException; +import jakarta.servlet.http.Cookie; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; @@ -34,24 +35,29 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo Map responseMap = principal.getUserInfo(); + String accessToken = JwtUtils.generateToken(responseMap, JwtConstants.ACCESS_EXP_TIME); String refreshToken = JwtUtils.generateToken(responseMap, JwtConstants.REFRESH_EXP_TIME); - responseMap.put("accessToken", JwtUtils.generateToken(responseMap, JwtConstants.ACCESS_EXP_TIME)); - responseMap.put("refreshToken", refreshToken); - Long userId = principal.getUserId(); Token token = new Token(refreshToken, userId); tokenRepository.save(token); - Gson gson = new Gson(); - String json = gson.toJson(responseMap); + response.addCookie(createCookie("accessToken", accessToken, JwtConstants.ACCESS_EXP_TIME, true, false)); + + response.addCookie(createCookie("refreshToken", refreshToken, JwtConstants.REFRESH_EXP_TIME, true, false)); - response.setContentType("application/json; charset=UTF-8"); + String clientUrl = "http://localhost:3000/genre"; + response.sendRedirect(clientUrl); + } - PrintWriter writer = response.getWriter(); - writer.println(json); - writer.flush(); + private Cookie createCookie(String name, String value, int maxAge, boolean httpOnly, boolean secure) { + Cookie cookie = new Cookie(name, value); + cookie.setMaxAge(maxAge); + cookie.setHttpOnly(httpOnly); + // cookie.setSecure(secure); + cookie.setPath("/"); + return cookie; } }