diff --git a/src/main/java/org/hankki/hankkiserver/api/advice/GlobalExceptionHandler.java b/src/main/java/org/hankki/hankkiserver/api/advice/GlobalExceptionHandler.java index e0003509..31bd8bb1 100644 --- a/src/main/java/org/hankki/hankkiserver/api/advice/GlobalExceptionHandler.java +++ b/src/main/java/org/hankki/hankkiserver/api/advice/GlobalExceptionHandler.java @@ -1,4 +1,41 @@ package org.hankki.hankkiserver.api.advice; +import lombok.extern.slf4j.Slf4j; +import org.hankki.hankkiserver.api.dto.HankkiResponse; +import org.hankki.hankkiserver.common.code.BusinessErrorCode; +import org.hankki.hankkiserver.common.exception.BadRequestException; +import org.hankki.hankkiserver.common.exception.NotFoundException; +import org.hankki.hankkiserver.common.exception.UnauthorizedException; + +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; + +@RestControllerAdvice +@Slf4j public class GlobalExceptionHandler { + + @ExceptionHandler(BadRequestException.class) + public HankkiResponse handleBadRequestException(BadRequestException e) { + log.error("handleBadRequestException() in GlobalExceptionHandler throw BadRequestException : {}", e.getMessage()); + return HankkiResponse.fail(e.getErrorCode()); + } + + @ExceptionHandler(UnauthorizedException.class) + public HankkiResponse handleUnauthorizedException(UnauthorizedException e) { + log.error("handleUnauthorizedException() in GlobalExceptionHandler throw UnauthorizedException : {}", e.getMessage()); + return HankkiResponse.fail(e.getErrorCode()); + } + + @ExceptionHandler(NotFoundException.class) + public HankkiResponse handleEntityNotFoundException(NotFoundException e) { + log.error("handleEntityNotFoundException() in GlobalExceptionHandler throw EntityNotFoundException : {}", e.getMessage()); + return HankkiResponse.fail(e.getErrorCode()); + } + + @ExceptionHandler(Exception.class) + public HankkiResponse handleException(Exception e) { + log.error("handleException() in GlobalExceptionHandler throw Exception : {}", e.getMessage()); + return HankkiResponse.fail(BusinessErrorCode.INTERNAL_SERVER_ERROR); + + } } diff --git a/src/main/java/org/hankki/hankkiserver/api/advice/ResponseAdvice.java b/src/main/java/org/hankki/hankkiserver/api/advice/ResponseAdvice.java new file mode 100644 index 00000000..517d6e41 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/advice/ResponseAdvice.java @@ -0,0 +1,29 @@ +package org.hankki.hankkiserver.api.advice; + +import org.hankki.hankkiserver.api.dto.HankkiResponse; +import org.springframework.core.MethodParameter; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.server.ServerHttpRequest; +import org.springframework.http.server.ServerHttpResponse; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice; + +@RestControllerAdvice(basePackages = "org.hankki.hankkiserver.api") +public class ResponseAdvice implements ResponseBodyAdvice { + + @Override + public boolean supports(MethodParameter returnType, Class converterType) { + return returnType.getParameterType() == HankkiResponse.class; + } + + @Override + public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) { + if (returnType.getParameterType() == HankkiResponse.class) { + HttpStatus status = HttpStatus.valueOf(((HankkiResponse) body).getCode()); + response.setStatusCode(status); + } + return body; + } + +} diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/controller/AuthController.java b/src/main/java/org/hankki/hankkiserver/api/auth/controller/AuthController.java index 7bf77a79..dae2ad91 100644 --- a/src/main/java/org/hankki/hankkiserver/api/auth/controller/AuthController.java +++ b/src/main/java/org/hankki/hankkiserver/api/auth/controller/AuthController.java @@ -3,15 +3,13 @@ import jakarta.annotation.Nullable; import lombok.RequiredArgsConstructor; import org.hankki.hankkiserver.auth.UserId; -import org.hankki.hankkiserver.api.dto.ApiResponse; -import org.hankki.hankkiserver.api.dto.BaseResponse; -import org.hankki.hankkiserver.common.code.SuccessCode; +import org.hankki.hankkiserver.api.dto.HankkiResponse; +import org.hankki.hankkiserver.common.code.CommonSuccessCode; import org.hankki.hankkiserver.api.auth.service.AuthService; import org.hankki.hankkiserver.api.auth.controller.request.UserLoginRequest; import org.hankki.hankkiserver.api.auth.service.response.UserLoginResponse; import org.hankki.hankkiserver.api.auth.service.response.UserReissueResponse; import org.springframework.http.HttpHeaders; -import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; @RestController @@ -22,32 +20,32 @@ public class AuthController { private final AuthService authService; @PostMapping("/auth/login") - public ResponseEntity> login( + public HankkiResponse login( @RequestHeader(HttpHeaders.AUTHORIZATION) final String token, @RequestBody final UserLoginRequest request) { final UserLoginResponse response = authService.login(token, request); - return ApiResponse.success(SuccessCode.OK, response); + return HankkiResponse.success(CommonSuccessCode.OK, response); } @PatchMapping("/auth/logout") - public ResponseEntity> signOut( + public HankkiResponse signOut( @UserId final Long userId) { authService.logOut(userId); - return ApiResponse.success(SuccessCode.OK); + return HankkiResponse.success(CommonSuccessCode.OK); } @DeleteMapping("/auth/withdraw") - public ResponseEntity> withdraw( + public HankkiResponse withdraw( @UserId final Long userId, @Nullable @RequestHeader("X-Apple-Code") final String code){ authService.withdraw(userId,code); - return ApiResponse.success(SuccessCode.NO_CONTENT); + return HankkiResponse.success(CommonSuccessCode.NO_CONTENT); } @PostMapping("/auth/reissue") - public ResponseEntity> reissue( + public HankkiResponse reissue( @RequestHeader(HttpHeaders.AUTHORIZATION) final String refreshToken) { final UserReissueResponse response = authService.reissue(refreshToken); - return ApiResponse.success(SuccessCode.OK, response); + return HankkiResponse.success(CommonSuccessCode.OK, response); } } diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/service/AuthService.java b/src/main/java/org/hankki/hankkiserver/api/auth/service/AuthService.java index 81fa93b2..c8863d4d 100644 --- a/src/main/java/org/hankki/hankkiserver/api/auth/service/AuthService.java +++ b/src/main/java/org/hankki/hankkiserver/api/auth/service/AuthService.java @@ -4,11 +4,11 @@ import org.hankki.hankkiserver.auth.jwt.JwtProvider; import org.hankki.hankkiserver.auth.jwt.JwtValidator; import org.hankki.hankkiserver.auth.jwt.Token; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.hankki.hankkiserver.domain.user.model.User; import org.hankki.hankkiserver.domain.user.model.UserInfo; import org.hankki.hankkiserver.domain.user.model.Platform; -import org.hankki.hankkiserver.common.exception.InvalidValueException; +import org.hankki.hankkiserver.common.exception.BadRequestException; import org.hankki.hankkiserver.common.exception.UnauthorizedException; import org.hankki.hankkiserver.external.openfeign.apple.AppleOAuthProvider; import org.hankki.hankkiserver.external.openfeign.dto.SocialInfoDto; @@ -19,8 +19,6 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.Optional; - import static org.hankki.hankkiserver.domain.user.model.MemberStatus.ACTIVE; import static org.hankki.hankkiserver.domain.user.model.Platform.APPLE; import static org.hankki.hankkiserver.domain.user.model.Platform.KAKAO; @@ -65,7 +63,7 @@ public void withdraw(final long userId, final String code) { String refreshToken = appleOAuthProvider.getAppleToken(code); appleOAuthProvider.requestRevoke(refreshToken); } catch (Exception e) { - throw new InvalidValueException(ErrorCode.APPLE_REVOKE_FAILED); + throw new BadRequestException(AuthErrorCode.APPLE_REVOKE_FAILED); } } user.softDelete(); diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/service/UserFinder.java b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserFinder.java index 0d2b4320..5dc0c0a9 100644 --- a/src/main/java/org/hankki/hankkiserver/api/auth/service/UserFinder.java +++ b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserFinder.java @@ -1,8 +1,8 @@ package org.hankki.hankkiserver.api.auth.service; import lombok.RequiredArgsConstructor; -import org.hankki.hankkiserver.common.code.ErrorCode; -import org.hankki.hankkiserver.common.exception.EntityNotFoundException; +import org.hankki.hankkiserver.common.code.UserErrorCode; +import org.hankki.hankkiserver.common.exception.NotFoundException; import org.hankki.hankkiserver.domain.user.model.Platform; import org.hankki.hankkiserver.domain.user.model.User; import org.hankki.hankkiserver.domain.user.repository.UserRepository; @@ -21,7 +21,7 @@ public class UserFinder { public User getUser(final long userId) { return userRepository.findById(userId) - .orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND)); + .orElseThrow(() -> new NotFoundException(UserErrorCode.USER_NOT_FOUND)); } public boolean isRegisteredUser(final Platform platform, final SocialInfoDto socialInfo) { diff --git a/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoFinder.java b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoFinder.java index b34a1dd7..f6e1788f 100644 --- a/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoFinder.java +++ b/src/main/java/org/hankki/hankkiserver/api/auth/service/UserInfoFinder.java @@ -1,8 +1,8 @@ package org.hankki.hankkiserver.api.auth.service; import lombok.RequiredArgsConstructor; -import org.hankki.hankkiserver.common.code.ErrorCode; -import org.hankki.hankkiserver.common.exception.EntityNotFoundException; +import org.hankki.hankkiserver.common.code.UserErrorCode; +import org.hankki.hankkiserver.common.exception.NotFoundException; import org.hankki.hankkiserver.domain.user.model.UserInfo; import org.hankki.hankkiserver.domain.user.repository.UserInfoRepository; import org.springframework.stereotype.Component; @@ -15,6 +15,6 @@ public class UserInfoFinder { public UserInfo getUserInfo(final long userId) { return userInfoRepository.findByUserId(userId) - .orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_INFO_NOT_FOUND)); + .orElseThrow(() -> new NotFoundException(UserErrorCode.USER_INFO_NOT_FOUND)); } } diff --git a/src/main/java/org/hankki/hankkiserver/api/dto/ApiResponse.java b/src/main/java/org/hankki/hankkiserver/api/dto/ApiResponse.java deleted file mode 100644 index 36154eec..00000000 --- a/src/main/java/org/hankki/hankkiserver/api/dto/ApiResponse.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.hankki.hankkiserver.api.dto; - -import org.hankki.hankkiserver.common.code.ErrorCode; -import org.hankki.hankkiserver.common.code.SuccessCode; -import org.springframework.http.ResponseEntity; - -public interface ApiResponse { - - static ResponseEntity> success(SuccessCode successCode) { - return ResponseEntity.status(successCode.getHttpStatus()) - .body(BaseResponse.of(successCode)); - } - - static ResponseEntity> success(SuccessCode successCode, T data) { - return org.springframework.http.ResponseEntity.status(successCode.getHttpStatus()) - .body(BaseResponse.of(successCode, data)); - } - - static ResponseEntity> failure(ErrorCode errorCode) { - return ResponseEntity.status(errorCode.getHttpStatus()) - .body(BaseResponse.of(errorCode)); - } -} diff --git a/src/main/java/org/hankki/hankkiserver/api/dto/BaseResponse.java b/src/main/java/org/hankki/hankkiserver/api/dto/BaseResponse.java deleted file mode 100644 index f0646334..00000000 --- a/src/main/java/org/hankki/hankkiserver/api/dto/BaseResponse.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.hankki.hankkiserver.api.dto; - -import com.fasterxml.jackson.annotation.JsonInclude; -import org.hankki.hankkiserver.common.code.ErrorCode; -import org.hankki.hankkiserver.common.code.SuccessCode; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; - -@Getter -@Builder(access = AccessLevel.PRIVATE) -@AllArgsConstructor(access = AccessLevel.PRIVATE) -public class BaseResponse { - - private final int status; - private final String message; - @JsonInclude(value = JsonInclude.Include.NON_NULL) - private final T data; - - public static BaseResponse of(SuccessCode successCode) { - return builder() - .status(successCode.getHttpStatus().value()) - .message(successCode.getMessage()) - .build(); - } - - public static BaseResponse of(SuccessCode successCode, T data) { - return builder() - .status(successCode.getHttpStatus().value()) - .message(successCode.getMessage()) - .data(data) - .build(); - } - - public static BaseResponse of(ErrorCode errorCode) { - return builder() - .status(errorCode.getHttpStatus().value()) - .message(errorCode.getMessage()) - .build(); - } -} diff --git a/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java b/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java new file mode 100644 index 00000000..4e2d6b00 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java @@ -0,0 +1,30 @@ +package org.hankki.hankkiserver.api.dto; + +import com.fasterxml.jackson.annotation.JsonInclude; +import lombok.*; +import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.SuccessCode; + +@Getter +@Builder(access = AccessLevel.PRIVATE) +@AllArgsConstructor(access = AccessLevel.PRIVATE) +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +public class HankkiResponse { + + private final int code; + private final String message; + @JsonInclude(value = JsonInclude.Include.NON_NULL) + private T data; + + public static HankkiResponse success(SuccessCode success) { + return new HankkiResponse<>(success.getHttpStatus().value(), success.getMessage()); + } + + public static HankkiResponse success(SuccessCode success, T data) { + return new HankkiResponse<>(success.getHttpStatus().value(), success.getMessage(), data); + } + + public static HankkiResponse fail(ErrorCode error) { + return new HankkiResponse<>(error.getHttpStatus().value(), error.getMessage()); + } +} diff --git a/src/main/java/org/hankki/hankkiserver/auth/JwtAuthenticationEntryPoint.java b/src/main/java/org/hankki/hankkiserver/auth/JwtAuthenticationEntryPoint.java index 98a96daf..2460af36 100644 --- a/src/main/java/org/hankki/hankkiserver/auth/JwtAuthenticationEntryPoint.java +++ b/src/main/java/org/hankki/hankkiserver/auth/JwtAuthenticationEntryPoint.java @@ -3,8 +3,8 @@ import com.fasterxml.jackson.databind.ObjectMapper; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import org.hankki.hankkiserver.api.dto.BaseResponse; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.api.dto.HankkiResponse; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.security.core.AuthenticationException; @@ -28,17 +28,17 @@ public void commence( } private void handleException(HttpServletResponse response) throws IOException { - setResponse(response, HttpStatus.UNAUTHORIZED, ErrorCode.UNAUTHORIZED); + setResponse(response, HttpStatus.UNAUTHORIZED, AuthErrorCode.UNAUTHORIZED); } private void setResponse( HttpServletResponse response, HttpStatus httpStatus, - ErrorCode errorCode) throws IOException { + AuthErrorCode authErrorCode) throws IOException { response.setContentType(MediaType.APPLICATION_JSON_VALUE); response.setCharacterEncoding("utf-8"); response.setStatus(httpStatus.value()); PrintWriter writer = response.getWriter(); - writer.write(objectMapper.writeValueAsString(BaseResponse.of(errorCode))); + writer.write(objectMapper.writeValueAsString(HankkiResponse.fail(authErrorCode))); } } diff --git a/src/main/java/org/hankki/hankkiserver/auth/filter/ExceptionHandlerFilter.java b/src/main/java/org/hankki/hankkiserver/auth/filter/ExceptionHandlerFilter.java index 6bb1be41..817f93e3 100644 --- a/src/main/java/org/hankki/hankkiserver/auth/filter/ExceptionHandlerFilter.java +++ b/src/main/java/org/hankki/hankkiserver/auth/filter/ExceptionHandlerFilter.java @@ -4,7 +4,8 @@ import jakarta.servlet.FilterChain; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; -import org.hankki.hankkiserver.api.dto.BaseResponse; +import org.hankki.hankkiserver.api.dto.HankkiResponse; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.hankki.hankkiserver.common.code.ErrorCode; import org.hankki.hankkiserver.common.exception.UnauthorizedException; import org.springframework.http.HttpStatus; @@ -36,25 +37,25 @@ private void handleUnauthorizedException( HttpServletResponse response, Exception e) throws IOException { UnauthorizedException ue = (UnauthorizedException) e; - ErrorCode errorCode = ue.getErrorCode(); - HttpStatus httpStatus = errorCode.getHttpStatus(); - setResponse(response, httpStatus, errorCode); + ErrorCode authErrorCode = ue.getErrorCode(); + HttpStatus httpStatus = authErrorCode.getHttpStatus(); + setResponse(response, httpStatus, authErrorCode); } private void handleException( HttpServletResponse response, Exception e) throws IOException { - setResponse(response, HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.INTERNAL_SERVER_ERROR); + setResponse(response, HttpStatus.INTERNAL_SERVER_ERROR, AuthErrorCode.INTERNAL_SERVER_ERROR); } private void setResponse( HttpServletResponse response, HttpStatus httpStatus, - ErrorCode errorCode) throws IOException { + ErrorCode authErrorCode) throws IOException { response.setContentType(MediaType.APPLICATION_JSON_VALUE); response.setCharacterEncoding("utf-8"); response.setStatus(httpStatus.value()); PrintWriter writer = response.getWriter(); - writer.write(objectMapper.writeValueAsString(BaseResponse.of(errorCode))); + writer.write(objectMapper.writeValueAsString(HankkiResponse.fail(authErrorCode))); } } diff --git a/src/main/java/org/hankki/hankkiserver/auth/filter/JwtAuthenticationFilter.java b/src/main/java/org/hankki/hankkiserver/auth/filter/JwtAuthenticationFilter.java index 983ea2e8..fd6514ed 100644 --- a/src/main/java/org/hankki/hankkiserver/auth/filter/JwtAuthenticationFilter.java +++ b/src/main/java/org/hankki/hankkiserver/auth/filter/JwtAuthenticationFilter.java @@ -8,7 +8,7 @@ import org.hankki.hankkiserver.auth.UserAuthentication; import org.hankki.hankkiserver.auth.jwt.JwtProvider; import org.hankki.hankkiserver.auth.jwt.JwtValidator; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.hankki.hankkiserver.common.exception.UnauthorizedException; import org.springframework.security.core.context.SecurityContext; import org.springframework.security.core.context.SecurityContextHolder; @@ -44,7 +44,7 @@ private String getAccessToken(HttpServletRequest request) { if (StringUtils.hasText(accessToken) && accessToken.startsWith(BEARER)) { return accessToken.substring(BEARER.length()); } - throw new UnauthorizedException(ErrorCode.INVALID_ACCESS_TOKEN); + throw new UnauthorizedException(AuthErrorCode.INVALID_ACCESS_TOKEN); } private void doAuthentication( diff --git a/src/main/java/org/hankki/hankkiserver/auth/jwt/JwtValidator.java b/src/main/java/org/hankki/hankkiserver/auth/jwt/JwtValidator.java index 3e042ffa..62048988 100644 --- a/src/main/java/org/hankki/hankkiserver/auth/jwt/JwtValidator.java +++ b/src/main/java/org/hankki/hankkiserver/auth/jwt/JwtValidator.java @@ -3,7 +3,7 @@ import io.jsonwebtoken.ExpiredJwtException; import io.jsonwebtoken.JwtParser; import lombok.RequiredArgsConstructor; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.hankki.hankkiserver.common.exception.UnauthorizedException; import org.springframework.stereotype.Component; @@ -19,9 +19,9 @@ public void validateAccessToken(String accessToken) { try { parseToken(accessToken); } catch (ExpiredJwtException e) { - throw new UnauthorizedException(ErrorCode.EXPIRED_ACCESS_TOKEN); + throw new UnauthorizedException(AuthErrorCode.EXPIRED_ACCESS_TOKEN); } catch (Exception e) { - throw new UnauthorizedException(ErrorCode.INVALID_ACCESS_TOKEN_VALUE); + throw new UnauthorizedException(AuthErrorCode.INVALID_ACCESS_TOKEN_VALUE); } } @@ -29,9 +29,9 @@ public void validateRefreshToken(final String refreshToken) { try { parseToken(getToken(refreshToken)); } catch (ExpiredJwtException e) { - throw new UnauthorizedException(ErrorCode.EXPIRED_REFRESH_TOKEN); + throw new UnauthorizedException(AuthErrorCode.EXPIRED_REFRESH_TOKEN); } catch (Exception e) { - throw new UnauthorizedException(ErrorCode.INVALID_REFRESH_TOKEN_VALUE); + throw new UnauthorizedException(AuthErrorCode.INVALID_REFRESH_TOKEN_VALUE); } } @@ -39,7 +39,7 @@ public void equalsRefreshToken( final String refreshToken, final String storedRefreshToken) { if (!getToken(refreshToken).equals(storedRefreshToken)) { - throw new UnauthorizedException(ErrorCode.MISMATCH_REFRESH_TOKEN); + throw new UnauthorizedException(AuthErrorCode.MISMATCH_REFRESH_TOKEN); } } @@ -52,6 +52,6 @@ private String getToken(final String refreshToken) { if (refreshToken.startsWith(BEARER)) { return refreshToken.substring(BEARER.length()); } - throw new UnauthorizedException(ErrorCode.INVALID_ACCESS_TOKEN); + throw new UnauthorizedException(AuthErrorCode.MISSING_BEARER_PREFIX); } } diff --git a/src/main/java/org/hankki/hankkiserver/common/code/AuthErrorCode.java b/src/main/java/org/hankki/hankkiserver/common/code/AuthErrorCode.java new file mode 100644 index 00000000..7d0b6b06 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/common/code/AuthErrorCode.java @@ -0,0 +1,45 @@ +package org.hankki.hankkiserver.common.code; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; + +@Getter +@RequiredArgsConstructor +public enum AuthErrorCode implements ErrorCode { + + /** + * 400 Bad Request + **/ + INVALID_PLATFORM_TYPE(HttpStatus.BAD_REQUEST, "유효하지 않은 플랫폼입니다."), + UNSUPPORTED_ALGORITHM(HttpStatus.BAD_REQUEST, "키 생성에 사용된 알고리즘을 지원하지 않습니다: "), + INVALID_KEY_SPEC(HttpStatus.BAD_REQUEST, "공개 키 생성에 잘못된 키 사양이 제공되었습니다."), + FAILED_TO_LOAD_PRIVATE_KEY(HttpStatus.BAD_REQUEST, "개인 키를 로드하는 데 실패했습니다."), + APPLE_REVOKE_FAILED(HttpStatus.BAD_REQUEST, "Apple 탈퇴에 실패했습니다."), + + /** + * 401 Unauthorized + **/ + UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "리소스 접근 권한이 없습니다."), + INVALID_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "액세스 토큰의 형식이 올바르지 않습니다."), + INVALID_ACCESS_TOKEN_VALUE(HttpStatus.UNAUTHORIZED, "액세스 토큰의 값이 올바르지 않습니다."), + EXPIRED_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "액세스 토큰이 만료되었습니다."), + INVALID_REFRESH_TOKEN_VALUE(HttpStatus.UNAUTHORIZED, "리프레시 토큰의 값이 올바르지 않습니다."), + EXPIRED_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "리프레시 토큰이 만료되었습니다."), + MISMATCH_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "리프레시 토큰이 일치하지 않습니다."), + INVALID_APPLE_IDENTITY_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 아이덴티티 토큰입니다."), + EXPIRED_IDENTITY_TOKEN(HttpStatus.UNAUTHORIZED, "아이덴티티 토큰이 만료되었습니다."), + INVALID_IDENTITY_TOKEN_VALUE(HttpStatus.UNAUTHORIZED, "애플 아이덴티티 토큰의 값이 올바르지 않습니다."), + MISSING_BEARER_PREFIX(HttpStatus.UNAUTHORIZED, "Bearer가 누락되었습니다."), + + /** + * 500 Internal Server Error + **/ + INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 오류입니다."); + + + private final HttpStatus httpStatus; + private final String message; + +} diff --git a/src/main/java/org/hankki/hankkiserver/common/code/BusinessErrorCode.java b/src/main/java/org/hankki/hankkiserver/common/code/BusinessErrorCode.java new file mode 100644 index 00000000..dfe2bb12 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/common/code/BusinessErrorCode.java @@ -0,0 +1,15 @@ +package org.hankki.hankkiserver.common.code; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; + +@Getter +@RequiredArgsConstructor +public enum BusinessErrorCode implements ErrorCode { + + INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 내부 오류가 발생했습니다."); + + private final HttpStatus httpStatus; + private final String message; +} diff --git a/src/main/java/org/hankki/hankkiserver/common/code/CommonSuccessCode.java b/src/main/java/org/hankki/hankkiserver/common/code/CommonSuccessCode.java new file mode 100644 index 00000000..852da4d5 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/common/code/CommonSuccessCode.java @@ -0,0 +1,19 @@ +package org.hankki.hankkiserver.common.code; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; + +@Getter +@RequiredArgsConstructor +public enum CommonSuccessCode implements SuccessCode { + + OK(HttpStatus.OK, "요청이 성공했습니다(200)."), + NO_CONTENT(HttpStatus.NO_CONTENT, "요청이 성공했습니다(204)."), + CREATED(HttpStatus.CREATED, "리소스가 생성되었습니다."); + + private final HttpStatus httpStatus; + private final String message; + +} diff --git a/src/main/java/org/hankki/hankkiserver/common/code/ErrorCode.java b/src/main/java/org/hankki/hankkiserver/common/code/ErrorCode.java index e5851852..5c3d8aaa 100644 --- a/src/main/java/org/hankki/hankkiserver/common/code/ErrorCode.java +++ b/src/main/java/org/hankki/hankkiserver/common/code/ErrorCode.java @@ -1,48 +1,10 @@ package org.hankki.hankkiserver.common.code; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; -@Getter -@RequiredArgsConstructor(access = AccessLevel.PRIVATE) -public enum ErrorCode { +public interface ErrorCode { - // reuqest error - BAD_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), - INVALID_PLATFORM_TYPE(HttpStatus.BAD_REQUEST, "유효하지 않은 플랫폼입니다."), - UNSUPPORTED_ALGORITHM(HttpStatus.BAD_REQUEST, "키 생성에 사용된 알고리즘을 지원하지 않습니다: "), - INVALID_KEY_SPEC(HttpStatus.BAD_REQUEST, "공개 키 생성에 잘못된 키 사양이 제공되었습니다."), - FAILED_TO_LOAD_PRIVATE_KEY(HttpStatus.BAD_REQUEST, "개인 키를 로드하는 데 실패했습니다."), - APPLE_REVOKE_FAILED(HttpStatus.BAD_REQUEST, "Apple 탈퇴에 실패했습니다."), + HttpStatus getHttpStatus(); + String getMessage(); - // unauthorized error - UNAUTHORIZED(HttpStatus.UNAUTHORIZED, "리소스 접근 권한이 없습니다."), - INVALID_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "액세스 토큰의 형식이 올바르지 않습니다."), - INVALID_ACCESS_TOKEN_VALUE(HttpStatus.UNAUTHORIZED, "액세스 토큰의 값이 올바르지 않습니다."), - EXPIRED_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "액세스 토큰이 만료되었습니다."), - INVALID_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "리프레시 토큰의 형식이 올바르지 않습니다."), - INVALID_REFRESH_TOKEN_VALUE(HttpStatus.UNAUTHORIZED, "리프레시 토큰의 값이 올바르지 않습니다."), - EXPIRED_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "리프레시 토큰이 만료되었습니다."), - MISMATCH_REFRESH_TOKEN(HttpStatus.UNAUTHORIZED, "리프레시 토큰이 일치하지 않습니다."), - INVALID_KAKAO_ACCESS_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 카카오 엑세스 토큰입니다."), - INVALID_APPLE_IDENTITY_TOKEN(HttpStatus.UNAUTHORIZED, "유효하지 않은 아이덴티티 토큰입니다."), - EXPIRED_IDENTITY_TOKEN(HttpStatus.UNAUTHORIZED, "아이덴티티 토큰이 만료되었습니다."), - INVALID_IDENTITY_TOKEN_VALUE(HttpStatus.UNAUTHORIZED, "애플 아이덴티티 토큰의 값이 올바르지 않습니다."), - - // not found error - USER_NOT_FOUND(HttpStatus.NOT_FOUND, "등록되지 않은 회원입니다."), - ENTITY_NOT_FOUND(HttpStatus.NOT_FOUND, "대상을 찾을 수 없습니다."), - USER_INFO_NOT_FOUND(HttpStatus.NOT_FOUND, "회원 정보를 찾을 수 없습니다."), - REFRESH_TOKEN_NOT_FOUND(HttpStatus.NOT_FOUND, "리프레쉬 토큰을 찾을 수 없습니다."), - - // internal server error - INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 오류입니다."), - - // method not allowed error - METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "잘못된 HTTP method 요청입니다."),; - - private final HttpStatus httpStatus; - private final String message; } diff --git a/src/main/java/org/hankki/hankkiserver/common/code/SuccessCode.java b/src/main/java/org/hankki/hankkiserver/common/code/SuccessCode.java index fb77fd27..9e27bc0c 100644 --- a/src/main/java/org/hankki/hankkiserver/common/code/SuccessCode.java +++ b/src/main/java/org/hankki/hankkiserver/common/code/SuccessCode.java @@ -1,19 +1,10 @@ package org.hankki.hankkiserver.common.code; -import lombok.AccessLevel; -import lombok.Getter; -import lombok.RequiredArgsConstructor; import org.springframework.http.HttpStatus; -@Getter -@RequiredArgsConstructor(access = AccessLevel.PRIVATE) -public enum SuccessCode { - - OK(HttpStatus.OK, "요청이 성공했습니다."), - NO_CONTENT(HttpStatus.NO_CONTENT, "요청이 성공했습니다."), - CREATED(HttpStatus.CREATED, "요청이 성공했습니다."); - - private final HttpStatus httpStatus; - private final String message; +public interface SuccessCode { + HttpStatus getHttpStatus(); + String getMessage(); + } diff --git a/src/main/java/org/hankki/hankkiserver/common/code/UserErrorCode.java b/src/main/java/org/hankki/hankkiserver/common/code/UserErrorCode.java new file mode 100644 index 00000000..05b14ec7 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/common/code/UserErrorCode.java @@ -0,0 +1,16 @@ +package org.hankki.hankkiserver.common.code; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; + +@Getter +@RequiredArgsConstructor +public enum UserErrorCode implements ErrorCode { + + USER_NOT_FOUND(HttpStatus.NOT_FOUND, "등록되지 않은 회원입니다."), + USER_INFO_NOT_FOUND(HttpStatus.NOT_FOUND, "회원 정보를 찾을 수 없습니다."); + + private final HttpStatus httpStatus; + private final String message; +} diff --git a/src/main/java/org/hankki/hankkiserver/common/exception/BadRequestException.java b/src/main/java/org/hankki/hankkiserver/common/exception/BadRequestException.java new file mode 100644 index 00000000..39f5421e --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/common/exception/BadRequestException.java @@ -0,0 +1,14 @@ +package org.hankki.hankkiserver.common.exception; + +import lombok.Getter; +import org.hankki.hankkiserver.common.code.ErrorCode; + +@Getter +public class BadRequestException extends RuntimeException { + private final ErrorCode errorCode; + + public BadRequestException(ErrorCode errorCode) { + super(errorCode.getMessage()); + this.errorCode = errorCode; + } +} \ No newline at end of file diff --git a/src/main/java/org/hankki/hankkiserver/common/exception/EntityNotFoundException.java b/src/main/java/org/hankki/hankkiserver/common/exception/EntityNotFoundException.java deleted file mode 100644 index 84833a59..00000000 --- a/src/main/java/org/hankki/hankkiserver/common/exception/EntityNotFoundException.java +++ /dev/null @@ -1,13 +0,0 @@ -package org.hankki.hankkiserver.common.exception; - -import org.hankki.hankkiserver.common.code.ErrorCode; - -public class EntityNotFoundException extends BusinessException { - public EntityNotFoundException() { - super(ErrorCode.ENTITY_NOT_FOUND); - } - - public EntityNotFoundException(ErrorCode errorCode) { - super(errorCode); - } -} diff --git a/src/main/java/org/hankki/hankkiserver/common/exception/InternalServerException.java b/src/main/java/org/hankki/hankkiserver/common/exception/InternalServerException.java new file mode 100644 index 00000000..b6eb32e7 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/common/exception/InternalServerException.java @@ -0,0 +1,12 @@ +package org.hankki.hankkiserver.common.exception; + +import org.hankki.hankkiserver.common.code.ErrorCode; + +public class InternalServerException extends RuntimeException { + private final ErrorCode errorCode; + + public InternalServerException(ErrorCode errorCode) { + super(errorCode.getMessage()); + this.errorCode = errorCode; + } +} diff --git a/src/main/java/org/hankki/hankkiserver/common/exception/InvalidValueException.java b/src/main/java/org/hankki/hankkiserver/common/exception/InvalidValueException.java deleted file mode 100644 index cf8ba4f1..00000000 --- a/src/main/java/org/hankki/hankkiserver/common/exception/InvalidValueException.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.hankki.hankkiserver.common.exception; - -import org.hankki.hankkiserver.common.code.ErrorCode; - -public class InvalidValueException extends BusinessException { - public InvalidValueException() { - super(ErrorCode.BAD_REQUEST); - } - - public InvalidValueException(ErrorCode errorCode) { - super(errorCode); - } -} - diff --git a/src/main/java/org/hankki/hankkiserver/common/exception/BusinessException.java b/src/main/java/org/hankki/hankkiserver/common/exception/NotFoundException.java similarity index 70% rename from src/main/java/org/hankki/hankkiserver/common/exception/BusinessException.java rename to src/main/java/org/hankki/hankkiserver/common/exception/NotFoundException.java index 085ad902..a6db85a7 100644 --- a/src/main/java/org/hankki/hankkiserver/common/exception/BusinessException.java +++ b/src/main/java/org/hankki/hankkiserver/common/exception/NotFoundException.java @@ -1,13 +1,14 @@ package org.hankki.hankkiserver.common.exception; -import org.hankki.hankkiserver.common.code.ErrorCode; import lombok.Getter; +import org.hankki.hankkiserver.common.code.ErrorCode; @Getter -public class BusinessException extends RuntimeException { +public class NotFoundException extends RuntimeException { + private final ErrorCode errorCode; - public BusinessException(ErrorCode errorCode) { + public NotFoundException(ErrorCode errorCode) { super(errorCode.getMessage()); this.errorCode = errorCode; } diff --git a/src/main/java/org/hankki/hankkiserver/common/exception/UnauthorizedException.java b/src/main/java/org/hankki/hankkiserver/common/exception/UnauthorizedException.java index 508b0ff1..f86efcd7 100644 --- a/src/main/java/org/hankki/hankkiserver/common/exception/UnauthorizedException.java +++ b/src/main/java/org/hankki/hankkiserver/common/exception/UnauthorizedException.java @@ -1,14 +1,17 @@ package org.hankki.hankkiserver.common.exception; +import lombok.Getter; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.hankki.hankkiserver.common.code.ErrorCode; -public class UnauthorizedException extends BusinessException { - public UnauthorizedException() { - super(ErrorCode.UNAUTHORIZED); - } +@Getter +public class UnauthorizedException extends RuntimeException { + private final ErrorCode errorCode; public UnauthorizedException(ErrorCode errorCode) { - super(errorCode); + super(errorCode.getMessage()); + this.errorCode = errorCode; } + } diff --git a/src/main/java/org/hankki/hankkiserver/domain/BaseTimeEntity.java b/src/main/java/org/hankki/hankkiserver/domain/BaseTimeEntity.java deleted file mode 100644 index 82d567a2..00000000 --- a/src/main/java/org/hankki/hankkiserver/domain/BaseTimeEntity.java +++ /dev/null @@ -1,21 +0,0 @@ -package org.hankki.hankkiserver.domain; - -import jakarta.persistence.EntityListeners; -import jakarta.persistence.MappedSuperclass; -import org.springframework.data.annotation.CreatedDate; -import org.springframework.data.annotation.LastModifiedDate; -import org.springframework.data.jpa.domain.support.AuditingEntityListener; - -import java.time.LocalDateTime; - -@MappedSuperclass -@EntityListeners(AuditingEntityListener.class) -public abstract class BaseTimeEntity { - - @CreatedDate - private LocalDateTime createdAt; - - @LastModifiedDate - private LocalDateTime updatedAt; -} - diff --git a/src/main/java/org/hankki/hankkiserver/domain/user/model/Platform.java b/src/main/java/org/hankki/hankkiserver/domain/user/model/Platform.java index 5ea84b49..9c78345f 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/user/model/Platform.java +++ b/src/main/java/org/hankki/hankkiserver/domain/user/model/Platform.java @@ -2,8 +2,8 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; -import org.hankki.hankkiserver.common.code.ErrorCode; -import org.hankki.hankkiserver.common.exception.InvalidValueException; +import org.hankki.hankkiserver.common.code.AuthErrorCode; +import org.hankki.hankkiserver.common.exception.BadRequestException; import java.util.Arrays; @@ -20,6 +20,6 @@ public static Platform getEnumPlatformFromStringPlatform(String loginPlatform) { return Arrays.stream(values()) .filter(platform -> platform.loginPlatform.equals(loginPlatform)) .findFirst() - .orElseThrow(() -> new InvalidValueException(ErrorCode.INVALID_PLATFORM_TYPE)); + .orElseThrow(() -> new BadRequestException(AuthErrorCode.INVALID_PLATFORM_TYPE)); } } diff --git a/src/main/java/org/hankki/hankkiserver/domain/user/model/User.java b/src/main/java/org/hankki/hankkiserver/domain/user/model/User.java index 6bd76ae8..eafc7f67 100644 --- a/src/main/java/org/hankki/hankkiserver/domain/user/model/User.java +++ b/src/main/java/org/hankki/hankkiserver/domain/user/model/User.java @@ -3,7 +3,7 @@ import jakarta.persistence.*; import lombok.*; -import org.hankki.hankkiserver.domain.BaseTimeEntity; +import org.hankki.hankkiserver.domain.common.BaseTimeEntity; import java.time.LocalDateTime; diff --git a/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleClientSecretGenerator.java b/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleClientSecretGenerator.java index 83b9bba8..42b55e78 100644 --- a/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleClientSecretGenerator.java +++ b/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleClientSecretGenerator.java @@ -3,8 +3,8 @@ import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import lombok.RequiredArgsConstructor; -import org.hankki.hankkiserver.common.code.ErrorCode; -import org.hankki.hankkiserver.common.exception.InvalidValueException; +import org.hankki.hankkiserver.common.code.AuthErrorCode; +import org.hankki.hankkiserver.common.exception.BadRequestException; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; @@ -41,7 +41,7 @@ protected String generateClientSecret() { .signWith(applePrivateKeyGenerator.getPrivateKey(), SignatureAlgorithm.ES256) .compact(); } catch (Exception e) { - throw new InvalidValueException(ErrorCode.FAILED_TO_LOAD_PRIVATE_KEY); + throw new BadRequestException(AuthErrorCode.FAILED_TO_LOAD_PRIVATE_KEY); } } } diff --git a/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleIdentityTokenParser.java b/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleIdentityTokenParser.java index af835903..c13a1358 100644 --- a/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleIdentityTokenParser.java +++ b/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleIdentityTokenParser.java @@ -3,7 +3,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import io.jsonwebtoken.*; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.hankki.hankkiserver.common.exception.UnauthorizedException; import org.springframework.stereotype.Component; @@ -22,7 +22,7 @@ public Map parseHeaders(String identityToken) { String decodedHeader = new String(Base64.getUrlDecoder().decode(encodedHeader)); return OBJECT_MAPPER.readValue(decodedHeader, Map.class); } catch (JsonProcessingException | ArrayIndexOutOfBoundsException e) { - throw new UnauthorizedException(ErrorCode.INVALID_APPLE_IDENTITY_TOKEN); + throw new UnauthorizedException(AuthErrorCode.INVALID_APPLE_IDENTITY_TOKEN); } } @@ -35,9 +35,9 @@ public Claims parsePublicKeyAndGetClaims(String identityToken, PublicKey publicK .getBody(); } catch (ExpiredJwtException e) { - throw new UnauthorizedException(ErrorCode.EXPIRED_IDENTITY_TOKEN); + throw new UnauthorizedException(AuthErrorCode.EXPIRED_IDENTITY_TOKEN); } catch (UnsupportedJwtException | MalformedJwtException | IllegalArgumentException e) { - throw new UnauthorizedException(ErrorCode.INVALID_IDENTITY_TOKEN_VALUE); + throw new UnauthorizedException(AuthErrorCode.INVALID_IDENTITY_TOKEN_VALUE); } } } diff --git a/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleOAuthProvider.java b/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleOAuthProvider.java index b76056c2..24316783 100644 --- a/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleOAuthProvider.java +++ b/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleOAuthProvider.java @@ -3,8 +3,8 @@ import io.jsonwebtoken.Claims; import lombok.RequiredArgsConstructor; -import org.hankki.hankkiserver.common.code.ErrorCode; -import org.hankki.hankkiserver.common.exception.InvalidValueException; +import org.hankki.hankkiserver.common.code.AuthErrorCode; +import org.hankki.hankkiserver.common.exception.BadRequestException; import org.hankki.hankkiserver.external.openfeign.apple.dto.ApplePublicKeys; import org.hankki.hankkiserver.external.openfeign.apple.dto.AppleTokenRequest; import org.hankki.hankkiserver.external.openfeign.dto.SocialInfoDto; @@ -45,7 +45,7 @@ public String getAppleToken(final String code) { AppleTokenRequest.of(code, clientId, clientSecret)); return appleTokenResponse.refreshToken(); } catch (Exception e) { - throw new InvalidValueException(ErrorCode.FAILED_TO_LOAD_PRIVATE_KEY); + throw new BadRequestException(AuthErrorCode.FAILED_TO_LOAD_PRIVATE_KEY); } } diff --git a/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/ApplePublicKeyGenerator.java b/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/ApplePublicKeyGenerator.java index 95f5ac99..f1324b2f 100644 --- a/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/ApplePublicKeyGenerator.java +++ b/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/ApplePublicKeyGenerator.java @@ -1,7 +1,8 @@ package org.hankki.hankkiserver.external.openfeign.apple; -import org.hankki.hankkiserver.common.code.ErrorCode; -import org.hankki.hankkiserver.common.exception.UnauthorizedException; +import org.hankki.hankkiserver.common.code.AuthErrorCode; +import org.hankki.hankkiserver.common.exception.BadRequestException; +import org.hankki.hankkiserver.common.exception.InternalServerException; import org.hankki.hankkiserver.external.openfeign.apple.dto.ApplePublicKeys; import org.hankki.hankkiserver.external.openfeign.apple.dto.ApplePublicKey; import org.springframework.stereotype.Component; @@ -38,11 +39,11 @@ public PublicKey generatePublicKey( KeyFactory keyFactory = KeyFactory.getInstance(applePublicKey.kty()); return keyFactory.generatePublic(rsaPublicKeySpec); } catch (NoSuchAlgorithmException e) { - throw new UnauthorizedException(ErrorCode.UNSUPPORTED_ALGORITHM); + throw new BadRequestException(AuthErrorCode.UNSUPPORTED_ALGORITHM); } catch (InvalidKeySpecException e) { - throw new UnauthorizedException(ErrorCode.INVALID_KEY_SPEC); + throw new BadRequestException(AuthErrorCode.INVALID_KEY_SPEC); } catch (Exception e) { - throw new UnauthorizedException(ErrorCode.INTERNAL_SERVER_ERROR); + throw new InternalServerException(AuthErrorCode.INTERNAL_SERVER_ERROR); } } } diff --git a/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/dto/ApplePublicKeys.java b/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/dto/ApplePublicKeys.java index c5b06c78..f68a160b 100644 --- a/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/dto/ApplePublicKeys.java +++ b/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/dto/ApplePublicKeys.java @@ -1,6 +1,6 @@ package org.hankki.hankkiserver.external.openfeign.apple.dto; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.hankki.hankkiserver.common.exception.UnauthorizedException; import java.util.List; @@ -13,6 +13,6 @@ public ApplePublicKey getMatchedPublicKey(String kid, String alg) { return this.applePublicKeys.stream() .filter(key -> key.kid().equals(kid) && key.alg().equals(alg)) .findFirst() - .orElseThrow(() -> new UnauthorizedException(ErrorCode.INVALID_APPLE_IDENTITY_TOKEN)); + .orElseThrow(() -> new UnauthorizedException(AuthErrorCode.INVALID_APPLE_IDENTITY_TOKEN)); } }