From 70f19b4770a5b6463ef5911b25de05a17e891a97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=EC=9E=AC=EC=A4=91?= <126754298+m-a-king@users.noreply.github.com> Date: Tue, 31 Dec 2024 14:40:33 +0900 Subject: [PATCH 1/3] =?UTF-8?q?feat(redirection):=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=20=EB=A6=AC=EB=94=94=EB=A0=89=EC=85=98=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 로직 개선 - 실패 케이스 추가 --- .../CustomOAuthFailureHandler.java | 13 ++++---- .../global/auth/redirect/RedirectService.java | 30 +++++++++++++++++-- .../global/auth/redirect/RedirectUseCase.java | 3 +- 3 files changed, 36 insertions(+), 10 deletions(-) rename src/main/java/com/somemore/global/auth/oauth/handler/{failure => }/CustomOAuthFailureHandler.java (64%) 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/redirect/RedirectService.java b/src/main/java/com/somemore/global/auth/redirect/RedirectService.java index d6d48a99c..ac9acc85f 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; } -} +} \ No newline at end of file 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); } From 9b6c4483bdf4b0b4f740bf3a45b360f5d6285327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=EC=9E=AC=EC=A4=91?= <126754298+m-a-king@users.noreply.github.com> Date: Tue, 31 Dec 2024 14:41:03 +0900 Subject: [PATCH 2/3] =?UTF-8?q?refactor:=20=ED=8C=A8=ED=82=A4=EC=A7=80=20?= =?UTF-8?q?=EC=9D=B4=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../handler/{success => }/CustomOAuthSuccessHandler.java | 2 +- src/main/java/com/somemore/global/config/SecurityConfig.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/main/java/com/somemore/global/auth/oauth/handler/{success => }/CustomOAuthSuccessHandler.java (98%) 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/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; From d3c579ec6ace95eeebba6d8fa34b6b1c31301753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A1=B0=EC=9E=AC=EC=A4=91?= <126754298+m-a-king@users.noreply.github.com> Date: Tue, 31 Dec 2024 14:45:08 +0900 Subject: [PATCH 3/3] =?UTF-8?q?refactor:=20=EA=B9=83=ED=97=88=EB=B8=8C=20?= =?UTF-8?q?=EA=B2=BD=EA=B3=A0=20=EA=B0=9C=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/somemore/global/auth/redirect/RedirectService.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ac9acc85f..36285b9a5 100644 --- a/src/main/java/com/somemore/global/auth/redirect/RedirectService.java +++ b/src/main/java/com/somemore/global/auth/redirect/RedirectService.java @@ -44,4 +44,4 @@ private void handleRedirectFailure(HttpServletRequest request, HttpServletRespon private String buildFallbackUrl() { return frontUrl + RedirectService.ERROR_PATH; } -} \ No newline at end of file +}