From 5b2b8e5c73e75ca26474c71d0746f93f84c40867 Mon Sep 17 00:00:00 2001 From: soohyeon Date: Mon, 2 Sep 2024 17:02:31 +0900 Subject: [PATCH] modify :: tokenProvider --- .../org/meogo/global/jwt/JwtTokenProvider.kt | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 deletions(-) diff --git a/src/main/kotlin/org/meogo/global/jwt/JwtTokenProvider.kt b/src/main/kotlin/org/meogo/global/jwt/JwtTokenProvider.kt index 4ab1fa4..d8b82db 100644 --- a/src/main/kotlin/org/meogo/global/jwt/JwtTokenProvider.kt +++ b/src/main/kotlin/org/meogo/global/jwt/JwtTokenProvider.kt @@ -12,7 +12,6 @@ import org.meogo.global.jwt.entity.repository.RefreshTokenRepository import org.meogo.global.jwt.exception.ExpiredTokenException import org.meogo.global.jwt.exception.InvalidJwtException import org.springframework.security.authentication.UsernamePasswordAuthenticationToken -import org.springframework.security.core.userdetails.UserDetails import org.springframework.stereotype.Component import java.util.Date import javax.servlet.http.HttpServletRequest @@ -29,16 +28,17 @@ class JwtTokenProvider( } fun getToken(name: String): TokenResponse { - val accessToken: String = generateAccessToken(name, ACCESS_KEY, jwtProperties.accessExp) - val refreshToken: String = generateRefreshToken(REFRESH_KEY, jwtProperties.refreshExp) + val accessToken = generateAccessToken(name, ACCESS_KEY, jwtProperties.accessExp) + val refreshToken = generateRefreshToken(REFRESH_KEY, jwtProperties.refreshExp) refreshTokenRepository.save( RefreshToken(name, refreshToken, jwtProperties.refreshExp) ) - return TokenResponse(accessToken = accessToken, refreshToken = refreshToken) + return TokenResponse(accessToken, refreshToken) } private fun generateAccessToken(name: String, type: String, expiration: Long): String { - return Jwts.builder().signWith(SignatureAlgorithm.HS256, jwtProperties.secretKey) + return Jwts.builder() + .signWith(SignatureAlgorithm.HS256, jwtProperties.secretKey) .setSubject(name) .setHeaderParam("type", type) .setIssuedAt(Date()) @@ -47,7 +47,8 @@ class JwtTokenProvider( } private fun generateRefreshToken(type: String, ttl: Long): String { - return Jwts.builder().signWith(SignatureAlgorithm.HS256, jwtProperties.secretKey) + return Jwts.builder() + .signWith(SignatureAlgorithm.HS256, jwtProperties.secretKey) .setHeaderParam("type", type) .setIssuedAt(Date()) .setExpiration(Date(System.currentTimeMillis() + ttl * 1000)) @@ -55,40 +56,36 @@ class JwtTokenProvider( } fun resolveToken(request: HttpServletRequest): String? { - val bearer: String? = request.getHeader("Authorization") - + val bearer = request.getHeader("Authorization") return parseToken(bearer) } fun parseToken(bearerToken: String?): String? { return if (bearerToken != null && bearerToken.startsWith("Bearer ")) { - return bearerToken.replace("Bearer ", "") + bearerToken.removePrefix("Bearer ") } else { null } } fun authorization(token: String): UsernamePasswordAuthenticationToken { - return token.let { - val userDetails: UserDetails = authDetailsService.loadUserByUsername(getTokenSubject(token)) - return UsernamePasswordAuthenticationToken(userDetails, "", userDetails.authorities) - } + val userDetails = authDetailsService.loadUserByUsername(getTokenSubject(token)) + return UsernamePasswordAuthenticationToken(userDetails, "", userDetails.authorities) } - private fun getTokenSubject(subject: String): String { - return getTokenBody(subject).subject + private fun getTokenSubject(token: String): String { + return getTokenBody(token).subject } private fun getTokenBody(token: String?): Claims { return try { - Jwts.parser().setSigningKey(jwtProperties.secretKey) - .parseClaimsJws(token).body + Jwts.parser().setSigningKey(jwtProperties.secretKey).parseClaimsJws(token).body + } catch (e: ExpiredJwtException) { + throw ExpiredTokenException + } catch (e: InvalidClaimException) { + throw InvalidJwtException } catch (e: Exception) { - when (e) { - is ExpiredJwtException -> throw ExpiredTokenException - is InvalidClaimException -> throw InvalidJwtException - else -> throw InvalidJwtException - } + throw InvalidJwtException } } }