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..694205e 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(oAuthFailureHandler())) .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()); + } +}