-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOAuth2AuthenticationSuccessHandler.java
More file actions
92 lines (71 loc) · 3.19 KB
/
OAuth2AuthenticationSuccessHandler.java
File metadata and controls
92 lines (71 loc) · 3.19 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package com.example.oauth2.security;
import com.example.oauth2.entity.User;
import com.example.oauth2.util.JwtTokenProvider;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.IOException;
@Slf4j
@Component
@RequiredArgsConstructor
public class OAuth2AuthenticationSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
private final JwtTokenProvider jwtTokenProvider;
@Value("${cors.allowed-origins}")
private String frontendUrl;
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException {
CustomOAuth2User oAuth2User = (CustomOAuth2User) authentication.getPrincipal();
User user = oAuth2User.getUser();
// JWT 토큰 생성
String accessToken = jwtTokenProvider.createAccessToken(user.getEmail(), user.getRole().toString());
String refreshToken = jwtTokenProvider.createRefreshToken(user.getEmail());
// 프론트엔드로 리다이렉트 (토큰을 쿼리 파라미터로 전달)
String targetUrl = UriComponentsBuilder.fromUriString(frontendUrl + "/login-success")
.queryParam("accessToken", accessToken)
.queryParam("refreshToken", refreshToken)
.build().toUriString();
log.info("OAuth2 로그인 성공: {} ({})", user.getName(), user.getEmail());
getRedirectStrategy().sendRedirect(request, response, targetUrl);
}
}
// CustomOAuth2User 클래스
package com.example.oauth2.security;
import com.example.oauth2.entity.User;
import lombok.Getter;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.core.user.OAuth2User;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
@Getter
public class CustomOAuth2User implements OAuth2User {
private final User user;
private final Map<String, Object> attributes;
private CustomOAuth2User(User user, Map<String, Object> attributes) {
this.user = user;
this.attributes = attributes;
}
public static CustomOAuth2User create(User user, Map<String, Object> attributes) {
return new CustomOAuth2User(user, attributes);
}
@Override
public Map<String, Object> getAttributes() {
return attributes;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + user.getRole().name()));
}
@Override
public String getName() {
return user.getName();
}
}