Skip to content

Commit

Permalink
fix. success handler custom
Browse files Browse the repository at this point in the history
  • Loading branch information
soyesenna committed Jun 5, 2024
1 parent 81ccecc commit aa31744
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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;

@Component
public class CustomSuccessHandler extends SavedRequestAwareAuthenticationSuccessHandler {

private final RequestCache requestCache = new HttpSessionRequestCache();

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

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

String targetUrl = savedRequest.getRedirectUrl();
targetUrl = "/api" + targetUrl;

getRedirectStrategy().sendRedirect(request, response, targetUrl);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
public class SecurityConfig {

private final CustomOAuthUserService oAuth2UserService;
private final CustomSuccessHandler successHandler;

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

return http.build();
}

@Bean
public AuthenticationSuccessHandler savedRequestAwareAuthenticationSuccessHandler() {
return new SavedRequestAwareAuthenticationSuccessHandler();
}

// @Bean
// protected SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
Expand Down

0 comments on commit aa31744

Please sign in to comment.