Skip to content

Commit

Permalink
✨ Fix: Jwt 일부 코드 복구 및 RefreshToken 만료 관련 예외처리 반영 (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
win-luck committed Jan 25, 2024
1 parent ad22c1c commit 8811977
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.diareat.diareat.auth.component;

import com.diareat.diareat.util.api.ResponseCode;
import com.diareat.diareat.util.exception.BaseException;
import com.diareat.diareat.util.exception.ValidException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
Expand Down Expand Up @@ -94,7 +94,7 @@ public boolean validateAccessToken(String jwtToken) {
public void validateRefreshToken(Long userPK, String refreshToken) {
String redisRefreshToken = redisTemplate.opsForValue().get(String.valueOf(userPK));
if (redisRefreshToken == null || !redisRefreshToken.equals(refreshToken)) {
throw new BaseException(ResponseCode.REFRESH_TOKEN_VALIDATION_FAILURE);
throw new ValidException(ResponseCode.REFRESH_TOKEN_VALIDATION_FAILURE);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ public ApiResponse<ResponseJwtDto> saveUser(@Valid @RequestBody JoinUserDto join
return ApiResponse.success(responseJwtDto, ResponseCode.USER_CREATE_SUCCESS.getMessage());
}

// 토큰 검증 (Jwt 토큰을 서버에 전송하여, 서버가 유효한 토큰인지 확인하고 True 혹은 예외 반환)
@Operation(summary = "[토큰 검증] 토큰 검증", description = "클라이언트가 가지고 있던 Jwt 토큰을 서버에 전송하여, 서버가 유효한 토큰인지 확인하고 OK 혹은 예외를 반환합니다.")
@GetMapping("/token")
public ApiResponse<Boolean> tokenCheck(@RequestHeader String accessToken) {
return ApiResponse.success(jwtTokenProvider.validateAccessToken(accessToken), ResponseCode.TOKEN_CHECK_SUCCESS.getMessage());
}

@Operation(summary = "[토큰 재발급] 토큰 재발급", description = "클라이언트가 가지고 있던 Refresh 토큰을 서버에 전송하여, 서버가 유효한 토큰인지 확인하고 OK 혹은 예외를 반환합니다.")
@PostMapping("/reissue")
public ApiResponse<ResponseJwtDto> reissueToken(@RequestHeader String refreshToken) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ public ApiResponse<Map<String, String>> handleInValidRequestException(MethodArgu
});
return ApiResponse.fail(ResponseCode.BAD_REQUEST, errors);
}

@ExceptionHandler(ValidException.class) // jwt 토큰 만료 관련 예외처리
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public ApiResponse<Void> handleValidException(ValidException e) {
log.info("Invalid Jwt Token: {}", e.getMessage());
return ApiResponse.fail(e.getResponseCode(), null);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.diareat.diareat.util.exception;

import com.diareat.diareat.util.api.ResponseCode;

public class ValidException extends BaseException {
public ValidException(ResponseCode responseCode) {
super(responseCode);
}
}

0 comments on commit 8811977

Please sign in to comment.