-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCustomOAuth2SuccessHandler.java
More file actions
78 lines (58 loc) · 3.06 KB
/
CustomOAuth2SuccessHandler.java
File metadata and controls
78 lines (58 loc) · 3.06 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
package doldol_server.doldol.auth.handler;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.core.Authentication;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;
import doldol_server.doldol.auth.dto.CustomUserDetails;
import doldol_server.doldol.auth.jwt.TokenProvider;
import doldol_server.doldol.auth.jwt.dto.UserTokenResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
@Component
@RequiredArgsConstructor
public class CustomOAuth2SuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
private final TokenProvider tokenProvider;
@Value("${oauth2.redirect-url.sign-up}")
private String signUpRedirectUrl;
@Value("${oauth2.redirect-url.login-success}")
private String loginSuccessRedirectUrl;
@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response,
Authentication authentication) throws IOException {
OAuth2AuthenticationToken oAuth2Token = (OAuth2AuthenticationToken)authentication;
OAuth2User oAuth2User = oAuth2Token.getPrincipal();
String registrationId = oAuth2Token.getAuthorizedClientRegistrationId();
CustomUserDetails userDetails = (CustomUserDetails)oAuth2User;
if (userDetails.getUserId() == null) {
handleNewSocialUser(response, userDetails.getSocialId(), registrationId);
} else {
handleExistingUser(response, userDetails, registrationId);
}
}
private void handleNewSocialUser(HttpServletResponse response, String socialId, String registrationId) throws
IOException {
String urlEncodedSocialId = URLEncoder.encode(socialId, StandardCharsets.UTF_8);
String urlEncodedRegistrationId = URLEncoder.encode(registrationId.toUpperCase(), StandardCharsets.UTF_8);
String redirectUrl =
signUpRedirectUrl + "?socialId=" + urlEncodedSocialId + "&socialType=" + urlEncodedRegistrationId;
response.sendRedirect(redirectUrl);
}
private void handleExistingUser(HttpServletResponse response, CustomUserDetails userDetails,
String registrationId) throws IOException {
String userid = String.valueOf(userDetails.getUserId());
UserTokenResponse loginToken = tokenProvider.createLoginToken(userid, userDetails.getRole());
String redirectUrl = loginSuccessRedirectUrl +
"?accessToken=" + URLEncoder.encode(loginToken.accessToken(), StandardCharsets.UTF_8) +
"&refreshToken=" + URLEncoder.encode(loginToken.refreshToken(), StandardCharsets.UTF_8) +
"&userId=" + userDetails.getUserId() +
"&role=" + URLEncoder.encode(userDetails.getRole().toString(), StandardCharsets.UTF_8) +
"&socialType=" + URLEncoder.encode(registrationId.toUpperCase(), StandardCharsets.UTF_8);
response.sendRedirect(redirectUrl);
}
}