Skip to content

Commit

Permalink
refactor: 요청실패시 /login?error 로 리다이렉팅 되는 에러 수정
Browse files Browse the repository at this point in the history
기본 FailureHandler가 위 url로 리다이렉팅 되도록 구현되어 있으므로 직접 구현해서 200응답을 반환하도록 변경하였다.
  • Loading branch information
yooooonshine committed Nov 25, 2024
1 parent 374b4a8 commit 0e0a2fd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;

import aimo.backend.common.properties.SecurityProperties;
import aimo.backend.common.security.oAuth.OAuth2LoginFailureHandler;
import aimo.backend.domains.member.service.MemberService;
import aimo.backend.common.security.filter.exceptionHandlingFilter.ExceptionHandlingFilter;
import aimo.backend.common.security.filter.jwtFilter.JwtAuthenticationFilter;
Expand All @@ -39,6 +40,7 @@ public class SecurityConfig {
private final JwtTokenProvider jwtTokenProvider;
private final CustomOAuth2UserService customOAuth2UserService;
private final OAuth2LoginSuccessHandler oAuth2LoginSuccessHandler;
private final OAuth2LoginFailureHandler oAuth2LoginFailureHandler;

private final UrlBasedCorsConfigurationSource ConfigurationSource;
private final SecurityProperties securityProperties;
Expand Down Expand Up @@ -83,7 +85,8 @@ public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Excepti
.oauth2Login((oauth2) -> oauth2
.userInfoEndpoint(userInfoEndpoint -> userInfoEndpoint
.userService(customOAuth2UserService))
.successHandler(oAuth2LoginSuccessHandler));
.successHandler(oAuth2LoginSuccessHandler)
.failureHandler(oAuth2LoginFailureHandler));

return http.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package aimo.backend.common.security.oAuth;

import java.io.IOException;

import org.springframework.http.HttpStatus;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;

import aimo.backend.common.dto.DataResponse;
import aimo.backend.common.util.responseWriter.ResponseWriter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class OAuth2LoginFailureHandler extends SimpleUrlAuthenticationFailureHandler {

@Override
public void onAuthenticationFailure(
HttpServletRequest request,
HttpServletResponse response,
AuthenticationException exception
) {

log.error("OAuth2 authentication failed", exception);

ResponseWriter.writeResponse(response, DataResponse.ok(), HttpStatus.OK);
}
}

0 comments on commit 0e0a2fd

Please sign in to comment.