22
33import java .io .IOException ;
44import java .util .Collection ;
5+ import java .util .Date ;
56import java .util .Iterator ;
67import java .util .Optional ;
78
1314import org .springframework .stereotype .Component ;
1415
1516import com .example .log4u .common .oauth2 .dto .CustomOAuth2User ;
17+ import com .example .log4u .common .oauth2 .entity .RefreshToken ;
1618import com .example .log4u .common .oauth2 .jwt .JwtUtil ;
19+ import com .example .log4u .common .oauth2 .repository .RefreshTokenRepository ;
1720import com .example .log4u .domain .user .entity .User ;
1821import com .example .log4u .domain .user .repository .UserRepository ;
1922
2730public class OAuth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
2831
2932 private final UserRepository userRepository ;
33+ private final RefreshTokenRepository refreshTokenRepository ;
3034 private final JwtUtil jwtUtil ;
3135
32-
3336 private static final String MAIN_PAGE = "http://localhost:3000/" ;
3437 private static final String PROFILE_CREATE_PAGE = "http://localhost:3000/profile" ;
3538 private static final String LOGIN_PAGE = "http://localhost:3000/login" ;
@@ -53,34 +56,41 @@ public void onAuthenticationSuccess(
5356 CustomOAuth2User customOAuth2User = (CustomOAuth2User )authentication .getPrincipal ();
5457 Optional <User > existUser = userRepository .findByProviderId (customOAuth2User .getProviderId ());
5558 Long userId = existUser .map (User ::getUserId ).orElse (null );
59+ String name = customOAuth2User .getName ();
5660
57- String redirectUrl = switch (customOAuth2User .getRole ()){
61+ String redirectUrl = switch (customOAuth2User .getRole ()) {
5862 case "ROLE_GUEST" -> PROFILE_CREATE_PAGE ;
5963 case "ROLE_USER" -> MAIN_PAGE ;
6064 default -> LOGIN_PAGE ;
6165 };
6266
63- redirectTo (response , userId , authentication , redirectUrl );
67+ setCookieAndSaveRefreshToken (response , userId , authentication , name );
68+ redirectTo (response , redirectUrl );
6469 }
6570
66- private void redirectTo (
71+ private void setCookieAndSaveRefreshToken (
6772 HttpServletResponse response ,
6873 Long userId ,
6974 Authentication authentication ,
70- String redirectUrl
71- ) throws IOException {
72-
75+ String name
76+ ) {
7377 Collection <? extends GrantedAuthority > authorities = authentication .getAuthorities ();
7478 Iterator <? extends GrantedAuthority > iterator = authorities .iterator ();
7579 GrantedAuthority auth = iterator .next ();
7680 String role = auth .getAuthority ();
7781
82+ // 쿠키 생성
7883 String access = jwtUtil .createJwt (ACCESS_TOKEN_KEY , userId , role , accessTokenValidityInSeconds );
79- String refresh = jwtUtil .createJwt (REFRESH_TOKEN_KEY , userId , role , refreshTokenValidityInSeconds );
84+ String refresh = jwtUtil .createJwt (REFRESH_TOKEN_KEY , userId , role , refreshTokenValidityInSeconds );
85+ // 저장
86+ saveRefreshToken (refresh , name );
8087
8188 response .addCookie (createCookie (ACCESS_TOKEN_KEY , access ));
8289 response .addCookie (createCookie (REFRESH_TOKEN_KEY , refresh ));
8390 response .setStatus (HttpStatus .OK .value ());
91+ }
92+
93+ public void redirectTo (HttpServletResponse response , String redirectUrl ) throws IOException {
8494 response .sendRedirect (redirectUrl );
8595 }
8696
@@ -93,4 +103,15 @@ private Cookie createCookie(String key, String value) {
93103 return cookie ;
94104 }
95105
106+ public void saveRefreshToken (String refresh , String name ) {
107+ Date date = new Date (System .currentTimeMillis () + refreshTokenValidityInSeconds );
108+
109+ RefreshToken refreshToken = new RefreshToken (
110+ null ,
111+ name ,
112+ refresh ,
113+ date .toString ()
114+ );
115+ refreshTokenRepository .save (refreshToken );
116+ }
96117}
0 commit comments