From aad86e128e573cdf294553bb0e192ab03b80ca18 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A3=BC=EC=98=81?= Date: Tue, 28 Nov 2023 10:28:12 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20::=20=ED=86=A0=ED=81=B0=20?= =?UTF-8?q?=EC=9E=AC=EB=B0=9C=EA=B8=89=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/user/domain/RefreshToken.kt | 8 +++++-- .../repository/RefreshTokenRepository.kt | 4 +++- .../user/presentation/UserController.kt | 7 ++++++ .../user/service/UserTokenRefreshService.kt | 24 +++++++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) create mode 100644 src/main/kotlin/com/example/sharing/domain/user/service/UserTokenRefreshService.kt diff --git a/src/main/kotlin/com/example/sharing/domain/user/domain/RefreshToken.kt b/src/main/kotlin/com/example/sharing/domain/user/domain/RefreshToken.kt index 493aba1..5e1c46e 100644 --- a/src/main/kotlin/com/example/sharing/domain/user/domain/RefreshToken.kt +++ b/src/main/kotlin/com/example/sharing/domain/user/domain/RefreshToken.kt @@ -13,5 +13,9 @@ class RefreshToken( @Indexed @field:NotBlank - val token: String -) \ No newline at end of file + var token: String +) { + fun updateToken(token: String) { + this.token = token + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/sharing/domain/user/domain/repository/RefreshTokenRepository.kt b/src/main/kotlin/com/example/sharing/domain/user/domain/repository/RefreshTokenRepository.kt index 28e8ea7..6121418 100644 --- a/src/main/kotlin/com/example/sharing/domain/user/domain/repository/RefreshTokenRepository.kt +++ b/src/main/kotlin/com/example/sharing/domain/user/domain/repository/RefreshTokenRepository.kt @@ -5,4 +5,6 @@ import org.springframework.data.repository.CrudRepository import org.springframework.stereotype.Repository @Repository -interface RefreshTokenRepository : CrudRepository \ No newline at end of file +interface RefreshTokenRepository : CrudRepository { + fun findByToken(refreshToken: String): RefreshToken +} \ No newline at end of file diff --git a/src/main/kotlin/com/example/sharing/domain/user/presentation/UserController.kt b/src/main/kotlin/com/example/sharing/domain/user/presentation/UserController.kt index c98e66a..d13b784 100644 --- a/src/main/kotlin/com/example/sharing/domain/user/presentation/UserController.kt +++ b/src/main/kotlin/com/example/sharing/domain/user/presentation/UserController.kt @@ -12,6 +12,7 @@ import com.example.sharing.domain.user.service.UpdateUserInfoService import com.example.sharing.domain.user.service.UploadProfileService import com.example.sharing.domain.user.service.UserLoginService import com.example.sharing.domain.user.service.UserSignUpService +import com.example.sharing.domain.user.service.UserTokenRefreshService import org.springframework.http.HttpStatus.* import org.springframework.web.bind.annotation.* import org.springframework.web.multipart.MultipartFile @@ -27,6 +28,7 @@ class UserController( private val queryUserService: QueryUserService, private val updateUserInfoService: UpdateUserInfoService, private val uploadProfileService: UploadProfileService, + private val tokenRefreshService: UserTokenRefreshService, ) { @ResponseStatus(CREATED) @PostMapping("/signup") @@ -56,4 +58,9 @@ class UserController( fun uploadProfile(@RequestPart(name = "profile") file: MultipartFile): UploadProfileResponse { return uploadProfileService.execute(file) } + + @PutMapping("/reissue") + fun tokenReissue(@RequestHeader("Authorization") refreshToken: String): TokenResponse { + return tokenRefreshService.execute(refreshToken) + } } \ No newline at end of file diff --git a/src/main/kotlin/com/example/sharing/domain/user/service/UserTokenRefreshService.kt b/src/main/kotlin/com/example/sharing/domain/user/service/UserTokenRefreshService.kt new file mode 100644 index 0000000..14ee4ef --- /dev/null +++ b/src/main/kotlin/com/example/sharing/domain/user/service/UserTokenRefreshService.kt @@ -0,0 +1,24 @@ +package com.example.sharing.domain.user.service + +import com.example.sharing.domain.user.domain.repository.RefreshTokenRepository +import com.example.sharing.domain.user.facade.UserFacade +import com.example.sharing.domain.user.presentation.dto.response.TokenResponse +import com.example.sharing.global.security.jwt.JwtTokenProvider +import org.springframework.stereotype.Service +import org.springframework.transaction.annotation.Transactional + +@Service +class UserTokenRefreshService( + private val refreshTokenRepository: RefreshTokenRepository, + private val userFacade: UserFacade, + private val jwtTokenProvider: JwtTokenProvider, +) { + @Transactional + fun execute(refreshToken: String): TokenResponse { + val user = userFacade.getCurrentUser() + val token = refreshTokenRepository.findByToken(refreshToken) + val tokenResponse = jwtTokenProvider.getToken(user.accountId) + token.updateToken(tokenResponse.refreshToken) + return TokenResponse(tokenResponse.accessToken, tokenResponse.refreshToken) + } +} \ No newline at end of file