Skip to content

Commit

Permalink
Merge pull request #20 from Central-MakeUs/refactor/19
Browse files Browse the repository at this point in the history
Refactor/19: spring security ๋ฆฌํŒฉํ† ๋ง
  • Loading branch information
yxhwxn authored Jul 31, 2024
2 parents 403c1ee + 2b314f0 commit c3db577
Show file tree
Hide file tree
Showing 51 changed files with 1,138 additions and 889 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/dev_deploy_beanstalk.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ jobs:
spring.datasource.url: ${{ secrets.DB_URL }}
spring.datasource.username: ${{ secrets.DB_USER }}
spring.datasource.password: ${{ secrets.DB_PASSWORD }}
spring.datasource.driver-class-name: ${{ secrets.DB_DRIVER }}
jwt.token.secret: ${{ secrets.JWT_TOKEN_SECRET }}
jwt.token.expiration: ${{ secrets.ACCESS_EXPIRY_SECONDS }}

# gradlew ์‹คํ–‰ ๊ถŒํ•œ ๋ถ€์—ฌ
- name: Grant execute permission for gradlew
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/cmc/suppin/global/enums/UserRole.java
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
}
5 changes: 5 additions & 0 deletions src/main/java/com/cmc/suppin/global/enums/UserStatus.java
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
}
7 changes: 0 additions & 7 deletions src/main/java/com/cmc/suppin/global/exception/BaseCode.java

This file was deleted.

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 src/main/java/com/cmc/suppin/global/exception/CommonErrorCode.java
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 src/main/java/com/cmc/suppin/global/exception/CustomException.java
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 src/main/java/com/cmc/suppin/global/exception/ErrorReasonDTO.java

This file was deleted.

115 changes: 0 additions & 115 deletions src/main/java/com/cmc/suppin/global/exception/ExceptionAdvice.java

This file was deleted.

This file was deleted.

30 changes: 30 additions & 0 deletions src/main/java/com/cmc/suppin/global/exception/MemberErrorCode.java
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 src/main/java/com/cmc/suppin/global/exception/ReasonDTO.java

This file was deleted.

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);
}
}
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);
}
}
Loading

0 comments on commit c3db577

Please sign in to comment.