Skip to content

Commit

Permalink
[refac] 패키지 구조 변경 (#20)
Browse files Browse the repository at this point in the history
* [refac] move AuthController.java to another package

* [refac] move SuccessMessage.java, ErrorMessage.java

* [refac] change name of SuccessMessage.java, ErrorMessage.java

* [refac] move AuthController.java, AuthService.java

* [refac] move dtos related with auth

* [refac] move exceptions

* [refac] move GlobalExceptionHandler.java

* [refac] move ApiResponse.java, BaseResponse.java

* [refac] move SuccessCode.java and ErrorCode.java

* [refac] move package oauth to external/openfeign

* [refac] move JwtAuthenticationEntryPoint.java

* [refac] move GlobalExceptionHandler.java

* [refac] move SuccessCode.java, ErrorCode.java

* [refac] move ApiResponse.java, BaseResponse.java
  • Loading branch information
Parkjyun authored Jul 8, 2024
1 parent 3510baa commit cbcb625
Show file tree
Hide file tree
Showing 45 changed files with 242 additions and 242 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.hankki.hankkiserver.api.advice;

public class GlobalExceptionHandler {
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package org.hankki.hankkiserver.controller;
package org.hankki.hankkiserver.api.auth.controller;

import jakarta.annotation.Nullable;
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.auth.UserId;
import org.hankki.hankkiserver.common.ApiResponse;
import org.hankki.hankkiserver.common.BaseResponse;
import org.hankki.hankkiserver.common.dto.SuccessMessage;
import org.hankki.hankkiserver.service.AuthService;
import org.hankki.hankkiserver.service.dto.request.UserLoginRequest;
import org.hankki.hankkiserver.service.dto.response.UserLoginResponse;
import org.hankki.hankkiserver.service.dto.response.UserReissueResponse;
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.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.*;
Expand All @@ -26,28 +26,28 @@ public ResponseEntity<BaseResponse<?>> login(
@RequestHeader(HttpHeaders.AUTHORIZATION) final String token,
@RequestBody final UserLoginRequest request) {
final UserLoginResponse response = authService.login(token, request);
return ApiResponse.success(SuccessMessage.OK, response);
return ApiResponse.success(SuccessCode.OK, response);
}

@PatchMapping("/auth/logout")
public ResponseEntity<BaseResponse<?>> signOut(
@UserId final Long userId) {
authService.logOut(userId);
return ApiResponse.success(SuccessMessage.OK);
return ApiResponse.success(SuccessCode.OK);
}

@DeleteMapping("/auth/withdraw")
public ResponseEntity<BaseResponse<?>> withdraw(
@UserId final Long userId,
@Nullable @RequestHeader("X-Apple-Code") final String code){
authService.withdraw(userId,code);
return ApiResponse.success(SuccessMessage.NO_CONTENT);
return ApiResponse.success(SuccessCode.NO_CONTENT);
}

@PostMapping("/auth/reissue")
public ResponseEntity<BaseResponse<?>> reissue(
@RequestHeader(HttpHeaders.AUTHORIZATION) final String refreshtoken) {
final UserReissueResponse response = authService.reissue(refreshtoken);
return ApiResponse.success(SuccessMessage.OK, response);
return ApiResponse.success(SuccessCode.OK, response);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.hankki.hankkiserver.service.dto.request;
package org.hankki.hankkiserver.api.auth.controller.request;

import jakarta.annotation.Nullable;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
package org.hankki.hankkiserver.service;
package org.hankki.hankkiserver.api.auth.service;

import lombok.RequiredArgsConstructor;
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.dto.ErrorMessage;
import org.hankki.hankkiserver.common.code.ErrorCode;
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.exception.EntityNotFoundException;
import org.hankki.hankkiserver.exception.InvalidValueException;
import org.hankki.hankkiserver.exception.UnauthorizedException;
import org.hankki.hankkiserver.oauth.apple.AppleOAuthProvider;
import org.hankki.hankkiserver.oauth.dto.SocialInfoDto;
import org.hankki.hankkiserver.oauth.kakao.KakaoOAuthProvider;
import org.hankki.hankkiserver.common.exception.EntityNotFoundException;
import org.hankki.hankkiserver.common.exception.InvalidValueException;
import org.hankki.hankkiserver.common.exception.UnauthorizedException;
import org.hankki.hankkiserver.external.openfeign.apple.AppleOAuthProvider;
import org.hankki.hankkiserver.external.openfeign.dto.SocialInfoDto;
import org.hankki.hankkiserver.domain.user.repository.UserInfoRepository;
import org.hankki.hankkiserver.domain.user.repository.UserRepository;
import org.hankki.hankkiserver.service.dto.request.UserLoginRequest;
import org.hankki.hankkiserver.service.dto.response.UserLoginResponse;
import org.hankki.hankkiserver.service.dto.response.UserReissueResponse;
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.hankki.hankkiserver.external.openfeign.kakao.KakaoOAuthProvider;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -64,7 +64,7 @@ public void withdraw(final Long userId, final String code) {
String refreshToken = appleOAuthProvider.getAppleToken(code);
appleOAuthProvider.requestRevoke(refreshToken);
} catch (Exception e) {
throw new InvalidValueException(ErrorMessage.APPLE_REVOKE_FAILED);
throw new InvalidValueException(ErrorCode.APPLE_REVOKE_FAILED);
}
}

Expand Down Expand Up @@ -99,7 +99,7 @@ private SocialInfoDto getSocialInfo(
} else if (APPLE == platform){
return appleOAuthProvider.getAppleUserInfo(providerToken, name);
}
throw new EntityNotFoundException(ErrorMessage.INVALID_PLATFORM_TYPE);
throw new EntityNotFoundException(ErrorCode.INVALID_PLATFORM_TYPE);
}

private boolean isRegisteredUser(Platform platform, SocialInfoDto socialInfo){
Expand Down Expand Up @@ -132,22 +132,22 @@ private User getUser(
final Platform platform,
final String serialId) {
return userRepository.findByPlatformAndSerialId(platform, serialId)
.orElseThrow(() -> new EntityNotFoundException(ErrorMessage.USER_NOT_FOUND));
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND));
}

private User getUser(final Long userId) {
return userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException(ErrorMessage.USER_NOT_FOUND));
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_NOT_FOUND));
}

private UserInfo getUserInfo(final Long memberId) {
return userInfoRepository.findByUserId(memberId)
.orElseThrow(() -> new EntityNotFoundException(ErrorMessage.USER_INFO_NOT_FOUND));
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.USER_INFO_NOT_FOUND));
}

private String getRefreshToken(final Long userId) {
return userInfoRepository.findByUserId(userId)
.orElseThrow(() -> new EntityNotFoundException(ErrorMessage.REFRESH_TOKEN_NOT_FOUND))
.orElseThrow(() -> new EntityNotFoundException(ErrorCode.REFRESH_TOKEN_NOT_FOUND))
.getRefreshToken();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.hankki.hankkiserver.service.dto.response;
package org.hankki.hankkiserver.api.auth.service.response;

import lombok.AccessLevel;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package org.hankki.hankkiserver.service.dto.response;
package org.hankki.hankkiserver.api.auth.service.response;

import lombok.AccessLevel;
import lombok.Builder;
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/org/hankki/hankkiserver/api/dto/ApiResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
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<BaseResponse<?>> success(SuccessCode successCode) {
return ResponseEntity.status(successCode.getHttpStatus())
.body(BaseResponse.of(successCode));
}

static <T> ResponseEntity<BaseResponse<?>> success(SuccessCode successCode, T data) {
return org.springframework.http.ResponseEntity.status(successCode.getHttpStatus())
.body(BaseResponse.of(successCode, data));
}

static ResponseEntity<BaseResponse<?>> failure(ErrorCode errorCode) {
return ResponseEntity.status(errorCode.getHttpStatus())
.body(BaseResponse.of(errorCode));
}
}
42 changes: 42 additions & 0 deletions src/main/java/org/hankki/hankkiserver/api/dto/BaseResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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<T> {

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 <T> 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();
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.hankki.hankkiserver.auth.jwt;
package org.hankki.hankkiserver.auth;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.hankki.hankkiserver.common.BaseResponse;
import org.hankki.hankkiserver.common.dto.ErrorMessage;
import org.hankki.hankkiserver.api.dto.BaseResponse;
import org.hankki.hankkiserver.common.code.ErrorCode;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.core.AuthenticationException;
Expand All @@ -28,17 +28,17 @@ public void commence(
}

private void handleException(HttpServletResponse response) throws IOException {
setResponse(response, HttpStatus.UNAUTHORIZED, ErrorMessage.UNAUTHORIZED);
setResponse(response, HttpStatus.UNAUTHORIZED, ErrorCode.UNAUTHORIZED);
}

private void setResponse(
HttpServletResponse response,
HttpStatus httpStatus,
ErrorMessage errorMessage) throws IOException {
ErrorCode errorCode) 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(errorMessage)));
writer.write(objectMapper.writeValueAsString(BaseResponse.of(errorCode)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.auth.filter.ExceptionHandlerFilter;
import org.hankki.hankkiserver.auth.filter.JwtAuthenticationFilter;
import org.hankki.hankkiserver.auth.jwt.JwtAuthenticationEntryPoint;
import org.hankki.hankkiserver.auth.JwtAuthenticationEntryPoint;
import org.hankki.hankkiserver.auth.jwt.JwtProvider;
import org.hankki.hankkiserver.auth.jwt.JwtValidator;
import org.springframework.context.annotation.Bean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import jakarta.servlet.FilterChain;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.hankki.hankkiserver.common.BaseResponse;
import org.hankki.hankkiserver.common.dto.ErrorMessage;
import org.hankki.hankkiserver.exception.UnauthorizedException;
import org.hankki.hankkiserver.api.dto.BaseResponse;
import org.hankki.hankkiserver.common.code.ErrorCode;
import org.hankki.hankkiserver.common.exception.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.filter.OncePerRequestFilter;
Expand Down Expand Up @@ -36,25 +36,25 @@ private void handleUnauthorizedException(
HttpServletResponse response,
Exception e) throws IOException {
UnauthorizedException ue = (UnauthorizedException) e;
ErrorMessage errorMessage = ue.getErrorMessage();
HttpStatus httpStatus = errorMessage.getHttpStatus();
setResponse(response, httpStatus, errorMessage);
ErrorCode errorCode = ue.getErrorCode();
HttpStatus httpStatus = errorCode.getHttpStatus();
setResponse(response, httpStatus, errorCode);
}

private void handleException(
HttpServletResponse response,
Exception e) throws IOException {
setResponse(response, HttpStatus.INTERNAL_SERVER_ERROR, ErrorMessage.INTERNAL_SERVER_ERROR);
setResponse(response, HttpStatus.INTERNAL_SERVER_ERROR, ErrorCode.INTERNAL_SERVER_ERROR);
}

private void setResponse(
HttpServletResponse response,
HttpStatus httpStatus,
ErrorMessage errorMessage) throws IOException {
ErrorCode errorCode) 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(errorMessage)));
writer.write(objectMapper.writeValueAsString(BaseResponse.of(errorCode)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
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.dto.ErrorMessage;
import org.hankki.hankkiserver.exception.UnauthorizedException;
import org.hankki.hankkiserver.common.code.ErrorCode;
import org.hankki.hankkiserver.common.exception.UnauthorizedException;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
Expand Down Expand Up @@ -44,7 +44,7 @@ private String getAccessToken(HttpServletRequest request) {
if (StringUtils.hasText(accessToken) && accessToken.startsWith(BEARER)) {
return accessToken.substring(BEARER.length());
}
throw new UnauthorizedException(ErrorMessage.INVALID_ACCESS_TOKEN);
throw new UnauthorizedException(ErrorCode.INVALID_ACCESS_TOKEN);
}

private void doAuthentication(
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/org/hankki/hankkiserver/auth/jwt/JwtValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtParser;
import lombok.RequiredArgsConstructor;
import org.hankki.hankkiserver.common.dto.ErrorMessage;
import org.hankki.hankkiserver.exception.UnauthorizedException;
import org.hankki.hankkiserver.common.code.ErrorCode;
import org.hankki.hankkiserver.common.exception.UnauthorizedException;
import org.springframework.stereotype.Component;

import static org.hankki.hankkiserver.auth.filter.JwtAuthenticationFilter.BEARER;
Expand All @@ -19,27 +19,27 @@ public void validateAccessToken(String accessToken) {
try {
parseToken(accessToken);
} catch (ExpiredJwtException e) {
throw new UnauthorizedException(ErrorMessage.EXPIRED_ACCESS_TOKEN);
throw new UnauthorizedException(ErrorCode.EXPIRED_ACCESS_TOKEN);
} catch (Exception e) {
throw new UnauthorizedException(ErrorMessage.INVALID_ACCESS_TOKEN_VALUE);
throw new UnauthorizedException(ErrorCode.INVALID_ACCESS_TOKEN_VALUE);
}
}

public void validateRefreshToken(final String refreshToken) {
try {
parseToken(getToken(refreshToken));
} catch (ExpiredJwtException e) {
throw new UnauthorizedException(ErrorMessage.EXPIRED_REFRESH_TOKEN);
throw new UnauthorizedException(ErrorCode.EXPIRED_REFRESH_TOKEN);
} catch (Exception e) {
throw new UnauthorizedException(ErrorMessage.INVALID_REFRESH_TOKEN_VALUE);
throw new UnauthorizedException(ErrorCode.INVALID_REFRESH_TOKEN_VALUE);
}
}

public void equalsRefreshToken(
final String refreshToken,
final String storedRefreshToken) {
if (!getToken(refreshToken).equals(storedRefreshToken)) {
throw new UnauthorizedException(ErrorMessage.MISMATCH_REFRESH_TOKEN);
throw new UnauthorizedException(ErrorCode.MISMATCH_REFRESH_TOKEN);
}
}

Expand All @@ -52,6 +52,6 @@ private String getToken(final String refreshToken) {
if (refreshToken.startsWith(BEARER)) {
return refreshToken.substring(BEARER.length());
}
throw new UnauthorizedException(ErrorMessage.INVALID_ACCESS_TOKEN);
throw new UnauthorizedException(ErrorCode.INVALID_ACCESS_TOKEN);
}
}
Loading

0 comments on commit cbcb625

Please sign in to comment.