Skip to content

Commit

Permalink
Merge pull request #150 from dnd-side-project/fix/#149-token
Browse files Browse the repository at this point in the history
[Fix] 토큰 만료 예외 분리
  • Loading branch information
f1v3-dev authored Nov 10, 2024
2 parents e3790c3 + 24b7640 commit 32af1c4
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.dnd.jjakkak.domain.jwt.exception;

import com.dnd.jjakkak.global.exception.GeneralException;

public class AccessTokenExpiredException extends GeneralException {

private static final String MESSAGE = "Access Token이 만료되었습니다.";

public AccessTokenExpiredException() {
super(MESSAGE);
}

@Override
public int getStatusCode() {
return 401;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.dnd.jjakkak.domain.jwt.exception;

import com.dnd.jjakkak.global.exception.GeneralException;

/**
* Refresh Token이 만료되었을 때 발생하는 예외입니다.
*
* @author 정승조
* @version 2024. 11. 06.
*/
public class RefreshTokenExpiredException extends GeneralException {

private static final String MESSAGE = "Refresh Token이 만료되었습니다. 다시 로그인 해주세요.";

public RefreshTokenExpiredException() {
super(MESSAGE);
}

@Override
public int getStatusCode() {
return 418;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.dnd.jjakkak.domain.jwt.filter;

import com.dnd.jjakkak.domain.jwt.exception.TokenExpiredException;
import com.dnd.jjakkak.domain.jwt.exception.AccessTokenExpiredException;
import com.dnd.jjakkak.domain.jwt.provider.JwtProvider;
import com.dnd.jjakkak.domain.member.entity.Member;
import com.dnd.jjakkak.domain.member.exception.MemberNotFoundException;
Expand Down Expand Up @@ -69,7 +69,7 @@ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse
try {
kakaoId = jwtProvider.validateToken(token);
} catch (ExpiredJwtException e) {
throw new TokenExpiredException("accessError");
throw new AccessTokenExpiredException();
}

if (Strings.isEmpty(kakaoId)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public JwtProvider(JjakkakProperties jjakkakProperties, TokenProperties tokenPro
*/
public String createAccessToken(String kakaoId) {

Date expiredDate = Date.from(Instant.now().plus(accessTokenExpirationDay, ChronoUnit.DAYS));
// Date expiredDate = Date.from(Instant.now().plus(accessTokenExpirationDay, ChronoUnit.DAYS));

// fixme: 테스트를 위해 AT 만료시간 3분으로 설정함!
// Date expiredDate = Date.from(Instant.now().plus(3, ChronoUnit.MINUTES));
Date expiredDate = Date.from(Instant.now().plus(3, ChronoUnit.MINUTES));

return Jwts.builder()
.signWith(key, SignatureAlgorithm.HS256)
Expand All @@ -70,7 +70,11 @@ public String createAccessToken(String kakaoId) {
* @return JWT
*/
public String createRefreshToken(String kakaoId) {
Date expiredDate = Date.from(Instant.now().plus(refreshTokenExpirationDay, ChronoUnit.DAYS));
// Date expiredDate = Date.from(Instant.now().plus(refreshTokenExpirationDay, ChronoUnit.DAYS));

// fixme: 테스트를 위해 RT 만료시간 5분으로 설정
Date expiredDate = Date.from(Instant.now().plus(5, ChronoUnit.MINUTES));

return Jwts.builder()
.signWith(key, SignatureAlgorithm.HS256)
.setSubject(kakaoId)
Expand All @@ -80,10 +84,11 @@ public String createRefreshToken(String kakaoId) {
}

/**
* JWT를 검증하는 메소드
* JWT를 검증하는 메소드.
*
* @param jwt String (JWT)
* @return subject (kakaoId)
* @throws ExpiredJwtException 토큰이 만료되었을 경우 발생합니다.
*/
public String validateToken(String jwt) throws JwtException {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.dnd.jjakkak.domain.member.service;

import com.dnd.jjakkak.domain.jwt.exception.TokenExpiredException;
import com.dnd.jjakkak.domain.jwt.exception.AccessTokenExpiredException;
import com.dnd.jjakkak.domain.jwt.exception.RefreshTokenExpiredException;
import com.dnd.jjakkak.domain.jwt.provider.JwtProvider;
import com.dnd.jjakkak.domain.member.dto.response.ReissueResponseDto;
import com.dnd.jjakkak.domain.member.exception.UnauthorizedException;
Expand Down Expand Up @@ -35,7 +36,7 @@ public ReissueResponseDto reissueToken(String refreshToken) {
try {
kakaoId = jwtProvider.validateToken(refreshToken);
} catch (ExpiredJwtException e) {
throw new TokenExpiredException("refreshError");
throw new RefreshTokenExpiredException();
}

String existsToken = refreshTokenService.findByKakaoId(kakaoId);
Expand Down Expand Up @@ -65,7 +66,7 @@ public boolean checkAuth(String authorization) {
try {
validate = jwtProvider.validateToken(accessToken);
} catch (ExpiredJwtException e) {
throw new TokenExpiredException("accessError");
throw new AccessTokenExpiredException();
}

return Strings.isNotBlank(validate);
Expand Down

0 comments on commit 32af1c4

Please sign in to comment.