Skip to content

Commit

Permalink
bugfix: JWT 재발급 로직 오류 수정 #8
Browse files Browse the repository at this point in the history
- @RequestBody 애노테이션 미설정으로 인한 문제 해결
- LoginRequest record -> class로 변경
  • Loading branch information
PgmJun committed Jan 24, 2024
1 parent 382896b commit 071ef8d
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public ApiResponse<String> logout(@MemberId final Long memberId) {
@Operation(summary = "JWT 토큰 갱신")
@ResponseStatus(HttpStatus.OK)
@PostMapping("/auth/reissue")
public ApiResponse<?> reissue(ReissueRequest request, HttpServletResponse response) {
public ApiResponse<?> reissue(@Valid @RequestBody ReissueRequest request, HttpServletResponse response) {
addTokensToCookie(createTokenService.reissueToken(request), response);

return ApiResponse.success();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

@ToString
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class LoginRequest {

@Schema(description = "소셜 로그인 타입", example = "KAKAO")
Expand All @@ -28,4 +26,4 @@ public class LoginRequest {
@Schema(description = "fcm 토큰", example = "dfdafjdslkfjslfjslifsjvmdsklvdosijsmvsdjvosadjvosd")
@NotBlank(message = "{auth.fcmToken.notBlank}")
private String fcmToken;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
package com.nice.petudio.api.controller.auth.dto.request;

public record ReissueRequest(String accessToken, String refreshToken) {

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.ToString;

@ToString
@Getter
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ReissueRequest {

@Schema(description = "JWT Access Token", example = "eyJhbGciOiJIUzUxdfadfadsMiJ9.udnKnDSK08EuX56E5k-")
@NotBlank
private String accessToken;

@Schema(description = "JWT Refresh Token", example = "eyJhbGciOiJIUzUxdfadfadsMiJ9.udnKnDSK08EuX56E5k-")
@NotBlank
private String refreshToken;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ public TokenVO createTokenInfo(Long memberId) {
return TokenVO.of(tokens.get(0), tokens.get(1));
}

public TokenVO reissueToken(final ReissueRequest request) {
Long memberId = jwtUtils.parseMemberId(request.accessToken())
public TokenVO reissueToken(ReissueRequest request) {
Long memberId = jwtUtils.parseMemberId(request.getAccessToken())
.orElseThrow();
Member member = MemberServiceUtils.findMemberById(memberRepository, memberId);

if (!jwtUtils.validateToken(request.refreshToken())) {
if (!jwtUtils.validateToken(request.getRefreshToken())) {
throw new UnAuthorizedException(ErrorCode.UNAUTHORIZED_JWT_EXCEPTION,
String.format("MemberId(%d)의 토큰 갱신 요청에 포함된 Refresh Token이 유효하지 않아, Token Refresh가 수행되지 않았습니다.",
memberId));
}
String refreshToken = (String) redisTemplate.opsForValue().get(RedisKey.REFRESH_TOKEN + memberId.toString());
String refreshToken = (String) redisTemplate.opsForValue().get(RedisKey.REFRESH_TOKEN.getKey() + memberId);
if (Objects.isNull(refreshToken)) {
throw new UnAuthorizedException(ErrorCode.UNAUTHORIZED_JWT_EXCEPTION,
String.format("보관 중인 MemberId(%d)의 Refresh Token이 존재하지 않아, Token Refresh가 수행되지 않았습니다.", memberId));
}
if (!refreshToken.equals(request.refreshToken())) {
if (!refreshToken.equals(request.getRefreshToken())) {
jwtUtils.expireRefreshToken(member.getId());
throw new UnAuthorizedException(ErrorCode.UNAUTHORIZED_JWT_EXCEPTION,
String.format("보관 중인 MemberId(%d)의 Refresh Token이 유효하지 않아, Token Refresh가 수행되지 않았습니다.", memberId));
Expand Down

0 comments on commit 071ef8d

Please sign in to comment.