From 337e080788f8261fc62a1054e29be9cf7aa407f9 Mon Sep 17 00:00:00 2001 From: JeonHaeseung <414catherine@gmail.com> Date: Wed, 12 Jun 2024 01:28:51 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[!HOTFIX]=20error=20handler=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80=EB=A1=9C=20exception=20=ED=99=95=EC=9D=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../auth/oAuth/OAuthFailureHandler.java | 21 +++++++++++++++++++ .../domain/auth/security/SecurityConfig.java | 9 +++++++- .../Ness/Backend/global/error/ErrorCode.java | 1 + .../exception/OAuthVerificationException.java | 16 ++++++++++++++ 4 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 src/main/java/Ness/Backend/domain/auth/oAuth/OAuthFailureHandler.java create mode 100644 src/main/java/Ness/Backend/global/error/exception/OAuthVerificationException.java diff --git a/src/main/java/Ness/Backend/domain/auth/oAuth/OAuthFailureHandler.java b/src/main/java/Ness/Backend/domain/auth/oAuth/OAuthFailureHandler.java new file mode 100644 index 0000000..a56c3b8 --- /dev/null +++ b/src/main/java/Ness/Backend/domain/auth/oAuth/OAuthFailureHandler.java @@ -0,0 +1,21 @@ +package Ness.Backend.domain.auth.oAuth; + +import Ness.Backend.global.error.ErrorCode; +import Ness.Backend.global.error.exception.OAuthVerificationException; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.authentication.AuthenticationFailureHandler; + +import java.io.IOException; +@Slf4j +@RequiredArgsConstructor +public class OAuthFailureHandler implements AuthenticationFailureHandler { + @Override + public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException { + throw new OAuthVerificationException(exception.getMessage()); + } +} \ No newline at end of file diff --git a/src/main/java/Ness/Backend/domain/auth/security/SecurityConfig.java b/src/main/java/Ness/Backend/domain/auth/security/SecurityConfig.java index 4e174d0..ed937fd 100644 --- a/src/main/java/Ness/Backend/domain/auth/security/SecurityConfig.java +++ b/src/main/java/Ness/Backend/domain/auth/security/SecurityConfig.java @@ -5,6 +5,7 @@ import Ness.Backend.domain.auth.jwt.JwtAuthorizationFilter; import Ness.Backend.domain.auth.jwt.JwtTokenProvider; import Ness.Backend.domain.auth.oAuth.OAuth2CustomUserService; +import Ness.Backend.domain.auth.oAuth.OAuthFailureHandler; import Ness.Backend.domain.auth.oAuth.OAuthSuccessHandler; import Ness.Backend.domain.member.MemberRepository; import jakarta.servlet.DispatcherType; @@ -44,6 +45,11 @@ public OAuthSuccessHandler oAuthSuccessHandler(){ return new OAuthSuccessHandler(jwtTokenProvider()); } + @Bean + public OAuthFailureHandler oAuthFailureHandler(){ + return new OAuthFailureHandler(); + } + /* 로그인: 사용자 정보(memberRepository 내용)를 토대로 토큰을 생성하거나 검증 */ @Bean public JwtTokenProvider jwtTokenProvider() { @@ -91,7 +97,8 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti .oauth2Login((oauth2) -> oauth2 //oauth가 성공하면 보내줄 포인트 .userInfoEndpoint(userInfoEndpoint -> userInfoEndpoint .userService(oAuth2CustomUserService)) - .successHandler(oAuthSuccessHandler())) + .successHandler(oAuthSuccessHandler()) + .failureHandler(()) .authorizeHttpRequests(requests -> requests .dispatcherTypeMatchers(DispatcherType.FORWARD).permitAll() //.requestMatchers("/signup/**", "/login/**").permitAll() // 회원가입 및 로그인 경로는 인증 생략 diff --git a/src/main/java/Ness/Backend/global/error/ErrorCode.java b/src/main/java/Ness/Backend/global/error/ErrorCode.java index cbaa3e9..9b86c74 100644 --- a/src/main/java/Ness/Backend/global/error/ErrorCode.java +++ b/src/main/java/Ness/Backend/global/error/ErrorCode.java @@ -30,6 +30,7 @@ public enum ErrorCode { INVALID_TOKEN_SIGNATURE(BAD_REQUEST, "AUTH010", "유효하지 않은 시그니처를 가진 토큰입니다. 온전한 토큰이 맞는지 확인해주세요."), TOKEN_ERROR(BAD_REQUEST, "AUTH011", "기타 토큰 에러입니다."), INVALID_PRINCIPAL(BAD_REQUEST, "AUTH012", "인증정보가 존재하지 않습니다."), + OAUTH_ERROR(BAD_REQUEST, "AUTH012", "소셜 로그인 에러입니다."), /* 카테고리 관련 */ INVALID_CATEGORY_NAME(CONFLICT, "CATE001", "해당 카테고리명이 이미 존재합니다. 카테고리명은 중복될 수 없습니다."), diff --git a/src/main/java/Ness/Backend/global/error/exception/OAuthVerificationException.java b/src/main/java/Ness/Backend/global/error/exception/OAuthVerificationException.java new file mode 100644 index 0000000..bf7f0b0 --- /dev/null +++ b/src/main/java/Ness/Backend/global/error/exception/OAuthVerificationException.java @@ -0,0 +1,16 @@ +package Ness.Backend.global.error.exception; + +import Ness.Backend.global.error.ErrorCode; +import lombok.Getter; +@Getter +public class OAuthVerificationException extends BaseException { + public OAuthVerificationException() { + super(ErrorCode.OAUTH_ERROR, ErrorCode.OAUTH_ERROR.getMessage()); + } + public OAuthVerificationException(String message) { + super(ErrorCode.OAUTH_ERROR, message); + } + public OAuthVerificationException(ErrorCode errorCode) { + super(errorCode, errorCode.getMessage()); + } +} From 082aa02185dba610309ec2ee4fa64fe58b65aceb Mon Sep 17 00:00:00 2001 From: JeonHaeseung <414catherine@gmail.com> Date: Wed, 12 Jun 2024 01:35:21 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[chore]=20=EC=BD=94=EB=93=9C=20=EB=88=84?= =?UTF-8?q?=EB=9D=BD=EB=90=9C=20=EB=B6=80=EB=B6=84=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/Ness/Backend/domain/auth/security/SecurityConfig.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/Ness/Backend/domain/auth/security/SecurityConfig.java b/src/main/java/Ness/Backend/domain/auth/security/SecurityConfig.java index ed937fd..694205e 100644 --- a/src/main/java/Ness/Backend/domain/auth/security/SecurityConfig.java +++ b/src/main/java/Ness/Backend/domain/auth/security/SecurityConfig.java @@ -98,7 +98,7 @@ public SecurityFilterChain filterChain(HttpSecurity httpSecurity) throws Excepti .userInfoEndpoint(userInfoEndpoint -> userInfoEndpoint .userService(oAuth2CustomUserService)) .successHandler(oAuthSuccessHandler()) - .failureHandler(()) + .failureHandler(oAuthFailureHandler())) .authorizeHttpRequests(requests -> requests .dispatcherTypeMatchers(DispatcherType.FORWARD).permitAll() //.requestMatchers("/signup/**", "/login/**").permitAll() // 회원가입 및 로그인 경로는 인증 생략