From 2428b1c3d116a4e008ac1ed00125d353ab73aad4 Mon Sep 17 00:00:00 2001 From: Parkjyun Date: Tue, 9 Jul 2024 01:23:40 +0900 Subject: [PATCH 1/9] [refac] create interface for code --- .../api/auth/controller/AuthController.java | 10 ++-- .../api/auth/service/AuthService.java | 6 +-- .../api/auth/service/UserFinder.java | 4 +- .../api/auth/service/UserInfoFinder.java | 4 +- .../hankkiserver/api/dto/ApiResponse.java | 22 ++++----- .../hankkiserver/api/dto/BaseResponse.java | 22 ++++----- .../auth/JwtAuthenticationEntryPoint.java | 8 ++-- .../auth/filter/ExceptionHandlerFilter.java | 14 +++--- .../auth/filter/JwtAuthenticationFilter.java | 4 +- .../hankkiserver/auth/jwt/JwtValidator.java | 14 +++--- .../common/code/AuthErrorCode.java | 48 +++++++++++++++++++ .../common/code/CommonSuccessCode.java | 19 ++++++++ .../hankkiserver/common/code/ErrorCode.java | 44 ++--------------- .../hankkiserver/common/code/SuccessCode.java | 17 ++----- .../common/exception/BusinessException.java | 10 ++-- .../exception/EntityNotFoundException.java | 8 ++-- .../exception/InvalidValueException.java | 8 ++-- .../exception/UnauthorizedException.java | 8 ++-- .../domain/user/model/Platform.java | 4 +- .../apple/AppleClientSecretGenerator.java | 4 +- .../apple/AppleIdentityTokenParser.java | 8 ++-- .../openfeign/apple/AppleOAuthProvider.java | 4 +- .../apple/ApplePublicKeyGenerator.java | 8 ++-- .../openfeign/apple/dto/ApplePublicKeys.java | 4 +- 24 files changed, 160 insertions(+), 142 deletions(-) create mode 100644 src/main/java/org/hankki/hankkiserver/common/code/AuthErrorCode.java create mode 100644 src/main/java/org/hankki/hankkiserver/common/code/CommonSuccessCode.java 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..40032bdf 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 @@ -5,7 +5,7 @@ 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.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; @@ -26,14 +26,14 @@ public ResponseEntity> 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 ApiResponse.success(CommonSuccessCode.OK, response); } @PatchMapping("/auth/logout") public ResponseEntity> signOut( @UserId final Long userId) { authService.logOut(userId); - return ApiResponse.success(SuccessCode.OK); + return ApiResponse.success(CommonSuccessCode.OK); } @DeleteMapping("/auth/withdraw") @@ -41,13 +41,13 @@ public ResponseEntity> withdraw( @UserId final Long userId, @Nullable @RequestHeader("X-Apple-Code") final String code){ authService.withdraw(userId,code); - return ApiResponse.success(SuccessCode.NO_CONTENT); + return ApiResponse.success(CommonSuccessCode.NO_CONTENT); } @PostMapping("/auth/reissue") public ResponseEntity> reissue( @RequestHeader(HttpHeaders.AUTHORIZATION) final String refreshToken) { final UserReissueResponse response = authService.reissue(refreshToken); - return ApiResponse.success(SuccessCode.OK, response); + return ApiResponse.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..c211d28b 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,7 +4,7 @@ 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; @@ -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 InvalidValueException(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..c3f1b231 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,7 +1,7 @@ package org.hankki.hankkiserver.api.auth.service; import lombok.RequiredArgsConstructor; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.hankki.hankkiserver.common.exception.EntityNotFoundException; import org.hankki.hankkiserver.domain.user.model.Platform; import org.hankki.hankkiserver.domain.user.model.User; @@ -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 EntityNotFoundException(AuthErrorCode.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..e0771824 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,7 +1,7 @@ package org.hankki.hankkiserver.api.auth.service; import lombok.RequiredArgsConstructor; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.hankki.hankkiserver.common.exception.EntityNotFoundException; import org.hankki.hankkiserver.domain.user.model.UserInfo; import org.hankki.hankkiserver.domain.user.repository.UserInfoRepository; @@ -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 EntityNotFoundException(AuthErrorCode.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 index 36154eec..4e72f6c0 100644 --- a/src/main/java/org/hankki/hankkiserver/api/dto/ApiResponse.java +++ b/src/main/java/org/hankki/hankkiserver/api/dto/ApiResponse.java @@ -1,23 +1,23 @@ package org.hankki.hankkiserver.api.dto; -import org.hankki.hankkiserver.common.code.ErrorCode; -import org.hankki.hankkiserver.common.code.SuccessCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; +import org.hankki.hankkiserver.common.code.CommonSuccessCode; 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(CommonSuccessCode commonSuccessCode) { + return ResponseEntity.status(commonSuccessCode.getHttpStatus()) + .body(BaseResponse.of(commonSuccessCode)); } - static ResponseEntity> success(SuccessCode successCode, T data) { - return org.springframework.http.ResponseEntity.status(successCode.getHttpStatus()) - .body(BaseResponse.of(successCode, data)); + static ResponseEntity> success(CommonSuccessCode commonSuccessCode, T data) { + return org.springframework.http.ResponseEntity.status(commonSuccessCode.getHttpStatus()) + .body(BaseResponse.of(commonSuccessCode, data)); } - static ResponseEntity> failure(ErrorCode errorCode) { - return ResponseEntity.status(errorCode.getHttpStatus()) - .body(BaseResponse.of(errorCode)); + static ResponseEntity> failure(AuthErrorCode authErrorCode) { + return ResponseEntity.status(authErrorCode.getHttpStatus()) + .body(BaseResponse.of(authErrorCode)); } } diff --git a/src/main/java/org/hankki/hankkiserver/api/dto/BaseResponse.java b/src/main/java/org/hankki/hankkiserver/api/dto/BaseResponse.java index f0646334..c32f831c 100644 --- a/src/main/java/org/hankki/hankkiserver/api/dto/BaseResponse.java +++ b/src/main/java/org/hankki/hankkiserver/api/dto/BaseResponse.java @@ -1,8 +1,8 @@ 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 org.hankki.hankkiserver.common.code.AuthErrorCode; +import org.hankki.hankkiserver.common.code.CommonSuccessCode; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Builder; @@ -18,25 +18,25 @@ public class BaseResponse { @JsonInclude(value = JsonInclude.Include.NON_NULL) private final T data; - public static BaseResponse of(SuccessCode successCode) { + public static BaseResponse of(CommonSuccessCode commonSuccessCode) { return builder() - .status(successCode.getHttpStatus().value()) - .message(successCode.getMessage()) + .status(commonSuccessCode.getHttpStatus().value()) + .message(commonSuccessCode.getMessage()) .build(); } - public static BaseResponse of(SuccessCode successCode, T data) { + public static BaseResponse of(CommonSuccessCode commonSuccessCode, T data) { return builder() - .status(successCode.getHttpStatus().value()) - .message(successCode.getMessage()) + .status(commonSuccessCode.getHttpStatus().value()) + .message(commonSuccessCode.getMessage()) .data(data) .build(); } - public static BaseResponse of(ErrorCode errorCode) { + public static BaseResponse of(AuthErrorCode authErrorCode) { return builder() - .status(errorCode.getHttpStatus().value()) - .message(errorCode.getMessage()) + .status(authErrorCode.getHttpStatus().value()) + .message(authErrorCode.getMessage()) .build(); } } diff --git a/src/main/java/org/hankki/hankkiserver/auth/JwtAuthenticationEntryPoint.java b/src/main/java/org/hankki/hankkiserver/auth/JwtAuthenticationEntryPoint.java index 98a96daf..2cadfd16 100644 --- a/src/main/java/org/hankki/hankkiserver/auth/JwtAuthenticationEntryPoint.java +++ b/src/main/java/org/hankki/hankkiserver/auth/JwtAuthenticationEntryPoint.java @@ -4,7 +4,7 @@ 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.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(BaseResponse.of(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..dc01b886 100644 --- a/src/main/java/org/hankki/hankkiserver/auth/filter/ExceptionHandlerFilter.java +++ b/src/main/java/org/hankki/hankkiserver/auth/filter/ExceptionHandlerFilter.java @@ -5,7 +5,7 @@ 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.common.code.AuthErrorCode; import org.hankki.hankkiserver.common.exception.UnauthorizedException; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -36,25 +36,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); + AuthErrorCode authErrorCode = ue.getAuthErrorCode(); + 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 { + 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(BaseResponse.of(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..38d65e16 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.INVALID_ACCESS_TOKEN); } } 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..bd39219a --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/common/code/AuthErrorCode.java @@ -0,0 +1,48 @@ +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 AuthErrorCode implements 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 탈퇴에 실패했습니다."), + + // 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/CommonSuccessCode.java b/src/main/java/org/hankki/hankkiserver/common/code/CommonSuccessCode.java new file mode 100644 index 00000000..515b0e1a --- /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(access = AccessLevel.PRIVATE) +public enum CommonSuccessCode implements SuccessCode { + + OK(HttpStatus.OK, "요청이 성공했습니다."), + NO_CONTENT(HttpStatus.NO_CONTENT, "요청이 성공했습니다."), + 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/exception/BusinessException.java b/src/main/java/org/hankki/hankkiserver/common/exception/BusinessException.java index 085ad902..527cd362 100644 --- a/src/main/java/org/hankki/hankkiserver/common/exception/BusinessException.java +++ b/src/main/java/org/hankki/hankkiserver/common/exception/BusinessException.java @@ -1,14 +1,14 @@ package org.hankki.hankkiserver.common.exception; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import lombok.Getter; @Getter public class BusinessException extends RuntimeException { - private final ErrorCode errorCode; + private final AuthErrorCode authErrorCode; - public BusinessException(ErrorCode errorCode) { - super(errorCode.getMessage()); - this.errorCode = errorCode; + public BusinessException(AuthErrorCode authErrorCode) { + super(authErrorCode.getMessage()); + this.authErrorCode = authErrorCode; } } diff --git a/src/main/java/org/hankki/hankkiserver/common/exception/EntityNotFoundException.java b/src/main/java/org/hankki/hankkiserver/common/exception/EntityNotFoundException.java index 84833a59..a696b838 100644 --- a/src/main/java/org/hankki/hankkiserver/common/exception/EntityNotFoundException.java +++ b/src/main/java/org/hankki/hankkiserver/common/exception/EntityNotFoundException.java @@ -1,13 +1,13 @@ package org.hankki.hankkiserver.common.exception; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; public class EntityNotFoundException extends BusinessException { public EntityNotFoundException() { - super(ErrorCode.ENTITY_NOT_FOUND); + super(AuthErrorCode.ENTITY_NOT_FOUND); } - public EntityNotFoundException(ErrorCode errorCode) { - super(errorCode); + public EntityNotFoundException(AuthErrorCode authErrorCode) { + super(authErrorCode); } } diff --git a/src/main/java/org/hankki/hankkiserver/common/exception/InvalidValueException.java b/src/main/java/org/hankki/hankkiserver/common/exception/InvalidValueException.java index cf8ba4f1..80166b0a 100644 --- a/src/main/java/org/hankki/hankkiserver/common/exception/InvalidValueException.java +++ b/src/main/java/org/hankki/hankkiserver/common/exception/InvalidValueException.java @@ -1,14 +1,14 @@ package org.hankki.hankkiserver.common.exception; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; public class InvalidValueException extends BusinessException { public InvalidValueException() { - super(ErrorCode.BAD_REQUEST); + super(AuthErrorCode.BAD_REQUEST); } - public InvalidValueException(ErrorCode errorCode) { - super(errorCode); + public InvalidValueException(AuthErrorCode authErrorCode) { + super(authErrorCode); } } 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..eda5182b 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,14 @@ package org.hankki.hankkiserver.common.exception; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; public class UnauthorizedException extends BusinessException { public UnauthorizedException() { - super(ErrorCode.UNAUTHORIZED); + super(AuthErrorCode.UNAUTHORIZED); } - public UnauthorizedException(ErrorCode errorCode) { - super(errorCode); + public UnauthorizedException(AuthErrorCode authErrorCode) { + super(authErrorCode); } } 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..5fa03360 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,7 +2,7 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.hankki.hankkiserver.common.exception.InvalidValueException; 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 InvalidValueException(AuthErrorCode.INVALID_PLATFORM_TYPE)); } } 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..ccd15d49 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,7 +3,7 @@ import io.jsonwebtoken.Jwts; import io.jsonwebtoken.SignatureAlgorithm; import lombok.RequiredArgsConstructor; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.hankki.hankkiserver.common.exception.InvalidValueException; 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 InvalidValueException(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..38666840 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,7 +3,7 @@ import io.jsonwebtoken.Claims; import lombok.RequiredArgsConstructor; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.hankki.hankkiserver.common.exception.InvalidValueException; import org.hankki.hankkiserver.external.openfeign.apple.dto.ApplePublicKeys; import org.hankki.hankkiserver.external.openfeign.apple.dto.AppleTokenRequest; @@ -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 InvalidValueException(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..738f7542 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,6 +1,6 @@ package org.hankki.hankkiserver.external.openfeign.apple; -import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.hankki.hankkiserver.common.exception.UnauthorizedException; import org.hankki.hankkiserver.external.openfeign.apple.dto.ApplePublicKeys; import org.hankki.hankkiserver.external.openfeign.apple.dto.ApplePublicKey; @@ -38,11 +38,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 UnauthorizedException(AuthErrorCode.UNSUPPORTED_ALGORITHM); } catch (InvalidKeySpecException e) { - throw new UnauthorizedException(ErrorCode.INVALID_KEY_SPEC); + throw new UnauthorizedException(AuthErrorCode.INVALID_KEY_SPEC); } catch (Exception e) { - throw new UnauthorizedException(ErrorCode.INTERNAL_SERVER_ERROR); + throw new UnauthorizedException(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)); } } From 3aba0e42cb5fb1e1e5b87ef69049780cbf0e10e2 Mon Sep 17 00:00:00 2001 From: Parkjyun Date: Tue, 9 Jul 2024 09:14:27 +0900 Subject: [PATCH 2/9] [refac] refactor exception and common api response --- .../api/advice/GlobalExceptionHandler.java | 49 +++++++++++++++++++ .../api/auth/controller/AuthController.java | 10 ++-- .../api/auth/service/AuthService.java | 4 +- .../api/auth/service/UserFinder.java | 6 +-- .../api/auth/service/UserInfoFinder.java | 6 +-- .../hankkiserver/api/dto/ApiResponse.java | 12 ++--- ...{BaseResponse.java => HankkiResponse.java} | 14 +++--- .../auth/JwtAuthenticationEntryPoint.java | 4 +- .../auth/filter/ExceptionHandlerFilter.java | 9 ++-- .../common/code/AuthErrorCode.java | 28 +++++------ .../common/code/BusinessErrorCode.java | 15 ++++++ .../common/code/CommonSuccessCode.java | 8 +-- .../common/code/UserErrorCode.java | 16 ++++++ .../common/exception/BadRequestException.java | 14 ++++++ .../common/exception/BusinessException.java | 14 ------ .../exception/EntityNotFoundException.java | 13 ----- .../exception/InternalServerException.java | 12 +++++ .../exception/InvalidValueException.java | 14 ------ .../common/exception/NotFoundException.java | 15 ++++++ .../exception/UnauthorizedException.java | 15 +++--- .../domain/user/model/Platform.java | 4 +- .../apple/AppleClientSecretGenerator.java | 4 +- .../openfeign/apple/AppleOAuthProvider.java | 4 +- .../apple/ApplePublicKeyGenerator.java | 8 +-- 24 files changed, 190 insertions(+), 108 deletions(-) rename src/main/java/org/hankki/hankkiserver/api/dto/{BaseResponse.java => HankkiResponse.java} (68%) create mode 100644 src/main/java/org/hankki/hankkiserver/common/code/BusinessErrorCode.java create mode 100644 src/main/java/org/hankki/hankkiserver/common/code/UserErrorCode.java create mode 100644 src/main/java/org/hankki/hankkiserver/common/exception/BadRequestException.java delete mode 100644 src/main/java/org/hankki/hankkiserver/common/exception/BusinessException.java delete mode 100644 src/main/java/org/hankki/hankkiserver/common/exception/EntityNotFoundException.java create mode 100644 src/main/java/org/hankki/hankkiserver/common/exception/InternalServerException.java delete mode 100644 src/main/java/org/hankki/hankkiserver/common/exception/InvalidValueException.java create mode 100644 src/main/java/org/hankki/hankkiserver/common/exception/NotFoundException.java 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..32b1ab35 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,53 @@ package org.hankki.hankkiserver.api.advice; +import lombok.extern.slf4j.Slf4j; +import org.hankki.hankkiserver.common.code.BusinessErrorCode; +import org.hankki.hankkiserver.common.code.ErrorCode; +import org.hankki.hankkiserver.common.code.UserErrorCode; +import org.hankki.hankkiserver.common.exception.BadRequestException; +import org.hankki.hankkiserver.common.exception.NotFoundException; +import org.hankki.hankkiserver.common.exception.UnauthorizedException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.servlet.NoHandlerFoundException; + +@RestControllerAdvice +@Slf4j public class GlobalExceptionHandler { + + @ExceptionHandler(BadRequestException.class) + public ResponseEntity handleBadRequestException(BadRequestException e) { + log.error("handleBadRequestException() in GlobalExceptionHandler throw BadRequestException : {}", e.getMessage()); + return ResponseEntity + .status(e.getErrorCode().getHttpStatus()) + .body(e.getErrorCode()); + } + + @ExceptionHandler(UnauthorizedException.class) + public ResponseEntity handleUnauthorizedException(UnauthorizedException e) { + log.error("handleUnauthorizedException() in GlobalExceptionHandler throw UnauthorizedException : {}", e.getMessage()); + return ResponseEntity + .status(e.getErrorCode().getHttpStatus()) + .body(e.getErrorCode()); + } + + @ExceptionHandler(NotFoundException.class) + public ResponseEntity handleEntityNotFoundException(NotFoundException e) { + log.error("handleEntityNotFoundException() in GlobalExceptionHandler throw EntityNotFoundException : {}", e.getMessage()); + return ResponseEntity + .status(e.getErrorCode().getHttpStatus()) + .body(e.getErrorCode()); + } + + @ExceptionHandler(Exception.class) + public ResponseEntity handleException(Exception e) { + log.error("handleException() in GlobalExceptionHandler throw Exception : {}", e.getMessage()); + return ResponseEntity + .status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(BusinessErrorCode.INTERNAL_SERVER_ERROR); + + } } 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 40032bdf..c3930d26 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 @@ -4,7 +4,7 @@ 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.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; @@ -22,7 +22,7 @@ public class AuthController { private final AuthService authService; @PostMapping("/auth/login") - public ResponseEntity> login( + public ResponseEntity> login( @RequestHeader(HttpHeaders.AUTHORIZATION) final String token, @RequestBody final UserLoginRequest request) { final UserLoginResponse response = authService.login(token, request); @@ -30,14 +30,14 @@ public ResponseEntity> login( } @PatchMapping("/auth/logout") - public ResponseEntity> signOut( + public ResponseEntity> signOut( @UserId final Long userId) { authService.logOut(userId); return ApiResponse.success(CommonSuccessCode.OK); } @DeleteMapping("/auth/withdraw") - public ResponseEntity> withdraw( + public ResponseEntity> withdraw( @UserId final Long userId, @Nullable @RequestHeader("X-Apple-Code") final String code){ authService.withdraw(userId,code); @@ -45,7 +45,7 @@ public ResponseEntity> withdraw( } @PostMapping("/auth/reissue") - public ResponseEntity> reissue( + public ResponseEntity> reissue( @RequestHeader(HttpHeaders.AUTHORIZATION) final String refreshToken) { final UserReissueResponse response = authService.reissue(refreshToken); return ApiResponse.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 c211d28b..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 @@ -8,7 +8,7 @@ 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; @@ -63,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(AuthErrorCode.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 c3f1b231..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.AuthErrorCode; -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(AuthErrorCode.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 e0771824..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.AuthErrorCode; -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(AuthErrorCode.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 index 4e72f6c0..8aba8c35 100644 --- a/src/main/java/org/hankki/hankkiserver/api/dto/ApiResponse.java +++ b/src/main/java/org/hankki/hankkiserver/api/dto/ApiResponse.java @@ -6,18 +6,18 @@ public interface ApiResponse { - static ResponseEntity> success(CommonSuccessCode commonSuccessCode) { + static ResponseEntity> success(CommonSuccessCode commonSuccessCode) { return ResponseEntity.status(commonSuccessCode.getHttpStatus()) - .body(BaseResponse.of(commonSuccessCode)); + .body(HankkiResponse.of(commonSuccessCode)); } - static ResponseEntity> success(CommonSuccessCode commonSuccessCode, T data) { + static ResponseEntity> success(CommonSuccessCode commonSuccessCode, T data) { return org.springframework.http.ResponseEntity.status(commonSuccessCode.getHttpStatus()) - .body(BaseResponse.of(commonSuccessCode, data)); + .body(HankkiResponse.of(commonSuccessCode, data)); } - static ResponseEntity> failure(AuthErrorCode authErrorCode) { + static ResponseEntity> failure(AuthErrorCode authErrorCode) { return ResponseEntity.status(authErrorCode.getHttpStatus()) - .body(BaseResponse.of(authErrorCode)); + .body(HankkiResponse.of(authErrorCode)); } } diff --git a/src/main/java/org/hankki/hankkiserver/api/dto/BaseResponse.java b/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java similarity index 68% rename from src/main/java/org/hankki/hankkiserver/api/dto/BaseResponse.java rename to src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java index c32f831c..fd09f0e5 100644 --- a/src/main/java/org/hankki/hankkiserver/api/dto/BaseResponse.java +++ b/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java @@ -1,31 +1,31 @@ package org.hankki.hankkiserver.api.dto; import com.fasterxml.jackson.annotation.JsonInclude; -import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.hankki.hankkiserver.common.code.CommonSuccessCode; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Getter; +import org.hankki.hankkiserver.common.code.ErrorCode; @Getter @Builder(access = AccessLevel.PRIVATE) @AllArgsConstructor(access = AccessLevel.PRIVATE) -public class BaseResponse { +public class HankkiResponse { private final int status; private final String message; @JsonInclude(value = JsonInclude.Include.NON_NULL) private final T data; - public static BaseResponse of(CommonSuccessCode commonSuccessCode) { + public static HankkiResponse of(CommonSuccessCode commonSuccessCode) { return builder() .status(commonSuccessCode.getHttpStatus().value()) .message(commonSuccessCode.getMessage()) .build(); } - public static BaseResponse of(CommonSuccessCode commonSuccessCode, T data) { + public static HankkiResponse of(CommonSuccessCode commonSuccessCode, T data) { return builder() .status(commonSuccessCode.getHttpStatus().value()) .message(commonSuccessCode.getMessage()) @@ -33,10 +33,10 @@ public static BaseResponse of(CommonSuccessCode commonSuccessCode, T data .build(); } - public static BaseResponse of(AuthErrorCode authErrorCode) { + public static HankkiResponse of(ErrorCode errorCode) { return builder() - .status(authErrorCode.getHttpStatus().value()) - .message(authErrorCode.getMessage()) + .status(errorCode.getHttpStatus().value()) + .message(errorCode.getMessage()) .build(); } } diff --git a/src/main/java/org/hankki/hankkiserver/auth/JwtAuthenticationEntryPoint.java b/src/main/java/org/hankki/hankkiserver/auth/JwtAuthenticationEntryPoint.java index 2cadfd16..88eba918 100644 --- a/src/main/java/org/hankki/hankkiserver/auth/JwtAuthenticationEntryPoint.java +++ b/src/main/java/org/hankki/hankkiserver/auth/JwtAuthenticationEntryPoint.java @@ -3,7 +3,7 @@ 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.api.dto.HankkiResponse; import org.hankki.hankkiserver.common.code.AuthErrorCode; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -39,6 +39,6 @@ private void setResponse( response.setCharacterEncoding("utf-8"); response.setStatus(httpStatus.value()); PrintWriter writer = response.getWriter(); - writer.write(objectMapper.writeValueAsString(BaseResponse.of(authErrorCode))); + writer.write(objectMapper.writeValueAsString(HankkiResponse.of(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 dc01b886..7b86f23e 100644 --- a/src/main/java/org/hankki/hankkiserver/auth/filter/ExceptionHandlerFilter.java +++ b/src/main/java/org/hankki/hankkiserver/auth/filter/ExceptionHandlerFilter.java @@ -4,8 +4,9 @@ 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; import org.springframework.http.MediaType; @@ -36,7 +37,7 @@ private void handleUnauthorizedException( HttpServletResponse response, Exception e) throws IOException { UnauthorizedException ue = (UnauthorizedException) e; - AuthErrorCode authErrorCode = ue.getAuthErrorCode(); + ErrorCode authErrorCode = ue.getErrorCode(); HttpStatus httpStatus = authErrorCode.getHttpStatus(); setResponse(response, httpStatus, authErrorCode); } @@ -50,11 +51,11 @@ private void handleException( private void setResponse( HttpServletResponse response, HttpStatus httpStatus, - AuthErrorCode authErrorCode) 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(authErrorCode))); + writer.write(objectMapper.writeValueAsString(HankkiResponse.of(authErrorCode))); } } diff --git a/src/main/java/org/hankki/hankkiserver/common/code/AuthErrorCode.java b/src/main/java/org/hankki/hankkiserver/common/code/AuthErrorCode.java index bd39219a..8de85b7e 100644 --- a/src/main/java/org/hankki/hankkiserver/common/code/AuthErrorCode.java +++ b/src/main/java/org/hankki/hankkiserver/common/code/AuthErrorCode.java @@ -6,43 +6,39 @@ import org.springframework.http.HttpStatus; @Getter -@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +@RequiredArgsConstructor public enum AuthErrorCode implements ErrorCode { - // reuqest error - BAD_REQUEST(HttpStatus.BAD_REQUEST, "잘못된 요청입니다."), + /** + * 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 탈퇴에 실패했습니다."), - // unauthorized error + /** + * 401 Unauthorized + **/ 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, "리프레쉬 토큰을 찾을 수 없습니다."), + /** + * 500 Internal Server Error + **/ + INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "서버 오류입니다."); - // 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/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 index 515b0e1a..852da4d5 100644 --- a/src/main/java/org/hankki/hankkiserver/common/code/CommonSuccessCode.java +++ b/src/main/java/org/hankki/hankkiserver/common/code/CommonSuccessCode.java @@ -6,12 +6,12 @@ import org.springframework.http.HttpStatus; @Getter -@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +@RequiredArgsConstructor public enum CommonSuccessCode implements SuccessCode { - OK(HttpStatus.OK, "요청이 성공했습니다."), - NO_CONTENT(HttpStatus.NO_CONTENT, "요청이 성공했습니다."), - CREATED(HttpStatus.CREATED, "요청이 성공했습니다."); + 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/UserErrorCode.java b/src/main/java/org/hankki/hankkiserver/common/code/UserErrorCode.java new file mode 100644 index 00000000..697720dd --- /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/BusinessException.java b/src/main/java/org/hankki/hankkiserver/common/exception/BusinessException.java deleted file mode 100644 index 527cd362..00000000 --- a/src/main/java/org/hankki/hankkiserver/common/exception/BusinessException.java +++ /dev/null @@ -1,14 +0,0 @@ -package org.hankki.hankkiserver.common.exception; - -import org.hankki.hankkiserver.common.code.AuthErrorCode; -import lombok.Getter; - -@Getter -public class BusinessException extends RuntimeException { - private final AuthErrorCode authErrorCode; - - public BusinessException(AuthErrorCode authErrorCode) { - super(authErrorCode.getMessage()); - this.authErrorCode = authErrorCode; - } -} 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 a696b838..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.AuthErrorCode; - -public class EntityNotFoundException extends BusinessException { - public EntityNotFoundException() { - super(AuthErrorCode.ENTITY_NOT_FOUND); - } - - public EntityNotFoundException(AuthErrorCode authErrorCode) { - super(authErrorCode); - } -} 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 80166b0a..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.AuthErrorCode; - -public class InvalidValueException extends BusinessException { - public InvalidValueException() { - super(AuthErrorCode.BAD_REQUEST); - } - - public InvalidValueException(AuthErrorCode authErrorCode) { - super(authErrorCode); - } -} - diff --git a/src/main/java/org/hankki/hankkiserver/common/exception/NotFoundException.java b/src/main/java/org/hankki/hankkiserver/common/exception/NotFoundException.java new file mode 100644 index 00000000..a6db85a7 --- /dev/null +++ b/src/main/java/org/hankki/hankkiserver/common/exception/NotFoundException.java @@ -0,0 +1,15 @@ +package org.hankki.hankkiserver.common.exception; + +import lombok.Getter; +import org.hankki.hankkiserver.common.code.ErrorCode; + +@Getter +public class NotFoundException extends RuntimeException { + + private final 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 eda5182b..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(AuthErrorCode.UNAUTHORIZED); - } +@Getter +public class UnauthorizedException extends RuntimeException { + private final ErrorCode errorCode; - public UnauthorizedException(AuthErrorCode authErrorCode) { - super(authErrorCode); + public UnauthorizedException(ErrorCode errorCode) { + super(errorCode.getMessage()); + this.errorCode = errorCode; } + } 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 5fa03360..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 @@ -3,7 +3,7 @@ import lombok.Getter; import lombok.RequiredArgsConstructor; import org.hankki.hankkiserver.common.code.AuthErrorCode; -import org.hankki.hankkiserver.common.exception.InvalidValueException; +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(AuthErrorCode.INVALID_PLATFORM_TYPE)); + .orElseThrow(() -> new BadRequestException(AuthErrorCode.INVALID_PLATFORM_TYPE)); } } 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 ccd15d49..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 @@ -4,7 +4,7 @@ import io.jsonwebtoken.SignatureAlgorithm; import lombok.RequiredArgsConstructor; import org.hankki.hankkiserver.common.code.AuthErrorCode; -import org.hankki.hankkiserver.common.exception.InvalidValueException; +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(AuthErrorCode.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/AppleOAuthProvider.java b/src/main/java/org/hankki/hankkiserver/external/openfeign/apple/AppleOAuthProvider.java index 38666840..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 @@ -4,7 +4,7 @@ import lombok.RequiredArgsConstructor; import org.hankki.hankkiserver.common.code.AuthErrorCode; -import org.hankki.hankkiserver.common.exception.InvalidValueException; +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(AuthErrorCode.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 738f7542..ed82dff6 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,6 +1,8 @@ package org.hankki.hankkiserver.external.openfeign.apple; 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.common.exception.UnauthorizedException; import org.hankki.hankkiserver.external.openfeign.apple.dto.ApplePublicKeys; import org.hankki.hankkiserver.external.openfeign.apple.dto.ApplePublicKey; @@ -38,11 +40,11 @@ public PublicKey generatePublicKey( KeyFactory keyFactory = KeyFactory.getInstance(applePublicKey.kty()); return keyFactory.generatePublic(rsaPublicKeySpec); } catch (NoSuchAlgorithmException e) { - throw new UnauthorizedException(AuthErrorCode.UNSUPPORTED_ALGORITHM); + throw new BadRequestException(AuthErrorCode.UNSUPPORTED_ALGORITHM); } catch (InvalidKeySpecException e) { - throw new UnauthorizedException(AuthErrorCode.INVALID_KEY_SPEC); + throw new BadRequestException(AuthErrorCode.INVALID_KEY_SPEC); } catch (Exception e) { - throw new UnauthorizedException(AuthErrorCode.INTERNAL_SERVER_ERROR); + throw new InternalServerException(AuthErrorCode.INTERNAL_SERVER_ERROR); } } } From 6904850678d56eb791cb1b8918eb694052f7dd12 Mon Sep 17 00:00:00 2001 From: Parkjyun Date: Tue, 9 Jul 2024 11:16:30 +0900 Subject: [PATCH 3/9] [feat] create response advice --- .../api/advice/ResponseAdvice.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 src/main/java/org/hankki/hankkiserver/api/advice/ResponseAdvice.java 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..fb6e2e1c --- /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).getStatus()); + response.setStatusCode(status); + } + return body; + } + +} From 0298f2d2eb8eb8ee85f10eeed691e8462d5c1195 Mon Sep 17 00:00:00 2001 From: Parkjyun Date: Tue, 9 Jul 2024 11:16:58 +0900 Subject: [PATCH 4/9] [refac] fix typo in UserErrorCode.java --- .../java/org/hankki/hankkiserver/common/code/UserErrorCode.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/hankki/hankkiserver/common/code/UserErrorCode.java b/src/main/java/org/hankki/hankkiserver/common/code/UserErrorCode.java index 697720dd..05b14ec7 100644 --- a/src/main/java/org/hankki/hankkiserver/common/code/UserErrorCode.java +++ b/src/main/java/org/hankki/hankkiserver/common/code/UserErrorCode.java @@ -9,7 +9,7 @@ public enum UserErrorCode implements ErrorCode { USER_NOT_FOUND(HttpStatus.NOT_FOUND, "등록되지 않은 회원입니다."), - USER_INFO_NOT_FOUND(HttpStatus.NOT_FOUND, "회원 정보를 찾을 수 없습니다."),; + USER_INFO_NOT_FOUND(HttpStatus.NOT_FOUND, "회원 정보를 찾을 수 없습니다."); private final HttpStatus httpStatus; private final String message; From f6aa5d734f45ac5f8fcda674ae896d7f45f7e015 Mon Sep 17 00:00:00 2001 From: Parkjyun Date: Tue, 9 Jul 2024 11:19:27 +0900 Subject: [PATCH 5/9] [refac] change Hankki response creation logic --- .../api/advice/GlobalExceptionHandler.java | 32 ++++++------------- .../api/auth/controller/AuthController.java | 18 +++++------ .../hankkiserver/api/dto/ApiResponse.java | 23 ------------- .../hankkiserver/api/dto/HankkiResponse.java | 32 ++++++------------- .../auth/JwtAuthenticationEntryPoint.java | 2 +- .../auth/filter/ExceptionHandlerFilter.java | 2 +- .../apple/ApplePublicKeyGenerator.java | 1 - 7 files changed, 30 insertions(+), 80 deletions(-) delete mode 100644 src/main/java/org/hankki/hankkiserver/api/dto/ApiResponse.java 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 32b1ab35..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,53 +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.code.ErrorCode; -import org.hankki.hankkiserver.common.code.UserErrorCode; import org.hankki.hankkiserver.common.exception.BadRequestException; import org.hankki.hankkiserver.common.exception.NotFoundException; import org.hankki.hankkiserver.common.exception.UnauthorizedException; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.HttpRequestMethodNotSupportedException; + import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.RestControllerAdvice; -import org.springframework.web.servlet.NoHandlerFoundException; @RestControllerAdvice @Slf4j public class GlobalExceptionHandler { @ExceptionHandler(BadRequestException.class) - public ResponseEntity handleBadRequestException(BadRequestException e) { + public HankkiResponse handleBadRequestException(BadRequestException e) { log.error("handleBadRequestException() in GlobalExceptionHandler throw BadRequestException : {}", e.getMessage()); - return ResponseEntity - .status(e.getErrorCode().getHttpStatus()) - .body(e.getErrorCode()); + return HankkiResponse.fail(e.getErrorCode()); } @ExceptionHandler(UnauthorizedException.class) - public ResponseEntity handleUnauthorizedException(UnauthorizedException e) { + public HankkiResponse handleUnauthorizedException(UnauthorizedException e) { log.error("handleUnauthorizedException() in GlobalExceptionHandler throw UnauthorizedException : {}", e.getMessage()); - return ResponseEntity - .status(e.getErrorCode().getHttpStatus()) - .body(e.getErrorCode()); + return HankkiResponse.fail(e.getErrorCode()); } @ExceptionHandler(NotFoundException.class) - public ResponseEntity handleEntityNotFoundException(NotFoundException e) { + public HankkiResponse handleEntityNotFoundException(NotFoundException e) { log.error("handleEntityNotFoundException() in GlobalExceptionHandler throw EntityNotFoundException : {}", e.getMessage()); - return ResponseEntity - .status(e.getErrorCode().getHttpStatus()) - .body(e.getErrorCode()); + return HankkiResponse.fail(e.getErrorCode()); } @ExceptionHandler(Exception.class) - public ResponseEntity handleException(Exception e) { + public HankkiResponse handleException(Exception e) { log.error("handleException() in GlobalExceptionHandler throw Exception : {}", e.getMessage()); - return ResponseEntity - .status(HttpStatus.INTERNAL_SERVER_ERROR) - .body(BusinessErrorCode.INTERNAL_SERVER_ERROR); + return HankkiResponse.fail(BusinessErrorCode.INTERNAL_SERVER_ERROR); } } 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 c3930d26..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,7 +3,6 @@ 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.HankkiResponse; import org.hankki.hankkiserver.common.code.CommonSuccessCode; import org.hankki.hankkiserver.api.auth.service.AuthService; @@ -11,7 +10,6 @@ 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(CommonSuccessCode.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(CommonSuccessCode.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(CommonSuccessCode.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(CommonSuccessCode.OK, response); + return HankkiResponse.success(CommonSuccessCode.OK, response); } } 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 8aba8c35..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.AuthErrorCode; -import org.hankki.hankkiserver.common.code.CommonSuccessCode; -import org.springframework.http.ResponseEntity; - -public interface ApiResponse { - - static ResponseEntity> success(CommonSuccessCode commonSuccessCode) { - return ResponseEntity.status(commonSuccessCode.getHttpStatus()) - .body(HankkiResponse.of(commonSuccessCode)); - } - - static ResponseEntity> success(CommonSuccessCode commonSuccessCode, T data) { - return org.springframework.http.ResponseEntity.status(commonSuccessCode.getHttpStatus()) - .body(HankkiResponse.of(commonSuccessCode, data)); - } - - static ResponseEntity> failure(AuthErrorCode authErrorCode) { - return ResponseEntity.status(authErrorCode.getHttpStatus()) - .body(HankkiResponse.of(authErrorCode)); - } -} diff --git a/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java b/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java index fd09f0e5..e1ffd602 100644 --- a/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java +++ b/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java @@ -1,42 +1,30 @@ package org.hankki.hankkiserver.api.dto; import com.fasterxml.jackson.annotation.JsonInclude; -import org.hankki.hankkiserver.common.code.CommonSuccessCode; -import lombok.AccessLevel; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.Getter; +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 status; private final String message; @JsonInclude(value = JsonInclude.Include.NON_NULL) - private final T data; + private T data; - public static HankkiResponse of(CommonSuccessCode commonSuccessCode) { - return builder() - .status(commonSuccessCode.getHttpStatus().value()) - .message(commonSuccessCode.getMessage()) - .build(); + public static HankkiResponse success(SuccessCode success) { + return new HankkiResponse<>(success.getHttpStatus().value(), success.getMessage()); } - public static HankkiResponse of(CommonSuccessCode commonSuccessCode, T data) { - return builder() - .status(commonSuccessCode.getHttpStatus().value()) - .message(commonSuccessCode.getMessage()) - .data(data) - .build(); + public static HankkiResponse success(SuccessCode success, T data) { + return new HankkiResponse<>(success.getHttpStatus().value(), success.getMessage(), data); } - public static HankkiResponse of(ErrorCode errorCode) { - return builder() - .status(errorCode.getHttpStatus().value()) - .message(errorCode.getMessage()) - .build(); + 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 88eba918..2460af36 100644 --- a/src/main/java/org/hankki/hankkiserver/auth/JwtAuthenticationEntryPoint.java +++ b/src/main/java/org/hankki/hankkiserver/auth/JwtAuthenticationEntryPoint.java @@ -39,6 +39,6 @@ private void setResponse( response.setCharacterEncoding("utf-8"); response.setStatus(httpStatus.value()); PrintWriter writer = response.getWriter(); - writer.write(objectMapper.writeValueAsString(HankkiResponse.of(authErrorCode))); + 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 7b86f23e..817f93e3 100644 --- a/src/main/java/org/hankki/hankkiserver/auth/filter/ExceptionHandlerFilter.java +++ b/src/main/java/org/hankki/hankkiserver/auth/filter/ExceptionHandlerFilter.java @@ -56,6 +56,6 @@ private void setResponse( response.setCharacterEncoding("utf-8"); response.setStatus(httpStatus.value()); PrintWriter writer = response.getWriter(); - writer.write(objectMapper.writeValueAsString(HankkiResponse.of(authErrorCode))); + writer.write(objectMapper.writeValueAsString(HankkiResponse.fail(authErrorCode))); } } 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 ed82dff6..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 @@ -3,7 +3,6 @@ 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.common.exception.UnauthorizedException; import org.hankki.hankkiserver.external.openfeign.apple.dto.ApplePublicKeys; import org.hankki.hankkiserver.external.openfeign.apple.dto.ApplePublicKey; import org.springframework.stereotype.Component; From fb965dab010f6e3d13cb18b5c25fa626eac324bf Mon Sep 17 00:00:00 2001 From: Parkjyun Date: Tue, 9 Jul 2024 21:33:58 +0900 Subject: [PATCH 6/9] [refac] delete duplicated BaseTimeEntity.java --- .../hankkiserver/domain/BaseTimeEntity.java | 21 ------------------- .../hankkiserver/domain/user/model/User.java | 2 +- 2 files changed, 1 insertion(+), 22 deletions(-) delete mode 100644 src/main/java/org/hankki/hankkiserver/domain/BaseTimeEntity.java 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/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; From 9051f2554e03f4fa3bbe3907eb56d5388d118203 Mon Sep 17 00:00:00 2001 From: Parkjyun Date: Tue, 9 Jul 2024 21:38:23 +0900 Subject: [PATCH 7/9] [refac] fix HankkiResponse.java as api spec --- .../java/org/hankki/hankkiserver/api/dto/HankkiResponse.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java b/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java index e1ffd602..4e2d6b00 100644 --- a/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java +++ b/src/main/java/org/hankki/hankkiserver/api/dto/HankkiResponse.java @@ -11,7 +11,7 @@ @RequiredArgsConstructor(access = AccessLevel.PRIVATE) public class HankkiResponse { - private final int status; + private final int code; private final String message; @JsonInclude(value = JsonInclude.Include.NON_NULL) private T data; From ae1759a4e1e137df53bebc29b324d1f226800cba Mon Sep 17 00:00:00 2001 From: Parkjyun Date: Tue, 9 Jul 2024 21:48:56 +0900 Subject: [PATCH 8/9] [refac] fix unappropriate error message while validating refreshtoken --- .../java/org/hankki/hankkiserver/auth/jwt/JwtValidator.java | 2 +- .../java/org/hankki/hankkiserver/common/code/AuthErrorCode.java | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) 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 38d65e16..62048988 100644 --- a/src/main/java/org/hankki/hankkiserver/auth/jwt/JwtValidator.java +++ b/src/main/java/org/hankki/hankkiserver/auth/jwt/JwtValidator.java @@ -52,6 +52,6 @@ private String getToken(final String refreshToken) { if (refreshToken.startsWith(BEARER)) { return refreshToken.substring(BEARER.length()); } - throw new UnauthorizedException(AuthErrorCode.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 index 8de85b7e..7d0b6b06 100644 --- a/src/main/java/org/hankki/hankkiserver/common/code/AuthErrorCode.java +++ b/src/main/java/org/hankki/hankkiserver/common/code/AuthErrorCode.java @@ -31,6 +31,7 @@ public enum AuthErrorCode implements ErrorCode { 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 From effce80a8b34fae2868e5248cd7caa3cf1289fa8 Mon Sep 17 00:00:00 2001 From: Parkjyun Date: Tue, 9 Jul 2024 22:22:41 +0900 Subject: [PATCH 9/9] [refac] change typo in ResponseAdvice.java --- .../java/org/hankki/hankkiserver/api/advice/ResponseAdvice.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/hankki/hankkiserver/api/advice/ResponseAdvice.java b/src/main/java/org/hankki/hankkiserver/api/advice/ResponseAdvice.java index fb6e2e1c..517d6e41 100644 --- a/src/main/java/org/hankki/hankkiserver/api/advice/ResponseAdvice.java +++ b/src/main/java/org/hankki/hankkiserver/api/advice/ResponseAdvice.java @@ -20,7 +20,7 @@ public boolean supports(MethodParameter returnType, Class converterType) { @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).getStatus()); + HttpStatus status = HttpStatus.valueOf(((HankkiResponse) body).getCode()); response.setStatusCode(status); } return body;