-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCustomSuccessHandler.java
More file actions
65 lines (49 loc) · 2.41 KB
/
CustomSuccessHandler.java
File metadata and controls
65 lines (49 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package org.example.studylog.oauth2;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.example.studylog.dto.oauth.CustomOAuth2User;
import org.example.studylog.jwt.JWTUtil;
import org.example.studylog.service.TokenService;
import org.example.studylog.util.CookieUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseCookie;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
@Component
@Slf4j
public class CustomSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
@Value("${spring.redirectUri}")
private String redirectUri;
private final JWTUtil jwtUtil;
private final TokenService tokenService;
public CustomSuccessHandler(JWTUtil jwtUtil, TokenService tokenService) {
this.jwtUtil = jwtUtil;
this.tokenService = tokenService;
}
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
//OAuth2User
CustomOAuth2User customUserDetails = (CustomOAuth2User) authentication.getPrincipal();
String oauthId = customUserDetails.getName();
Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
Iterator<? extends GrantedAuthority> iterator = authorities.iterator();
GrantedAuthority auth = iterator.next();
String role = auth.getAuthority();
// 토큰 생성
String refresh = jwtUtil.createJwt("refresh", oauthId, role, 86400000L);
// refresh 토큰 저장
tokenService.addRefreshEntity(oauthId, refresh, 86400000L);
// ResponseCookie 생성하여 응답 헤더에 추가
ResponseCookie cookie = CookieUtil.createCookie("refresh", refresh);
response.addHeader("Set-Cookie", cookie.toString());
// 회원가입 화면으로 리다이렉션(임시: 프론트 로그인 완료 화면으로 변경 예정)
response.sendRedirect(redirectUri);
}
}