Skip to content

Commit

Permalink
Merge pull request #28 from 9oormthon-univ/dev
Browse files Browse the repository at this point in the history
[#2]🐛Fix: 토큰 발급 수정
  • Loading branch information
sumin220 authored Nov 22, 2024
2 parents a09b997 + bc1e2a2 commit 0698045
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 0698045

Please sign in to comment.