Skip to content

Commit

Permalink
refactor: 예외 클래스 이름 더 명확하게 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
zzoe2346 committed Oct 24, 2024
1 parent d4bd2c3 commit a377d8f
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 39 deletions.
14 changes: 7 additions & 7 deletions src/main/java/com/example/sinitto/auth/service/TokenService.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.example.sinitto.auth.service;

import com.example.sinitto.auth.dto.TokenResponse;
import com.example.sinitto.common.exception.AccessTokenExpired;
import com.example.sinitto.common.exception.ForceLogoutException;
import com.example.sinitto.common.exception.RefreshTokenStolen;
import com.example.sinitto.common.exception.AccessTokenExpiredException;
import com.example.sinitto.common.exception.InvalidJwtException;
import com.example.sinitto.common.exception.RefreshTokenStolenException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
Expand Down Expand Up @@ -63,11 +63,11 @@ public String extractEmail(String token) {
.parseClaimsJws(token)
.getBody();
} catch (Exception e) {
throw new ForceLogoutException(e.getMessage());
throw new InvalidJwtException(e.getMessage());
}

if (claims.getExpiration().before(new Date())) {
throw new AccessTokenExpired("액세스 토큰이 만료되었습니다. 리프레시 토큰으로 다시 액세스 토큰을 발급받으세요.");
throw new AccessTokenExpiredException("액세스 토큰이 만료되었습니다. 리프레시 토큰으로 다시 액세스 토큰을 발급받으세요.");
}

return claims.getSubject();
Expand All @@ -79,11 +79,11 @@ public TokenResponse refreshAccessToken(String refreshToken) {
String storedRefreshToken = redisTemplate.opsForValue().get(email);

if (storedRefreshToken == null) {
throw new ForceLogoutException("토큰이 만료되었습니다. 재로그인이 필요합니다.");
throw new InvalidJwtException("토큰이 만료되었습니다. 재로그인이 필요합니다.");
}

if (!storedRefreshToken.equals(refreshToken)) {
throw new RefreshTokenStolen("이미 한번 사용된 리프레시 토큰입니다. 리프레시 토큰이 탈취되었을 가능성이 있습니다.");
throw new RefreshTokenStolenException("이미 한번 사용된 리프레시 토큰입니다. 리프레시 토큰이 탈취되었을 가능성이 있습니다.");
}

redisTemplate.delete(email);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.sinitto.common.exception;

public class AccessTokenExpiredException extends RuntimeException {

public AccessTokenExpiredException(String message) {
super(message);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -50,24 +50,24 @@ public ResponseEntity<ProblemDetail> handleBadRequestException(BadRequestExcepti
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(problemDetail);
}

@ExceptionHandler(ForceLogoutException.class)
public ResponseEntity<ProblemDetail> handleForceLogoutException(ForceLogoutException e) {
@ExceptionHandler(InvalidJwtException.class)
public ResponseEntity<ProblemDetail> handleInvalidJwtException(InvalidJwtException e) {

ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(460), e.getMessage());
problemDetail.setTitle("Force Logout");
problemDetail.setTitle("Invalid Jwt");
return ResponseEntity.status(HttpStatusCode.valueOf(460)).body(problemDetail);
}

@ExceptionHandler(AccessTokenExpired.class)
public ResponseEntity<ProblemDetail> handleAccessTokenExpired(AccessTokenExpired e) {
@ExceptionHandler(AccessTokenExpiredException.class)
public ResponseEntity<ProblemDetail> handleAccessTokenExpiredException(AccessTokenExpiredException e) {

ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(461), e.getMessage());
problemDetail.setTitle("Access Token Expired");
return ResponseEntity.status(HttpStatusCode.valueOf(461)).body(problemDetail);
}

@ExceptionHandler(RefreshTokenStolen.class)
public ResponseEntity<ProblemDetail> handleRefreshTokenStolen(RefreshTokenStolen e) {
@ExceptionHandler(RefreshTokenStolenException.class)
public ResponseEntity<ProblemDetail> handleRefreshTokenStolenException(RefreshTokenStolenException e) {

ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatusCode.valueOf(462), e.getMessage());
problemDetail.setTitle("Refresh Token Stolen");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.sinitto.common.exception;

public class InvalidJwtException extends RuntimeException {

public InvalidJwtException(String message) {
super(message);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.example.sinitto.common.exception;

public class RefreshTokenStolenException extends RuntimeException {

public RefreshTokenStolenException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.example.sinitto.common.exception;

public class UnauthorizedException extends RuntimeException{
public class UnauthorizedException extends RuntimeException {

public UnauthorizedException(String message) {
super(message);
Expand Down

0 comments on commit a377d8f

Please sign in to comment.