Skip to content

Commit

Permalink
Jwt 토큰 재발급 로직 수정 (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
ysw789 authored Sep 30, 2024
1 parent a4d4adc commit bb97420
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.dongyang.dongpo.controller.auth;

import com.dongyang.dongpo.apiresponse.ApiResponse;
import com.dongyang.dongpo.domain.member.Member;
import com.dongyang.dongpo.dto.JwtToken;
import com.dongyang.dongpo.dto.auth.JwtToken;
import com.dongyang.dongpo.dto.auth.JwtTokenReissueDto;
import com.dongyang.dongpo.dto.auth.SocialTokenDto;
import com.dongyang.dongpo.service.auth.SocialService;
import com.dongyang.dongpo.service.token.TokenService;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;

@RestController
Expand Down Expand Up @@ -44,7 +41,7 @@ public ResponseEntity callback(){

@PostMapping("/reissue")
@Operation(summary = "JWT토큰 재발급")
public ResponseEntity<ApiResponse<JwtToken>> reissue(@RequestBody String refreshToken) {
return ResponseEntity.ok(new ApiResponse<>(tokenService.reissueAccessToken(refreshToken)));
public ResponseEntity<ApiResponse<JwtToken>> reissue(@RequestBody JwtTokenReissueDto jwtTokenReissueDto) {
return ResponseEntity.ok(new ApiResponse<>(tokenService.reissueAccessToken(jwtTokenReissueDto)));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.dongyang.dongpo.dto;
package com.dongyang.dongpo.dto.auth;

import lombok.AllArgsConstructor;
import lombok.Builder;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.dongyang.dongpo.dto.auth;

import lombok.Getter;

@Getter
public class JwtTokenReissueDto {
private String refreshToken;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.dongyang.dongpo.config.security.CustomUserDetailsService;
import com.dongyang.dongpo.domain.member.Member.Role;
import com.dongyang.dongpo.dto.JwtToken;
import com.dongyang.dongpo.dto.auth.JwtToken;
import com.dongyang.dongpo.exception.CustomException;
import com.dongyang.dongpo.exception.ErrorCode;
import io.jsonwebtoken.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import com.dongyang.dongpo.domain.member.Member;
import com.dongyang.dongpo.domain.member.Member.SocialType;
import com.dongyang.dongpo.dto.JwtToken;
import com.dongyang.dongpo.dto.auth.JwtToken;
import com.dongyang.dongpo.dto.auth.UserInfo;
import com.dongyang.dongpo.exception.CustomException;
import com.dongyang.dongpo.exception.ErrorCode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import com.dongyang.dongpo.domain.RefreshToken;
import com.dongyang.dongpo.domain.member.Member;
import com.dongyang.dongpo.domain.member.MemberTitle;
import com.dongyang.dongpo.dto.JwtToken;
import com.dongyang.dongpo.dto.auth.JwtToken;
import com.dongyang.dongpo.dto.auth.UserInfo;
import com.dongyang.dongpo.dto.mypage.MyPageDto;
import com.dongyang.dongpo.dto.mypage.MyPageUpdateDto;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

import com.dongyang.dongpo.domain.RefreshToken;
import com.dongyang.dongpo.domain.member.Member;
import com.dongyang.dongpo.dto.JwtToken;
import com.dongyang.dongpo.dto.auth.JwtToken;
import com.dongyang.dongpo.dto.auth.JwtTokenReissueDto;
import com.dongyang.dongpo.exception.CustomException;
import com.dongyang.dongpo.exception.ErrorCode;
import com.dongyang.dongpo.jwt.JwtTokenProvider;
import com.dongyang.dongpo.repository.RefreshTokenRepository;
import com.dongyang.dongpo.repository.member.MemberRepository;
import io.jsonwebtoken.Claims;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
Expand All @@ -24,8 +24,8 @@ public class TokenService {
private final MemberRepository memberRepository;

@Transactional
public JwtToken reissueAccessToken(String token) {
String email = jwtTokenProvider.parseClaims(token).getSubject();
public JwtToken reissueAccessToken(JwtTokenReissueDto jwtTokenReissueDto) {
String email = jwtTokenProvider.parseClaims(jwtTokenReissueDto.getRefreshToken()).getSubject();
RefreshToken refreshToken = refreshTokenRepository.findByEmail(email)
.orElseThrow(() -> new CustomException(ErrorCode.EXPIRED_TOKEN));

Expand All @@ -36,7 +36,7 @@ public JwtToken reissueAccessToken(String token) {
refreshToken.updateRefreshToken(jwtToken.getRefreshToken());
refreshTokenRepository.save(refreshToken);

log.info("Refresh Token Reissued : {}", member.getId());
log.info("Refresh Token Reissued : {}", member.getEmail());
return jwtToken;
}

Expand All @@ -54,7 +54,7 @@ public JwtToken social_AlreadyExistMember(Member member){
.build();

refreshTokenRepository.save(refreshToken);
log.info("(Login) Refresh Token Reissued : {}", member.getId());
log.info("(Login) Refresh Token Reissued : {}", member.getEmail());
return jwtToken;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

import com.dongyang.dongpo.domain.RefreshToken;
import com.dongyang.dongpo.domain.member.Member;
import com.dongyang.dongpo.dto.JwtToken;
import com.dongyang.dongpo.dto.auth.JwtToken;
import com.dongyang.dongpo.dto.auth.JwtTokenReissueDto;
import com.dongyang.dongpo.exception.CustomException;
import com.dongyang.dongpo.exception.ErrorCode;
import com.dongyang.dongpo.jwt.JwtTokenProvider;
Expand Down Expand Up @@ -44,6 +45,7 @@ class TokenServiceTest {
@DisplayName("토큰 재발급")
void reissueAccessToken() {
JwtToken mockJwtToken = mock(JwtToken.class);
JwtTokenReissueDto jwtTokenReissueDto = new JwtTokenReissueDto();
when(mockJwtToken.getAccessToken()).thenReturn("AccessToken");
when(mockJwtToken.getRefreshToken()).thenReturn("RefreshToken");

Expand All @@ -55,7 +57,7 @@ void reissueAccessToken() {
when(memberRepository.findByEmail(any())).thenReturn(Optional.of(member));
when(jwtTokenProvider.createToken(any(), any())).thenReturn(mockJwtToken);

JwtToken jwtToken = tokenService.reissueAccessToken(any());
JwtToken jwtToken = tokenService.reissueAccessToken(jwtTokenReissueDto);

assertNotNull(jwtToken);
assertNotNull(jwtToken.getAccessToken());
Expand All @@ -72,13 +74,14 @@ void reissueAccessToken() {
void reissueAccessTokenExpired() {
Claims claims = mock(Claims.class);
claims.setSubject("test@email");
JwtTokenReissueDto jwtTokenReissueDto = new JwtTokenReissueDto();

when(jwtTokenProvider.parseClaims(any())).thenReturn(claims);

// Exception
when(refreshTokenRepository.findByEmail(member.getEmail())).thenReturn(Optional.empty());

Exception exception = assertThrows(Exception.class, () -> tokenService.reissueAccessToken(anyString()));
Exception exception = assertThrows(Exception.class, () -> tokenService.reissueAccessToken(jwtTokenReissueDto));

assertInstanceOf(CustomException.class, exception);
assertEquals(ErrorCode.EXPIRED_TOKEN.getMessage(), exception.getMessage());
Expand All @@ -90,14 +93,15 @@ void reissueAccessTokenExpired() {
void reissueAccessTokenMemberNotFound() {
Claims claims = mock(Claims.class);
claims.setSubject("test@email");
JwtTokenReissueDto jwtTokenReissueDto = new JwtTokenReissueDto();

when(jwtTokenProvider.parseClaims(any())).thenReturn(claims);
when(refreshTokenRepository.findByEmail(any())).thenReturn(Optional.of(refreshToken));

// Exception
when(memberRepository.findByEmail(any())).thenReturn(Optional.empty());

Exception exception = assertThrows(Exception.class, () -> tokenService.reissueAccessToken(anyString()));
Exception exception = assertThrows(Exception.class, () -> tokenService.reissueAccessToken(jwtTokenReissueDto));

assertInstanceOf(CustomException.class, exception);
assertEquals(ErrorCode.MEMBER_NOT_FOUND.getMessage(), exception.getMessage());
Expand Down

0 comments on commit bb97420

Please sign in to comment.