Skip to content

Commit 77f9c5f

Browse files
committed
feat(annotation): userId, roleId 추출 예외 및 핸들링 기능 추가
- 예외가 클라이언트에게 전달되지 않도록 처리, 로그로 기록.
1 parent 3d203c2 commit 77f9c5f

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

src/main/java/com/somemore/global/exception/ExceptionMessage.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ public enum ExceptionMessage {
6969
// NOTE
7070
NOT_EXISTS_NOTE("존재하지 않는 쪽지입니다."),
7171

72+
// AUTH
73+
AUTHENTICATION_MISSING("사용자가 인증되지 않았습니다."),
74+
INVALID_PRINCIPAL_TYPE("인증 객체 타입이 올바르지 않습니다."),
75+
7276
;
7377
private final String message;
7478
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.somemore.global.exception;
2+
3+
import org.springframework.security.core.AuthenticationException;
4+
5+
public class InvalidAuthenticationException extends AuthenticationException {
6+
7+
public InvalidAuthenticationException(final String message) {
8+
super(message);
9+
}
10+
11+
public InvalidAuthenticationException(final String message, final Throwable cause) {
12+
super(message, cause);
13+
}
14+
15+
public InvalidAuthenticationException(final ExceptionMessage message) {
16+
super(message.getMessage());
17+
}
18+
19+
public InvalidAuthenticationException(final ExceptionMessage message, final Throwable cause) {
20+
super(message.getMessage(), cause);
21+
}
22+
}

src/main/java/com/somemore/global/exception/handler/GlobalExceptionHandler.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
import com.somemore.global.exception.BadRequestException;
44
import com.somemore.global.exception.DuplicateException;
55
import com.somemore.global.exception.ImageUploadException;
6+
import com.somemore.global.exception.InvalidAuthenticationException;
67
import com.somemore.global.exception.NoSuchElementException;
8+
import lombok.extern.slf4j.Slf4j;
79
import org.springframework.http.HttpStatus;
810
import org.springframework.http.ProblemDetail;
911
import org.springframework.web.bind.MethodArgumentNotValidException;
1012
import org.springframework.web.bind.annotation.ExceptionHandler;
1113
import org.springframework.web.bind.annotation.RestControllerAdvice;
1214

1315
@RestControllerAdvice
16+
@Slf4j
1417
public class GlobalExceptionHandler {
1518

1619
//예시 코드
@@ -66,4 +69,16 @@ ProblemDetail handleNoSuchElementException(final NoSuchElementException e) {
6669
return problemDetail;
6770
}
6871

72+
@ExceptionHandler(InvalidAuthenticationException.class)
73+
ProblemDetail handleInvalidAuthenticationException(InvalidAuthenticationException e) {
74+
75+
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.BAD_REQUEST, e.getMessage());
76+
problemDetail.setTitle("인증 문제");
77+
problemDetail.setDetail("인증에 문제가 발생했습니다.");
78+
79+
log.warn("InvalidAuthenticationException: {}", e.getMessage());
80+
81+
return problemDetail;
82+
}
83+
6984
}

0 commit comments

Comments
 (0)