-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from TinU-Official/issue63/refresh-token-logic…
…-feat [issue63] [Feat] RefreshToken logic 완성
- Loading branch information
Showing
10 changed files
with
147 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.tinuproject.tinu.domain.token | ||
|
||
class Tokens( | ||
val accessToken : String, | ||
val refreshToken : String | ||
) { | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
src/main/kotlin/com/tinuproject/tinu/domain/token/refreshtoken/RefreshTokenController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package com.tinuproject.tinu.domain.token.refreshtoken | ||
|
||
import com.tinuproject.tinu.domain.exception.token.InvalidedTokenException | ||
import com.tinuproject.tinu.domain.exception.token.NotFoundTokenException | ||
import com.tinuproject.tinu.domain.token.Tokens | ||
import com.tinuproject.tinu.domain.token.refreshtoken.service.RefreshTokenService | ||
import com.tinuproject.tinu.web.CookieGenerator | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.http.ResponseEntity | ||
import org.springframework.stereotype.Controller | ||
import org.springframework.web.bind.annotation.CookieValue | ||
import org.springframework.web.bind.annotation.GetMapping | ||
import org.springframework.web.bind.annotation.RequestMapping | ||
|
||
@Controller | ||
@RequestMapping("api/token") | ||
class RefreshTokenController( | ||
private val refreshTokenService : RefreshTokenService, | ||
|
||
@Value("\${cookie.token.access-token}") | ||
private val accessTokenKey : String, | ||
|
||
@Value("\${cookie.token.refresh-token}") | ||
private val refreshTokenkey : String | ||
) { | ||
var log : Logger = LoggerFactory.getLogger(this::class.java) | ||
|
||
|
||
@GetMapping("/generate") | ||
fun generateAccessToken(httpServletResponse: HttpServletResponse, @CookieValue(name = "RefreshToken") refreshToken : String?): ResponseEntity<Map<String, Any>> { | ||
var tokens : Tokens | ||
log.info("AccessToken 갱신 시도") | ||
if(refreshToken==null){ | ||
throw NotFoundTokenException() | ||
} | ||
try{ | ||
tokens = refreshTokenService.ReissueAccessTokenByRefreshToken(refreshToken) | ||
}catch (e : InvalidedTokenException){ | ||
throw e | ||
} | ||
|
||
httpServletResponse.addCookie(CookieGenerator.createCookies(accessTokenKey,tokens.accessToken)) | ||
httpServletResponse.addCookie(CookieGenerator.createCookies(refreshTokenkey, tokens.refreshToken)) | ||
var body : MutableMap<String, Any> = mutableMapOf() | ||
|
||
body.put("Tokens",tokens) | ||
|
||
var responseEntity : ResponseEntity<Map<String, Any>> = ResponseEntity.ok().body(body) | ||
return responseEntity | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
...main/kotlin/com/tinuproject/tinu/domain/token/refreshtoken/service/RefreshTokenService.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.tinuproject.tinu.domain.token.refreshtoken.service | ||
|
||
import com.tinuproject.tinu.domain.token.Tokens | ||
import org.springframework.stereotype.Service | ||
|
||
|
||
interface RefreshTokenService { | ||
fun ReissueAccessTokenByRefreshToken(refreshToken : String) : Tokens | ||
} |
53 changes: 53 additions & 0 deletions
53
.../kotlin/com/tinuproject/tinu/domain/token/refreshtoken/service/RefreshTokenServiceImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package com.tinuproject.tinu.domain.token.refreshtoken.service | ||
|
||
import com.tinuproject.tinu.domain.entity.RefreshToken | ||
import com.tinuproject.tinu.domain.exception.token.InvalidedTokenException | ||
import com.tinuproject.tinu.domain.token.Tokens | ||
import com.tinuproject.tinu.domain.token.refreshtoken.repository.RefreshTokenRepository | ||
import com.tinuproject.tinu.security.jwt.JwtUtil | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.springframework.beans.factory.annotation.Value | ||
import org.springframework.stereotype.Service | ||
import java.security.SignatureException | ||
import java.util.* | ||
|
||
@Service | ||
class RefreshTokenServiceImpl( | ||
@Value("\${jwt.refresh-token.expiration-time}") | ||
private val REFRESH_TOKEN_EXPIRATION_TIME: Long, // 리프레쉬 토큰 유효기간 | ||
|
||
@Value("\${jwt.access-token.expiration-time}") | ||
private val ACCESS_TOKEN_EXPIRATION_TIME: Long, // 액세스 토큰 유효기간 | ||
|
||
private val refreshTokenRepository: RefreshTokenRepository, | ||
private val jwtUtil : JwtUtil, | ||
):RefreshTokenService { | ||
var log : Logger = LoggerFactory.getLogger(this::class.java) | ||
|
||
override fun ReissueAccessTokenByRefreshToken(refreshToken: String): Tokens { | ||
try{ | ||
jwtUtil.validateToken(refreshToken) | ||
}catch (e : SignatureException){ | ||
throw InvalidedTokenException() | ||
} | ||
|
||
val userId :UUID = UUID.fromString(jwtUtil.getUserIdFromToken(refreshToken)) | ||
|
||
//리프레쉬 토큰 삭제 | ||
refreshTokenRepository.deleteByUserId(userId) | ||
|
||
//리프레쉬 토큰 재발행. | ||
val token = jwtUtil.generateRefreshToken(userId,REFRESH_TOKEN_EXPIRATION_TIME) | ||
|
||
val newRefreshToken = RefreshToken(userId = userId, token = token) | ||
|
||
//리프레쉬 토큰 저장 | ||
refreshTokenRepository.save(newRefreshToken) | ||
|
||
//AccesToken 재발행. | ||
val accessToken : String = jwtUtil.generateAccessToken(userId, ACCESS_TOKEN_EXPIRATION_TIME) | ||
|
||
return Tokens(accessToken=accessToken, refreshToken = refreshToken) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
src/main/kotlin/com/tinuproject/tinu/web/CookieGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.tinuproject.tinu.web | ||
|
||
import jakarta.servlet.http.Cookie | ||
|
||
class CookieGenerator { | ||
companion object{ | ||
fun createCookies(key : String, value : String) : Cookie { | ||
val cookie = Cookie(key, value) | ||
cookie.path = "/" | ||
cookie.isHttpOnly = false | ||
cookie.secure = false | ||
cookie.maxAge = 3600 | ||
|
||
return cookie | ||
} | ||
} | ||
} |