Skip to content

Commit cd7beba

Browse files
authored
[merge] 배포, 로컬 url 분리 배포환경에 반영
[deploy] 배포, 로컬 url 분리 배포환경에 반영
2 parents 1480e86 + f0575ab commit cd7beba

6 files changed

Lines changed: 70 additions & 8 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.Timo.Timo.global.auth.filter;
2+
3+
import java.io.IOException;
4+
5+
import org.springframework.stereotype.Component;
6+
import org.springframework.web.filter.OncePerRequestFilter;
7+
8+
import jakarta.servlet.FilterChain;
9+
import jakarta.servlet.ServletException;
10+
import jakarta.servlet.http.HttpServletRequest;
11+
import jakarta.servlet.http.HttpServletResponse;
12+
13+
@Component
14+
public class OAuthOriginCaptureFilter extends OncePerRequestFilter {
15+
16+
public static final String SESSION_KEY = "OAUTH_FRONTEND_ORIGIN";
17+
private static final String AUTHORIZATION_PATH_PREFIX = "/oauth2/authorization/";
18+
private static final String ORIGIN_PARAM = "redirect_origin";
19+
20+
@Override
21+
protected void doFilterInternal(
22+
HttpServletRequest request,
23+
HttpServletResponse response,
24+
FilterChain filterChain
25+
) throws ServletException, IOException {
26+
if (request.getRequestURI().startsWith(AUTHORIZATION_PATH_PREFIX)) {
27+
String origin = request.getParameter(ORIGIN_PARAM);
28+
if (origin != null && !origin.isBlank()) {
29+
request.getSession().setAttribute(SESSION_KEY, origin);
30+
}
31+
}
32+
filterChain.doFilter(request, response);
33+
}
34+
}

‎src/main/java/com/Timo/Timo/global/auth/handler/OAuthSuccessHandler.java‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.Timo.Timo.global.auth.handler;
22

3+
import com.Timo.Timo.global.auth.filter.OAuthOriginCaptureFilter;
34
import com.Timo.Timo.global.auth.principal.CustomUserDetails;
45
import com.Timo.Timo.global.auth.service.AuthCodeService;
56
import com.Timo.Timo.global.auth.service.RefreshTokenService;
@@ -8,6 +9,7 @@
89
import jakarta.servlet.http.HttpServletRequest;
910
import jakarta.servlet.http.HttpServletResponse;
1011
import java.io.IOException;
12+
import java.util.List;
1113
import lombok.RequiredArgsConstructor;
1214
import org.springframework.beans.factory.annotation.Value;
1315
import 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
}

‎src/main/java/com/Timo/Timo/global/config/SecurityConfig.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.Timo.Timo.global.auth.handler.JwtAuthenticationEntryPoint;
44
import com.Timo.Timo.global.auth.handler.OAuthFailureHandler;
5+
import com.Timo.Timo.global.auth.filter.OAuthOriginCaptureFilter;
56
import com.Timo.Timo.global.auth.handler.OAuthSuccessHandler;
67
import com.Timo.Timo.global.auth.service.CustomOAuth2UserService;
78
import com.Timo.Timo.global.jwt.filter.JwtAuthenticationFilter;
@@ -14,6 +15,7 @@
1415
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
1516
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer;
1617
import org.springframework.security.config.http.SessionCreationPolicy;
18+
import org.springframework.security.oauth2.client.web.OAuth2AuthorizationRequestRedirectFilter;
1719
import org.springframework.security.web.SecurityFilterChain;
1820
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
1921
import org.springframework.web.cors.CorsConfigurationSource;
@@ -29,6 +31,7 @@ public class SecurityConfig {
2931
private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
3032
private final JwtAuthenticationFilter jwtAuthenticationFilter;
3133
private final CorsConfigurationSource corsConfigurationSource;
34+
private final OAuthOriginCaptureFilter oAuthOriginCaptureFilter;
3235

3336
@Bean
3437
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
@@ -64,7 +67,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
6467
exception.authenticationEntryPoint(jwtAuthenticationEntryPoint)
6568
)
6669
.addFilterBefore(jwtAuthenticationFilter, UsernamePasswordAuthenticationFilter.class)
67-
.addFilterBefore(mdcLoggingFilter(), JwtAuthenticationFilter.class);
70+
.addFilterBefore(mdcLoggingFilter(), JwtAuthenticationFilter.class)
71+
.addFilterBefore(oAuthOriginCaptureFilter, OAuth2AuthorizationRequestRedirectFilter.class);
6872

6973
return http.build();
7074
}
@@ -82,4 +86,4 @@ public FilterRegistrationBean<MdcLoggingFilter> mdcLoggingFilterRegistration(
8286
registrationBean.setEnabled(false);
8387
return registrationBean;
8488
}
85-
}
89+
}

‎src/main/resources/application-local.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ security:
3333

3434
app:
3535
oauth2:
36-
redirect-uri: http://localhost:5173/oauth/callback
36+
allowed-frontend-urls: ${FRONTEND_URLS}
3737
calendar:
3838
redirect-uri: ${FRONTEND_URL}/oauth/calendar/callback
3939
auth:

‎src/main/resources/application-prod.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ security:
4242

4343
app:
4444
oauth2:
45-
redirect-uri: ${FRONTEND_URL}/oauth/callback
45+
allowed-frontend-urls: ${FRONTEND_URLS}
4646
calendar:
4747
redirect-uri: ${FRONTEND_URL}/oauth/calendar/callback
4848
auth:
@@ -72,4 +72,4 @@ sentry:
7272
dsn: ${SENTRY_DSN}
7373
environment: ${SENTRY_ENVIRONMENT:prod}
7474
traces-sample-rate: ${SENTRY_TRACES_SAMPLE_RATE:0.0}
75-
release: ${SENTRY_RELEASE:unknown}
75+
release: ${SENTRY_RELEASE:unknown}

‎src/main/resources/application.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ springdoc:
2828
default-produces-media-type: application/json
2929
default-consumes-media-type: application/json
3030

31+
app:
32+
oauth2:
33+
callback-path: /oauth/callback
34+
3135
management:
3236
endpoints:
3337
web:

0 commit comments

Comments
 (0)