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: Jwt 일부 코드 복구 및 RefreshToken 만료 관련 예외처리 반영 (#152) #154

Merged
merged 1 commit into from
Jan 25, 2024
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
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);
}
}
Loading