11package com .Timo .Timo .global .auth .handler ;
22
3+ import com .Timo .Timo .global .auth .filter .OAuthOriginCaptureFilter ;
34import com .Timo .Timo .global .auth .principal .CustomUserDetails ;
45import com .Timo .Timo .global .auth .service .AuthCodeService ;
56import com .Timo .Timo .global .auth .service .RefreshTokenService ;
89import jakarta .servlet .http .HttpServletRequest ;
910import jakarta .servlet .http .HttpServletResponse ;
1011import java .io .IOException ;
12+ import java .util .List ;
1113import lombok .RequiredArgsConstructor ;
1214import org .springframework .beans .factory .annotation .Value ;
1315import org .springframework .http .HttpHeaders ;
@@ -24,8 +26,11 @@ public class OAuthSuccessHandler extends SimpleUrlAuthenticationSuccessHandler {
2426 private final RefreshTokenService refreshTokenService ;
2527 private final AuthCodeService authCodeService ;
2628
27- @ Value ("${app.oauth2.redirect-uri}" )
28- private String redirectUri ;
29+ @ Value ("${app.oauth2.allowed-frontend-urls}" )
30+ private List <String > allowedFrontendUrls ;
31+
32+ @ Value ("${app.oauth2.callback-path}" )
33+ private String callbackPath ;
2934
3035 @ Value ("${app.auth.cookie-secure}" )
3136 private boolean cookieSecure ;
@@ -37,6 +42,8 @@ public void onAuthenticationSuccess(
3742 Authentication authentication
3843 ) throws IOException {
3944
45+ String frontendOrigin = resolveFrontendOrigin (request );
46+
4047 CustomUserDetails userDetails = (CustomUserDetails ) authentication .getPrincipal ();
4148 Long userId = userDetails .getUserId ();
4249 boolean onboardingCompleted = userDetails .getUser ().isOnboardingCompleted ();
@@ -56,10 +63,23 @@ public void onAuthenticationSuccess(
5663 onboardingCompleted
5764 );
5865
59- String redirectUrl = UriComponentsBuilder .fromUriString (redirectUri )
66+ String redirectUrl = UriComponentsBuilder .fromUriString (frontendOrigin + callbackPath )
6067 .queryParam ("code" , code )
6168 .build ().toUriString ();
6269
6370 getRedirectStrategy ().sendRedirect (request , response , redirectUrl );
6471 }
72+
73+ private String resolveFrontendOrigin (HttpServletRequest request ) {
74+ String origin = null ;
75+ if (request .getSession (false ) != null ) {
76+ origin = (String ) request .getSession ().getAttribute (OAuthOriginCaptureFilter .SESSION_KEY );
77+ request .getSession ().removeAttribute (OAuthOriginCaptureFilter .SESSION_KEY );
78+ }
79+
80+ if (origin != null && allowedFrontendUrls .contains (origin )) {
81+ return origin ;
82+ }
83+ return allowedFrontendUrls .get (0 );
84+ }
6585}
0 commit comments