diff --git a/src/main/java/com/dnd/dndtravel/auth/controller/AuthController.java b/src/main/java/com/dnd/dndtravel/auth/controller/AuthController.java index c805a30..99dd550 100644 --- a/src/main/java/com/dnd/dndtravel/auth/controller/AuthController.java +++ b/src/main/java/com/dnd/dndtravel/auth/controller/AuthController.java @@ -1,10 +1,12 @@ package com.dnd.dndtravel.auth.controller; +import com.dnd.dndtravel.auth.controller.request.ReIssueTokenRequest; import com.dnd.dndtravel.auth.service.dto.response.AppleIdTokenPayload; import com.dnd.dndtravel.auth.service.AppleOAuthService; import com.dnd.dndtravel.auth.service.JwtTokenService; import com.dnd.dndtravel.auth.controller.request.AppleLoginRequest; import com.dnd.dndtravel.auth.service.dto.response.TokenResponse; +import com.dnd.dndtravel.auth.service.dto.response.ReissueTokenResponse; import com.dnd.dndtravel.member.domain.Member; import com.dnd.dndtravel.member.service.MemberService; @@ -41,4 +43,9 @@ public ResponseEntity appleOAuthLogin(@RequestBody AppleLoginRequ return ResponseEntity.ok(tokenResponse); } + + @PostMapping("/reissue/token") + public ReissueTokenResponse reissueToken(@RequestBody ReIssueTokenRequest reissueTokenRequest) { + return jwtTokenService.reIssue(reissueTokenRequest.refreshToken()); + } } \ No newline at end of file diff --git a/src/main/java/com/dnd/dndtravel/auth/controller/request/ReIssueTokenRequest.java b/src/main/java/com/dnd/dndtravel/auth/controller/request/ReIssueTokenRequest.java new file mode 100644 index 0000000..39c76b6 --- /dev/null +++ b/src/main/java/com/dnd/dndtravel/auth/controller/request/ReIssueTokenRequest.java @@ -0,0 +1,6 @@ +package com.dnd.dndtravel.auth.controller.request; + +public record ReIssueTokenRequest( + String refreshToken +) { +} diff --git a/src/main/java/com/dnd/dndtravel/auth/repository/RefreshTokenRepository.java b/src/main/java/com/dnd/dndtravel/auth/repository/RefreshTokenRepository.java index 05d9236..83c7996 100644 --- a/src/main/java/com/dnd/dndtravel/auth/repository/RefreshTokenRepository.java +++ b/src/main/java/com/dnd/dndtravel/auth/repository/RefreshTokenRepository.java @@ -1,5 +1,8 @@ package com.dnd.dndtravel.auth.repository; + +import java.util.Optional; + import com.dnd.dndtravel.auth.domain.RefreshToken; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @@ -7,4 +10,6 @@ @Repository public interface RefreshTokenRepository extends JpaRepository { RefreshToken findByMemberId(Long memberId); + + Optional findByRefreshToken(String refreshToken); } \ No newline at end of file diff --git a/src/main/java/com/dnd/dndtravel/auth/service/JwtProvider.java b/src/main/java/com/dnd/dndtravel/auth/service/JwtProvider.java index 4661b13..abe324d 100644 --- a/src/main/java/com/dnd/dndtravel/auth/service/JwtProvider.java +++ b/src/main/java/com/dnd/dndtravel/auth/service/JwtProvider.java @@ -37,9 +37,8 @@ public String accessToken(Long memberId) { .compact(); } - public String refreshToken(Long memberId) { + public String refreshToken() { return Jwts.builder() - .claim(CLAIM_CONTENT, memberId) .issuedAt(new Date(System.currentTimeMillis())) .expiration(new Date(System.currentTimeMillis() + this.refreshTokenExpiredTime)) .signWith(secretKey) diff --git a/src/main/java/com/dnd/dndtravel/auth/service/JwtTokenService.java b/src/main/java/com/dnd/dndtravel/auth/service/JwtTokenService.java index a52ddea..79b70cb 100644 --- a/src/main/java/com/dnd/dndtravel/auth/service/JwtTokenService.java +++ b/src/main/java/com/dnd/dndtravel/auth/service/JwtTokenService.java @@ -6,6 +6,7 @@ import com.dnd.dndtravel.auth.domain.RefreshToken; import com.dnd.dndtravel.auth.repository.RefreshTokenRepository; import com.dnd.dndtravel.auth.service.dto.response.TokenResponse; +import com.dnd.dndtravel.auth.service.dto.response.ReissueTokenResponse; import lombok.RequiredArgsConstructor; @@ -21,7 +22,7 @@ public TokenResponse generateTokens(Long memberId) { RefreshToken refreshToken = refreshTokenRepository.findByMemberId(memberId); if (refreshToken == null) { - String newRefreshToken = jwtProvider.refreshToken(memberId); + String newRefreshToken = jwtProvider.refreshToken(); refreshTokenRepository.save(RefreshToken.of(memberId, newRefreshToken)); // refreshToken은 DB에 저장 return new TokenResponse(jwtProvider.accessToken(memberId), newRefreshToken); } else if (refreshToken.isExpire()) { @@ -30,4 +31,16 @@ public TokenResponse generateTokens(Long memberId) { return new TokenResponse(jwtProvider.accessToken(memberId), null); } + + @Transactional + public ReissueTokenResponse reIssue(String token) { + //validation + RefreshToken refreshToken = refreshTokenRepository.findByRefreshToken(token).orElseThrow(() -> new RuntimeException("유효하지 않은 토큰")); + + //RTR + refreshTokenRepository.delete(refreshToken); + String newRefreshToken = jwtProvider.refreshToken(); + refreshTokenRepository.save(RefreshToken.of(refreshToken.getMemberId(), newRefreshToken)); + return new ReissueTokenResponse(jwtProvider.accessToken(refreshToken.getMemberId()), newRefreshToken); + } } diff --git a/src/main/java/com/dnd/dndtravel/auth/service/dto/response/ReissueTokenResponse.java b/src/main/java/com/dnd/dndtravel/auth/service/dto/response/ReissueTokenResponse.java new file mode 100644 index 0000000..f7a880f --- /dev/null +++ b/src/main/java/com/dnd/dndtravel/auth/service/dto/response/ReissueTokenResponse.java @@ -0,0 +1,7 @@ +package com.dnd.dndtravel.auth.service.dto.response; + +public record ReissueTokenResponse( + String accessToken, + String refreshToken +) { +}