Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: 리프레시 토큰 및 엑세스 토큰 만료 예외 분리 #284

Merged
merged 1 commit into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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