diff --git a/src/main/java/com/somemore/global/auth/oauth/handler/failure/CustomOAuthFailureHandler.java b/src/main/java/com/somemore/global/auth/oauth/handler/CustomOAuthFailureHandler.java similarity index 64% rename from src/main/java/com/somemore/global/auth/oauth/handler/failure/CustomOAuthFailureHandler.java rename to src/main/java/com/somemore/global/auth/oauth/handler/CustomOAuthFailureHandler.java index 7278b423d..2858062f8 100644 --- a/src/main/java/com/somemore/global/auth/oauth/handler/failure/CustomOAuthFailureHandler.java +++ b/src/main/java/com/somemore/global/auth/oauth/handler/CustomOAuthFailureHandler.java @@ -1,5 +1,6 @@ -package com.somemore.global.auth.oauth.handler.failure; +package com.somemore.global.auth.oauth.handler; +import com.somemore.global.auth.redirect.RedirectUseCase; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; @@ -13,11 +14,13 @@ @Slf4j public class CustomOAuthFailureHandler extends SimpleUrlAuthenticationFailureHandler { + private final RedirectUseCase redirectUseCase; + + private static final String FAILURE_PATH = "/error/login"; + @Override public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) { - // TODO 프론트엔드와 협의 - log.error("안녕 난 말하는 감자야"); - log.error(exception.getMessage()); - log.error(exception.getCause().getMessage()); + redirectUseCase.redirect(request, response, FAILURE_PATH); + log.error("OAuth 로그인 실패: {}", exception.getMessage()); } } diff --git a/src/main/java/com/somemore/global/auth/oauth/handler/success/CustomOAuthSuccessHandler.java b/src/main/java/com/somemore/global/auth/oauth/handler/CustomOAuthSuccessHandler.java similarity index 98% rename from src/main/java/com/somemore/global/auth/oauth/handler/success/CustomOAuthSuccessHandler.java rename to src/main/java/com/somemore/global/auth/oauth/handler/CustomOAuthSuccessHandler.java index 443939136..98dab7682 100644 --- a/src/main/java/com/somemore/global/auth/oauth/handler/success/CustomOAuthSuccessHandler.java +++ b/src/main/java/com/somemore/global/auth/oauth/handler/CustomOAuthSuccessHandler.java @@ -1,4 +1,4 @@ -package com.somemore.global.auth.oauth.handler.success; +package com.somemore.global.auth.oauth.handler; import com.somemore.global.auth.cookie.CookieUseCase; import com.somemore.global.auth.jwt.domain.EncodedToken; diff --git a/src/main/java/com/somemore/global/auth/redirect/RedirectService.java b/src/main/java/com/somemore/global/auth/redirect/RedirectService.java index d6d48a99c..36285b9a5 100644 --- a/src/main/java/com/somemore/global/auth/redirect/RedirectService.java +++ b/src/main/java/com/somemore/global/auth/redirect/RedirectService.java @@ -4,6 +4,7 @@ import jakarta.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; import org.springframework.security.web.RedirectStrategy; import org.springframework.stereotype.Service; @@ -14,10 +15,33 @@ @Slf4j public class RedirectService implements RedirectUseCase { + @Value("${app.front-url}") + private String frontUrl; + private final RedirectStrategy redirectStrategy; + private static final String ERROR_PATH = "/error"; + @Override - public void redirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException { - redirectStrategy.sendRedirect(request, response, url); + public void redirect(HttpServletRequest request, HttpServletResponse response, String path) { + try { + redirectStrategy.sendRedirect(request, response, path); + } catch (IOException e) { + log.error("리디렉션 중 오류 발생 - 대상 URL: {}, 메시지: {}", path, e.getMessage()); + handleRedirectFailure(request, response); + } + } + + private void handleRedirectFailure(HttpServletRequest request, HttpServletResponse response) { + String fallbackUrl = buildFallbackUrl(); + try { + redirectStrategy.sendRedirect(request, response, fallbackUrl); + } catch (IOException e) { + log.error("에러 페이지로 리디렉션 시도 중 오류 발생 - 메시지: {}", e.getMessage()); + } + } + + private String buildFallbackUrl() { + return frontUrl + RedirectService.ERROR_PATH; } } diff --git a/src/main/java/com/somemore/global/auth/redirect/RedirectUseCase.java b/src/main/java/com/somemore/global/auth/redirect/RedirectUseCase.java index 5b23905d3..96a8ea638 100644 --- a/src/main/java/com/somemore/global/auth/redirect/RedirectUseCase.java +++ b/src/main/java/com/somemore/global/auth/redirect/RedirectUseCase.java @@ -2,8 +2,7 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import java.io.IOException; public interface RedirectUseCase { - void redirect(HttpServletRequest request, HttpServletResponse response, String url) throws IOException; + void redirect(HttpServletRequest request, HttpServletResponse response, String path); } diff --git a/src/main/java/com/somemore/global/config/SecurityConfig.java b/src/main/java/com/somemore/global/config/SecurityConfig.java index 98f60a701..c4f14db2a 100644 --- a/src/main/java/com/somemore/global/config/SecurityConfig.java +++ b/src/main/java/com/somemore/global/config/SecurityConfig.java @@ -6,8 +6,8 @@ import com.somemore.global.auth.jwt.filter.JwtAuthFilter; import com.somemore.global.auth.jwt.filter.JwtExceptionFilter; import com.somemore.global.auth.jwt.usecase.GenerateTokensOnLoginUseCase; -import com.somemore.global.auth.oauth.handler.failure.CustomOAuthFailureHandler; -import com.somemore.global.auth.oauth.handler.success.CustomOAuthSuccessHandler; +import com.somemore.global.auth.oauth.handler.CustomOAuthFailureHandler; +import com.somemore.global.auth.oauth.handler.CustomOAuthSuccessHandler; import com.somemore.global.auth.oauth.service.CustomOAuth2UserService; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean;