From bc1e2a2836c04ccfd65b58794aff6453931c1016 Mon Sep 17 00:00:00 2001 From: sonsumin Date: Fri, 22 Nov 2024 16:42:44 +0900 Subject: [PATCH] =?UTF-8?q?[#2]=F0=9F=90=9BFix:=20=ED=86=A0=ED=81=B0=20?= =?UTF-8?q?=EB=B0=9C=EA=B8=89=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../global/oauth/OAuth2SuccessHandler.java | 104 ++++++++---------- 1 file changed, 43 insertions(+), 61 deletions(-) diff --git a/src/main/java/univ/yesummit/global/oauth/OAuth2SuccessHandler.java b/src/main/java/univ/yesummit/global/oauth/OAuth2SuccessHandler.java index 3202a46..fa0f74f 100644 --- a/src/main/java/univ/yesummit/global/oauth/OAuth2SuccessHandler.java +++ b/src/main/java/univ/yesummit/global/oauth/OAuth2SuccessHandler.java @@ -7,6 +7,7 @@ import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.http.ResponseCookie; import org.springframework.security.core.Authentication; import org.springframework.security.web.authentication.AuthenticationSuccessHandler; import org.springframework.stereotype.Component; @@ -43,75 +44,56 @@ public void onAuthenticationSuccess(HttpServletRequest request, HttpServletRespo throw new RuntimeException(e); } - // 토큰을 쿠키에 저장 - int accessTokenMaxAge = jwtUtils.getAccessExpiration().intValue() / 1000; // 밀리초를 초로 변환 - int refreshTokenMaxAge = jwtUtils.getRefreshExpiration().intValue() / 1000; + // 첫 로그인 여부 확인 + boolean firstLogin = memberService.isFirstLogin(memberId); - // Access Token 쿠키 - Cookie accessTokenCookie = new Cookie("accessToken", accessToken); - accessTokenCookie.setHttpOnly(true); // JavaScript 접근 불가 - accessTokenCookie.setSecure(false); // HTTPS가 아닌 경우 false - accessTokenCookie.setDomain("localhost"); // 로컬 환경 도메인 설정 - accessTokenCookie.setPath("/"); // 모든 경로에서 유효 - accessTokenCookie.setMaxAge(accessTokenMaxAge); + // 응답 데이터 생성 + Map tokens = new HashMap<>(); + tokens.put("accessToken", accessToken); + tokens.put("refreshToken", refreshToken); + tokens.put("firstLogin", firstLogin); - // Refresh Token 쿠키 - Cookie refreshTokenCookie = new Cookie("refreshToken", refreshToken); - refreshTokenCookie.setHttpOnly(true); - refreshTokenCookie.setSecure(false); - refreshTokenCookie.setDomain("localhost"); - refreshTokenCookie.setPath("/"); - refreshTokenCookie.setMaxAge(refreshTokenMaxAge); + response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); - - response.addCookie(accessTokenCookie); - response.addCookie(refreshTokenCookie); - - // 첫 로그인 여부에 따라 리다이렉트 - if (memberService.isFirstLogin(memberId)) { - response.sendRedirect("http://localhost:3000/signup"); - } else { - response.sendRedirect("http://localhost:3000/home"); - } + // JSON 응답 생성 + new ObjectMapper().writeValue(response.getWriter(), tokens); } } -// @Override -// public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException { -// log.info("OAuth2SuccessHandler.onAuthenticationSuccess Member Name : {}", authentication.getName()); -// -// OAuth2Member oAuth2Member = (OAuth2Member) authentication.getPrincipal(); -// Long memberId = oAuth2Member.getMemberId(); +// // 토큰을 쿠키에 저장 +// int accessTokenMaxAge = jwtUtils.getAccessExpiration().intValue() / 1000; // 밀리초를 초로 변환 +// int refreshTokenMaxAge = jwtUtils.getRefreshExpiration().intValue() / 1000; + +// // Access Token 쿠키 +// ResponseCookie accessTokenCookie = ResponseCookie.from("accessToken", accessToken) +// .httpOnly(true) +// .secure(true) // HTTPS에서만 동작 +// .sameSite("None") // Cross-Domain 허용 +// .path("/") +// .domain("yesummit.kro.kr") // 도메인 지정 +// .maxAge(accessTokenMaxAge) +// .build(); // -// boolean firstLogin = memberService.isFirstLogin(memberId); -// // JWT 토큰 생성 -// String accessToken = jwtUtils.createAccessToken(memberId); -// String refreshToken = jwtUtils.createRefreshToken(memberId); +// // Refresh Token 쿠키 +// ResponseCookie refreshTokenCookie = ResponseCookie.from("refreshToken", refreshToken) +// .httpOnly(true) +// .secure(true) +// .sameSite("None") +// .path("/") +// .domain("yesummit.kro.kr") +// .maxAge(refreshTokenMaxAge) +// .build(); // -// // Redirect 경로 지정 -// String redirectUrl = firstLogin ? "/additional-info" : "/home"; // -// // Refresh 토큰을 멤버 엔티티에 저장 -// try { -// memberService.updateRefreshToken(memberId, refreshToken); -// } catch (Exception e) { -// throw new RuntimeException(e); +// response.addHeader("Set-Cookie", accessTokenCookie.toString()); +// response.addHeader("Set-Cookie", refreshTokenCookie.toString()); + + // 첫 로그인 여부에 따라 리다이렉트 +// if (memberService.isFirstLogin(memberId)) { +// response.sendRedirect("http://localhost:3000/signup"); +// } else { +// response.sendRedirect("http://localhost:3000/home"); // } -// -// // 응답 데이터 생성 -// Map responseData = new HashMap<>(); -// responseData.put("accessToken", accessToken); -// responseData.put("refreshToken", refreshToken); -// responseData.put("redirectUrl", redirectUrl); -// -// // 응답 설정 -// response.setContentType("application/json"); -// response.setCharacterEncoding("UTF-8"); -// -// // JSON으로 응답 -// ObjectMapper objectMapper = new ObjectMapper(); -// String jsonResponse = objectMapper.writeValueAsString(responseData); -// response.getWriter().write(jsonResponse); // } -//} - +//} \ No newline at end of file