From 65da130c550eee72a2f3de17c7165afde0148309 Mon Sep 17 00:00:00 2001 From: youngreal <59333182+youngreal@users.noreply.github.com> Date: Mon, 2 Sep 2024 11:58:58 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8#29=20-=20refresh=20token=20?= =?UTF-8?q?=EC=9E=AC=EB=B0=9C=EA=B8=89=20API=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dndtravel/auth/controller/AuthController.java | 5 +++++ .../controller/request/ReIssueTokenRequest.java | 6 ++++++ .../auth/repository/RefreshTokenRepository.java | 5 +++++ .../dnd/dndtravel/auth/service/JwtProvider.java | 3 +-- .../dndtravel/auth/service/JwtTokenService.java | 15 ++++++++++++++- .../dto/response/ReissueTokenResponse.java | 7 +++++++ 6 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 src/main/java/com/dnd/dndtravel/auth/controller/request/ReIssueTokenRequest.java create mode 100644 src/main/java/com/dnd/dndtravel/auth/service/dto/response/ReissueTokenResponse.java 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..9c54c41 100644 --- a/src/main/java/com/dnd/dndtravel/auth/controller/AuthController.java +++ b/src/main/java/com/dnd/dndtravel/auth/controller/AuthController.java @@ -41,4 +41,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 +) { +} From dd5425ba0264099d22e70f222d0685b569d1ddbf Mon Sep 17 00:00:00 2001 From: youngreal <59333182+youngreal@users.noreply.github.com> Date: Mon, 2 Sep 2024 13:02:08 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=A8#29=20-=20=EB=88=84=EB=9D=BD?= =?UTF-8?q?=EB=90=9C=20import=EB=AC=B8=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/dnd/dndtravel/auth/controller/AuthController.java | 2 ++ 1 file changed, 2 insertions(+) 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 9c54c41..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;