refactor: JwtHandler 코틀린 전환 및 중복 코드 제거

- getMemberIdFromToken의 파라미터를 nullable로 지정하였음. null인 경우 parseClaimJws에서 예외가 발생하기 때문
This commit is contained in:
이상진 2025-07-13 20:49:51 +09:00
parent b954a2fdfb
commit cba22ce4cc

View File

@ -1,64 +1,50 @@
package roomescape.system.auth.infrastructure.jwt; package roomescape.system.auth.infrastructure.jwt
import java.util.Date; import io.jsonwebtoken.*
import org.springframework.beans.factory.annotation.Value
import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus
import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component
import org.springframework.stereotype.Component; import roomescape.system.exception.ErrorType
import roomescape.system.exception.RoomEscapeException
import io.jsonwebtoken.ExpiredJwtException; import java.util.*
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.SignatureException;
import io.jsonwebtoken.UnsupportedJwtException;
import roomescape.system.exception.ErrorType;
import roomescape.system.exception.RoomEscapeException;
@Component @Component
public class JwtHandler { class JwtHandler(
@Value("\${security.jwt.token.secret-key}")
private val secretKey: String,
@Value("${security.jwt.token.secret-key}") @Value("\${security.jwt.token.access.expire-length}")
private String secretKey; private val accessTokenExpireTime: Long
) {
fun createToken(memberId: Long): String {
val date = Date()
val accessTokenExpiredAt = Date(date.time + accessTokenExpireTime)
@Value("${security.jwt.token.access.expire-length}") return Jwts.builder()
private long accessTokenExpireTime;
public TokenDto createToken(Long memberId) {
Date date = new Date();
Date accessTokenExpiredAt = new Date(date.getTime() + accessTokenExpireTime);
String accessToken = Jwts.builder()
.claim("memberId", memberId) .claim("memberId", memberId)
.setIssuedAt(date) .setIssuedAt(date)
.setExpiration(accessTokenExpiredAt) .setExpiration(accessTokenExpiredAt)
.signWith(SignatureAlgorithm.HS256, secretKey.getBytes()) .signWith(SignatureAlgorithm.HS256, secretKey.toByteArray())
.compact(); .compact()
return new TokenDto(accessToken);
} }
public Long getMemberIdFromToken(String token) { fun getMemberIdFromToken(token: String?): Long {
validateToken(token);
return Jwts.parser().setSigningKey(secretKey.getBytes()).parseClaimsJws(token)
.getBody()
.get("memberId", Long.class);
}
public void validateToken(String token) {
try { try {
Jwts.parser().setSigningKey(secretKey.getBytes()).parseClaimsJws(token); return Jwts.parser()
} catch (ExpiredJwtException e) { .setSigningKey(secretKey.toByteArray())
throw new RoomEscapeException(ErrorType.EXPIRED_TOKEN, HttpStatus.UNAUTHORIZED); .parseClaimsJws(token)
} catch (UnsupportedJwtException e) { .getBody()
throw new RoomEscapeException(ErrorType.UNSUPPORTED_TOKEN, HttpStatus.UNAUTHORIZED); .get("memberId", Number::class.java)
} catch (MalformedJwtException e) { .toLong()
throw new RoomEscapeException(ErrorType.MALFORMED_TOKEN, HttpStatus.UNAUTHORIZED); } catch (e: Exception) {
} catch (SignatureException e) { when (e) {
throw new RoomEscapeException(ErrorType.INVALID_SIGNATURE_TOKEN, HttpStatus.UNAUTHORIZED); is ExpiredJwtException -> throw RoomEscapeException(ErrorType.EXPIRED_TOKEN, HttpStatus.UNAUTHORIZED)
} catch (IllegalArgumentException e) { is UnsupportedJwtException -> throw RoomEscapeException(ErrorType.UNSUPPORTED_TOKEN, HttpStatus.UNAUTHORIZED)
throw new RoomEscapeException(ErrorType.ILLEGAL_TOKEN, HttpStatus.UNAUTHORIZED); is MalformedJwtException -> throw RoomEscapeException(ErrorType.MALFORMED_TOKEN, HttpStatus.UNAUTHORIZED)
is SignatureException -> throw RoomEscapeException(ErrorType.INVALID_SIGNATURE_TOKEN, HttpStatus.UNAUTHORIZED)
is IllegalArgumentException -> throw RoomEscapeException(ErrorType.INVALID_TOKEN, HttpStatus.UNAUTHORIZED)
else -> throw RoomEscapeException(ErrorType.UNEXPECTED_ERROR, HttpStatus.INTERNAL_SERVER_ERROR)
}
} }
} }
} }