-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: security 모듈 CustomException 정의 및 ErrorCode 정리 (#159)
* refactor: ErrorCode 정리 * refactor: HttpResponseUtil 적용 * refactor: SecurityCustomException 생성, 적용 * refactor: GlobalExceptionHandler 리팩터링 --------- Co-authored-by: Kim Dae Hwi <[email protected]>
- Loading branch information
Showing
14 changed files
with
116 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 0 additions & 9 deletions
9
api/api-member/src/main/java/com/pgms/apimember/exception/SecurityException.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
core/core-domain/src/main/java/com/pgms/coredomain/domain/common/GlobalErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.pgms.coredomain.domain.common; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import com.pgms.coredomain.response.ErrorResponse; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum GlobalErrorCode implements BaseErrorCode { | ||
INTERNAL_SERVER_ERROR("INTERNAL SERVER ERROR", HttpStatus.INTERNAL_SERVER_ERROR, "서버에 문제가 발생했습니다. 잠시 후 다시 시도해주세요."), | ||
VALIDATION_FAILED("VALIDATION FAILED", HttpStatus.BAD_REQUEST, "입력값에 대한 검증에 실패했습니다."); | ||
|
||
private final String errorCode; | ||
private final HttpStatus status; | ||
private final String message; | ||
|
||
GlobalErrorCode(String errorCode, HttpStatus status, String message) { | ||
this.errorCode = errorCode; | ||
this.status = status; | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public ErrorResponse getErrorResponse() { | ||
return new ErrorResponse(errorCode, message); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
core/core-domain/src/main/java/com/pgms/coredomain/domain/common/SecurityErrorCode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.pgms.coredomain.domain.common; | ||
|
||
import org.springframework.http.HttpStatus; | ||
|
||
import com.pgms.coredomain.response.ErrorResponse; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public enum SecurityErrorCode implements BaseErrorCode { | ||
UNAUTHORIZED("UNAUTHORIZED", HttpStatus.UNAUTHORIZED, "로그인 해주세요."), | ||
ACCESS_TOKEN_EXPIRED("ACCESS TOKEN EXPIRED", HttpStatus.UNAUTHORIZED, "토큰이 만료되었습니다"), | ||
REFRESH_TOKEN_EXPIRED("REFRESH TOKEN EXPIRED", HttpStatus.UNAUTHORIZED, "다시 로그인 해주세요."), | ||
FORBIDDEN("FORBIDDEN", HttpStatus.FORBIDDEN, "권한이 없습니다"); | ||
|
||
private final String errorCode; | ||
private final HttpStatus status; | ||
private final String message; | ||
|
||
SecurityErrorCode(String errorCode, HttpStatus status, String message) { | ||
this.errorCode = errorCode; | ||
this.status = status; | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public ErrorResponse getErrorResponse() { | ||
return new ErrorResponse(errorCode, message); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...urity/src/main/java/com/pgms/coresecurity/security/exception/SecurityCustomException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.pgms.coresecurity.security.exception; | ||
|
||
import com.pgms.coredomain.domain.common.BaseErrorCode; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class SecurityCustomException extends RuntimeException { | ||
|
||
private final BaseErrorCode errorCode; | ||
|
||
public SecurityCustomException(BaseErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.errorCode = errorCode; | ||
} | ||
} |
17 changes: 4 additions & 13 deletions
17
...ore-security/src/main/java/com/pgms/coresecurity/security/jwt/JwtAccessDeniedHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,32 @@ | ||
package com.pgms.coresecurity.security.jwt; | ||
|
||
import static com.pgms.coredomain.domain.common.SecurityErrorCode.*; | ||
|
||
import java.io.IOException; | ||
|
||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.web.access.AccessDeniedHandler; | ||
import org.springframework.stereotype.Component; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.pgms.coredomain.response.ErrorResponse; | ||
import com.pgms.coresecurity.security.util.HttpResponseUtil; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
/** | ||
* 인증된 사용자가 필요한 권한없이 접근하려고 할 때 발생하는 예외 처리 | ||
*/ | ||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class JwtAccessDeniedHandler implements AccessDeniedHandler { | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@Override | ||
public void handle(HttpServletRequest request, HttpServletResponse response, | ||
AccessDeniedException accessDeniedException) throws IOException { | ||
log.warn("Access Denied: ", accessDeniedException); | ||
|
||
ErrorResponse errorResponse = new ErrorResponse("FORBIDDEN", "권한이 없습니다."); | ||
|
||
response.setContentType(MediaType.APPLICATION_JSON_VALUE); | ||
response.setStatus(HttpStatus.FORBIDDEN.value()); | ||
response.setCharacterEncoding("UTF-8"); | ||
objectMapper.writeValue(response.getOutputStream(), errorResponse); | ||
HttpResponseUtil.setErrorResponse(response, HttpStatus.FORBIDDEN, FORBIDDEN.getErrorResponse()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters