Skip to content

Commit

Permalink
[#2]๐Ÿ›Fix: ํ† ํฐ ๋ฐœ๊ธ‰ ์ˆ˜์ •
Browse files Browse the repository at this point in the history
  • Loading branch information
sumin220 committed Nov 22, 2024
1 parent 9211498 commit bc1e2a2
Showing 1 changed file with 43 additions and 61 deletions.
104 changes: 43 additions & 61 deletions src/main/java/univ/yesummit/global/oauth/OAuth2SuccessHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, Object> 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<String, Object> 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);
// }
//}

//}

0 comments on commit bc1e2a2

Please sign in to comment.