Skip to content

Commit

Permalink
리프레시 토큰 및 엑세스 토큰 만료 예외 분리 (#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
juno-junho authored Dec 1, 2023
1 parent 0a70ea2 commit 48197f3
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/docs/asciidoc/exception-api.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,11 @@
|`+ACCESS_TOKEN_NOT_EXIST+`
|엑세스 토큰이 없습니다

|`+EXPIRED_TOKEN+`
|토큰이 만료되었습니다
|`+EXPIRED_ACCESS_TOKEN+`
|엑세스 토큰이 만료되었습니다

|`+EXPIRED_REFRESH_TOKEN+`
|리프레시 토큰이 만료되었습니다

|`+INVALID_ACCESS_TOKEN+`
|유효하지 않은 엑세스 토큰입니다
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ public enum GlobalExceptionCode implements ExceptionMessageInterface {
FAIL_DESERIALIZE("JSON 데이터를 변환하는데 실패했습니다"),
INVALID_TOKEN_FORMAT("토큰 포멧이 잘못되었습니다"), // Bearer 포함 x
ACCESS_TOKEN_NOT_EXIST("엑세스 토큰이 없습니다."),
EXPIRED_TOKEN("토큰이 만료되었습니다"),
EXPIRED_ACCESS_TOKEN("엑세스 토큰이 만료되었습니다"),
EXPIRED_REFRESH_TOKEN("리프레시 토큰이 만료되었습니다"),
INVALID_ACCESS_TOKEN("유효하지 않은 엑세스 토큰입니다"),
INVALID_REFRESH_TOKEN("유효하지 않은 리프레시 토큰입니다"),
INVALID_FILE_EXTENSION("유효한 파일 확장자가 아닙니다"),
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/spaceclub/global/jwt/Jwt.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import java.util.Date;

import static com.spaceclub.global.exception.GlobalExceptionCode.EXPIRED_TOKEN;
import static com.spaceclub.global.exception.GlobalExceptionCode.EXPIRED_ACCESS_TOKEN;
import static com.spaceclub.global.exception.GlobalExceptionCode.INVALID_ACCESS_TOKEN;

@Slf4j
Expand Down Expand Up @@ -68,7 +68,7 @@ public boolean isValidFormat(String token) { // 디코딩
try {
jwtVerifier.verify(token);
} catch (TokenExpiredException e) {
throw new AccessTokenException(EXPIRED_TOKEN);
throw new AccessTokenException(EXPIRED_ACCESS_TOKEN);
} catch (JWTVerificationException e) {
throw new AccessTokenException(INVALID_ACCESS_TOKEN);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/spaceclub/global/jwt/JwtManager.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.spaceclub.global.jwt;

import com.auth0.jwt.exceptions.JWTVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.spaceclub.global.exception.RefreshTokenException;
import com.spaceclub.user.domain.User;
import com.spaceclub.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import static com.spaceclub.global.exception.GlobalExceptionCode.EXPIRED_REFRESH_TOKEN;
import static com.spaceclub.global.exception.GlobalExceptionCode.INVALID_REFRESH_TOKEN;
import static com.spaceclub.user.UserExceptionMessage.USER_NOT_FOUND;

Expand Down Expand Up @@ -45,6 +47,8 @@ public boolean isValidRefreshToken(String refreshToken, Long userId) {
private void verifyRefreshToken(String refreshToken) {
try {
jwt.verify(refreshToken);
} catch (TokenExpiredException e) {
throw new RefreshTokenException(EXPIRED_REFRESH_TOKEN);
} catch (JWTVerificationException e) {
throw new RefreshTokenException(INVALID_REFRESH_TOKEN);
}
Expand Down

0 comments on commit 48197f3

Please sign in to comment.