Skip to content

Commit

Permalink
Fix. 로그인 성공시 리다이렉션
Browse files Browse the repository at this point in the history
fix. URI 사용하여 path 추출
fix. target url 경로 추출
fix. success handler custom

fix. savedRequestAwareAuthenticationSuccessHandler사용

fix. redirect URI 경로 재수정

fix. security redirect uri 경로 수정
  • Loading branch information
soyesenna authored and Miensoap committed Jun 5, 2024
1 parent 9500214 commit 807e649
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 27 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package team07.airbnb.config.security;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import org.springframework.stereotype.Component;


import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

@Component
public class CustomSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

private final RequestCache requestCache = new HttpSessionRequestCache();

public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException, IOException {
SavedRequest savedRequest = requestCache.getRequest(request, response);

if (savedRequest == null) {
super.onAuthenticationSuccess(request, response, authentication);
return;
}

String targetUrl = savedRequest.getRedirectUrl();

try {
URI targetUri = new URI(targetUrl);
String path = targetUri.getPath();
String query = targetUri.getQuery();
String newTargetUrl = request.getContextPath() + "/api" + path;
if (query != null) {
newTargetUrl += "?" + query;
}

getRedirectStrategy().sendRedirect(request, response, newTargetUrl);
} catch (URISyntaxException e) {
super.onAuthenticationSuccess(request, response, authentication);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.Customizer;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.oauth2.client.userinfo.OAuth2UserService;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler;
import team07.airbnb.domain.user.enums.Role;
import team07.airbnb.domain.user.service.CustomOAuthUserService;

@Configuration
@RequiredArgsConstructor
@EnableWebSecurity
public class SecurityConfig {

private final CustomOAuthUserService oAuth2UserService;
private final CustomAuthenticationSuccessHandler authenticationSuccessHandler;
private final CustomSuccessHandler successHandler;

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
Expand Down Expand Up @@ -43,13 +47,14 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.oauth2Login(
oAuth -> {
oAuth.userInfoEndpoint(userInfo -> userInfo.userService(oAuth2UserService));
oAuth.successHandler(authenticationSuccessHandler);
oAuth.successHandler(successHandler);
}
);

return http.build();
}


// @Bean
// protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// http.authorizeHttpRequests(auth -> auth
Expand Down

0 comments on commit 807e649

Please sign in to comment.