Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#2]๐Ÿ›Fix: ํ† ํฐ ๋ฐœ๊ธ‰ ์ˆ˜์ • #28

Merged
merged 1 commit into from
Nov 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
// }
//}

//}
Loading