diff --git a/backend/src/main/java/aimo/backend/common/security/SecurityConfig.java b/backend/src/main/java/aimo/backend/common/security/SecurityConfig.java index e7a46c2..424d976 100644 --- a/backend/src/main/java/aimo/backend/common/security/SecurityConfig.java +++ b/backend/src/main/java/aimo/backend/common/security/SecurityConfig.java @@ -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; @@ -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; @@ -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(); } diff --git a/backend/src/main/java/aimo/backend/common/security/oAuth/OAuth2LoginFailureHandler.java b/backend/src/main/java/aimo/backend/common/security/oAuth/OAuth2LoginFailureHandler.java new file mode 100644 index 0000000..e4d96cf --- /dev/null +++ b/backend/src/main/java/aimo/backend/common/security/oAuth/OAuth2LoginFailureHandler.java @@ -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); + } +}