-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from Central-MakeUs/refactor/19
Refactor/19: spring security ๋ฆฌํฉํ ๋ง
- Loading branch information
Showing
51 changed files
with
1,138 additions
and
889 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
File renamed without changes.
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,6 @@ | ||
package com.cmc.suppin.global.enums; | ||
|
||
public enum UserRole { | ||
ROLE_ADMIN, | ||
ROLE_USER | ||
} |
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,5 @@ | ||
package com.cmc.suppin.global.enums; | ||
|
||
public enum UserStatus { | ||
ACTIVE, INACTIVE, DELETED | ||
} |
This file was deleted.
Oops, something went wrong.
9 changes: 7 additions & 2 deletions
9
src/main/java/com/cmc/suppin/global/exception/BaseErrorCode.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,8 +1,13 @@ | ||
package com.cmc.suppin.global.exception; | ||
|
||
import com.cmc.suppin.global.response.ErrorResponse; | ||
import org.springframework.http.HttpStatus; | ||
|
||
public interface BaseErrorCode { | ||
|
||
public ErrorReasonDTO getReason(); | ||
ErrorResponse getErrorResponse(); | ||
|
||
String getMessage(); | ||
|
||
public ErrorReasonDTO getReasonHttpStatus(); | ||
HttpStatus getStatus(); | ||
} |
37 changes: 37 additions & 0 deletions
37
src/main/java/com/cmc/suppin/global/exception/CommonErrorCode.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,37 @@ | ||
package com.cmc.suppin.global.exception; | ||
|
||
import com.cmc.suppin.global.response.ErrorResponse; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
@AllArgsConstructor | ||
public enum CommonErrorCode implements BaseErrorCode { | ||
|
||
// ๊ฐ์ฅ ์ผ๋ฐ์ ์ธ ์๋ฌ | ||
_INTERNAL_SERVER_ERROR(HttpStatus.INTERNAL_SERVER_ERROR, "COMMON500", "์๋ฒ ์๋ฌ, ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ ๋ฐ๋๋๋ค."), | ||
_BAD_REQUEST(HttpStatus.BAD_REQUEST, "COMMON400", "์๋ชป๋ ์์ฒญ์ ๋๋ค."), | ||
_FORBIDDEN(HttpStatus.FORBIDDEN, "COMMON403", "๊ธ์ง๋ ์์ฒญ์ ๋๋ค."), | ||
|
||
// test | ||
TEMP_EXCEPTION(HttpStatus.BAD_REQUEST, "TEMP4001", "ํ ์คํธ"), | ||
|
||
// ํ์ด์ง ๊ด๋ จ ์๋ฌ | ||
PAGE_NEGATIVE_INPUT(HttpStatus.BAD_REQUEST, "PAGE4001", "ํ์ด์ง ๋ฒํธ๋ 1์ด์์ ์ซ์์ฌ์ผ ํฉ๋๋ค."), | ||
; | ||
|
||
private final HttpStatus httpStatus; | ||
private final String code; | ||
private final String message; | ||
|
||
@Override | ||
public ErrorResponse getErrorResponse() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public HttpStatus getStatus() { | ||
return null; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/cmc/suppin/global/exception/CustomException.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,14 @@ | ||
package com.cmc.suppin.global.exception; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
public class CustomException extends RuntimeException { | ||
|
||
private final BaseErrorCode errorCode; | ||
|
||
public CustomException(BaseErrorCode errorCode) { | ||
super(errorCode.getMessage()); | ||
this.errorCode = errorCode; | ||
} | ||
} |
23 changes: 0 additions & 23 deletions
23
src/main/java/com/cmc/suppin/global/exception/ErrorReasonDTO.java
This file was deleted.
Oops, something went wrong.
115 changes: 0 additions & 115 deletions
115
src/main/java/com/cmc/suppin/global/exception/ExceptionAdvice.java
This file was deleted.
Oops, something went wrong.
19 changes: 0 additions & 19 deletions
19
src/main/java/com/cmc/suppin/global/exception/GeneralException.java
This file was deleted.
Oops, something went wrong.
30 changes: 30 additions & 0 deletions
30
src/main/java/com/cmc/suppin/global/exception/MemberErrorCode.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.cmc.suppin.global.exception; | ||
|
||
import com.cmc.suppin.global.response.ErrorResponse; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public enum MemberErrorCode implements BaseErrorCode { | ||
MEMBER_NOT_FOUND("mem-404/01", HttpStatus.NOT_FOUND, "ํ์์ ์ฐพ์ ์ ์์ต๋๋ค."), | ||
VALIDATION_FAILED("mem-400/01", HttpStatus.BAD_REQUEST, "์ ๋ ฅ๊ฐ์ ๋ํ ๊ฒ์ฆ์ ์คํจํ์ต๋๋ค."), | ||
MEMBER_ALREADY_DELETED("mem-400/02", HttpStatus.BAD_REQUEST, "ํํดํ ํ์์ ๋๋ค."), | ||
PASSWORD_CONFIRM_NOT_MATCHED("mem-400/03", HttpStatus.BAD_REQUEST, "๋น๋ฐ๋ฒํธ๊ฐ ํ์ธ์ด ์ผ์นํ์ง ์์ต๋๋ค."), | ||
DUPLICATE_MEMBER_EMAIL("mem-409/01", HttpStatus.CONFLICT, "์ด๋ฏธ ์กด์ฌํ๋ ์ด๋ฉ์ผ์ ๋๋ค."), | ||
DUPLICATE_NICKNAME("mem-409/01", HttpStatus.CONFLICT, "์ด๋ฏธ ์กด์ฌํ๋ ๋๋ค์์ ๋๋ค."); | ||
|
||
private final String code; | ||
private final HttpStatus status; | ||
private final String message; | ||
|
||
MemberErrorCode(String code, HttpStatus status, String message) { | ||
this.code = code; | ||
this.status = status; | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public ErrorResponse getErrorResponse() { | ||
return ErrorResponse.of(code, message); | ||
} | ||
} |
20 changes: 0 additions & 20 deletions
20
src/main/java/com/cmc/suppin/global/exception/ReasonDTO.java
This file was deleted.
Oops, something went wrong.
32 changes: 32 additions & 0 deletions
32
src/main/java/com/cmc/suppin/global/exception/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,32 @@ | ||
package com.cmc.suppin.global.exception; | ||
|
||
import com.cmc.suppin.global.response.ErrorResponse; | ||
import lombok.Getter; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Getter | ||
public enum SecurityErrorCode implements BaseErrorCode { | ||
INVALID_TOKEN("sec-400/01", HttpStatus.BAD_REQUEST, "์ ํจํ์ง ์์ ํ ํฐ์ ๋๋ค."), | ||
INVALID_OAUTH_CODE("sec-400/02", HttpStatus.BAD_REQUEST, "์ ํจํ์ง ์์ ์์ ๋ก๊ทธ์ธ ์ฝ๋์ ๋๋ค."), | ||
UNAUTHORIZED("sec-401/01", HttpStatus.UNAUTHORIZED, "๋ก๊ทธ์ธ ํด์ฃผ์ธ์."), | ||
ACCESS_TOKEN_EXPIRED("sec-401/02", HttpStatus.UNAUTHORIZED, "ํ ํฐ์ด ๋ง๋ฃ๋์์ต๋๋ค"), | ||
REFRESH_TOKEN_EXPIRED("sec-401/03", HttpStatus.UNAUTHORIZED, "๋ค์ ๋ก๊ทธ์ธ ํด์ฃผ์ธ์."), | ||
ALREADY_LOGOUT("sec-401/04", HttpStatus.UNAUTHORIZED, "๋ก๊ทธ์์ ์ํ๋ก ์ฌ๋ก๊ทธ์ธ์ด ํ์ํฉ๋๋ค."), | ||
FORBIDDEN("sec-403/01", HttpStatus.FORBIDDEN, "๊ถํ์ด ์์ต๋๋ค"), | ||
OAUTH_LOGIN_FAILED("sec-500", HttpStatus.INTERNAL_SERVER_ERROR, "์์ ๋ก๊ทธ์ธ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค. ๊ด๋ฆฌ์์๊ฒ ๋ฌธ์ํ์ธ์."); | ||
|
||
private final String code; | ||
private final HttpStatus status; | ||
private final String message; | ||
|
||
SecurityErrorCode(String code, HttpStatus status, String message) { | ||
this.code = code; | ||
this.status = status; | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public ErrorResponse getErrorResponse() { | ||
return ErrorResponse.of(code, message); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/cmc/suppin/global/exception/handler/GlobalExceptionHandler.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,4 +1,45 @@ | ||
package com.cmc.suppin.global.exception.handler; | ||
|
||
import com.cmc.suppin.global.exception.BaseErrorCode; | ||
import com.cmc.suppin.global.exception.CustomException; | ||
import com.cmc.suppin.global.response.ErrorResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.validation.BindingResult; | ||
import org.springframework.validation.FieldError; | ||
import org.springframework.web.bind.MethodArgumentNotValidException; | ||
import org.springframework.web.bind.annotation.ExceptionHandler; | ||
import org.springframework.web.bind.annotation.RestControllerAdvice; | ||
|
||
import java.util.List; | ||
|
||
@Slf4j | ||
@RestControllerAdvice | ||
public class GlobalExceptionHandler { | ||
|
||
@ExceptionHandler(CustomException.class) | ||
protected ResponseEntity<ErrorResponse> handleCustomException(CustomException e) { | ||
log.warn(">>>>> Custom Exception: ", e); | ||
BaseErrorCode errorCode = e.getErrorCode(); | ||
return ResponseEntity.status(errorCode.getStatus()) | ||
.body(errorCode.getErrorResponse()); | ||
} | ||
|
||
@ExceptionHandler(MethodArgumentNotValidException.class) | ||
protected ResponseEntity<ErrorResponse> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { | ||
log.warn(">>>>> Validation Failed: ", e); | ||
BindingResult bindingResult = e.getBindingResult(); | ||
List<FieldError> fieldErrors = bindingResult.getFieldErrors(); | ||
ErrorResponse errorResponse = ErrorResponse.of("400", "์ ๋ ฅ๊ฐ์ ๋ํ ๊ฒ์ฆ์ ์คํจํ์ต๋๋ค."); | ||
fieldErrors.forEach(error -> errorResponse.addValidation(error.getField(), error.getDefaultMessage())); | ||
return ResponseEntity.status(e.getStatusCode()).body(errorResponse); | ||
} | ||
|
||
@ExceptionHandler(Exception.class) | ||
protected ResponseEntity<ErrorResponse> handleGlobalException(Exception e) { | ||
log.error(">>>>> Internal Server Error: ", e); | ||
ErrorResponse errorResponse = ErrorResponse.of("500", e.getMessage()); | ||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse); | ||
} | ||
} |
Oops, something went wrong.