Skip to content

Commit 2c228d8

Browse files
committed
feat(oauth): authentication to CustomOAuth2User 로직 개선, 쿠키 값 수정
- 쿠키에는 띄어쓰기가 포함될 수 없는 문제 해결.
1 parent fc9a7f6 commit 2c228d8

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/main/java/com/somemore/global/auth/oauth/handler/CustomOAuthSuccessHandler.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.somemore.global.auth.jwt.domain.EncodedToken;
55
import com.somemore.global.auth.jwt.domain.TokenType;
66
import com.somemore.global.auth.jwt.usecase.GenerateTokensOnLoginUseCase;
7+
import com.somemore.global.auth.oauth.domain.CustomOAuth2User;
78
import com.somemore.global.auth.oauth.processor.OAuthUserProcessor;
89
import com.somemore.global.auth.redirect.RedirectUseCase;
910
import com.somemore.user.domain.UserRole;
@@ -35,7 +36,7 @@ public class CustomOAuthSuccessHandler extends SimpleUrlAuthenticationSuccessHan
3536
public void onAuthenticationSuccess(HttpServletRequest request,
3637
HttpServletResponse response,
3738
Authentication authentication) {
38-
OAuth2User oauthUser = extractOAuthUser(authentication);
39+
CustomOAuth2User oauthUser = extractOAuthUser(authentication);
3940
UUID userId = oauthUserProcessor.fetchUserIdByOAuthUser(oauthUser);
4041

4142
processAccessToken(response, userId);
@@ -55,14 +56,28 @@ private void processAccessToken(HttpServletResponse response, UUID userId) {
5556
generateTokensOnLoginUseCase.generateLoginToken(
5657
userId, UserRole.getOAuthUserDefaultRole());
5758

58-
cookieUseCase.setToken(response, loginToken.getValueWithPrefix(), TokenType.SIGN_IN);
59+
cookieUseCase.setToken(response, loginToken.value(), TokenType.SIGN_IN);
5960
}
6061

61-
private OAuth2User extractOAuthUser(Authentication authentication) {
62+
private CustomOAuth2User extractOAuthUser(Authentication authentication) {
63+
OAuth2AuthenticationToken oAuth2AuthenticationToken = castToOAuth2AuthenticationTokenBy(authentication);
64+
OAuth2User oAuth2User = oAuth2AuthenticationToken.getPrincipal();
65+
return castToCustomOAuth2UserBy(oAuth2User);
66+
}
67+
68+
private OAuth2AuthenticationToken castToOAuth2AuthenticationTokenBy(Authentication authentication) {
6269
if (authentication instanceof OAuth2AuthenticationToken token) {
63-
return token.getPrincipal();
70+
return token;
6471
}
6572
log.error("Authentication 객체가 OAuth2AuthenticationToken 타입이 아닙니다: {}", authentication.getClass().getName());
66-
throw new IllegalArgumentException("잘못된 인증 객체입니다.");
73+
throw new IllegalArgumentException();
74+
}
75+
76+
private CustomOAuth2User castToCustomOAuth2UserBy(OAuth2User oAuth2User) {
77+
if (oAuth2User instanceof CustomOAuth2User customOAuth2User) {
78+
return customOAuth2User;
79+
}
80+
log.error("OAuth2User 객체가 CustomOAuth2User 타입이 아닙니다: {}", oAuth2User.getClass().getName());
81+
throw new IllegalArgumentException();
6782
}
6883
}

0 commit comments

Comments
 (0)