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; .claim("memberId", memberId)
.setIssuedAt(date)
.setExpiration(accessTokenExpiredAt)
.signWith(SignatureAlgorithm.HS256, secretKey.toByteArray())
.compact()
}
public TokenDto createToken(Long memberId) { fun getMemberIdFromToken(token: String?): Long {
Date date = new Date(); try {
Date accessTokenExpiredAt = new Date(date.getTime() + accessTokenExpireTime); return Jwts.parser()
.setSigningKey(secretKey.toByteArray())
String accessToken = Jwts.builder() .parseClaimsJws(token)
.claim("memberId", memberId) .getBody()
.setIssuedAt(date) .get("memberId", Number::class.java)
.setExpiration(accessTokenExpiredAt) .toLong()
.signWith(SignatureAlgorithm.HS256, secretKey.getBytes()) } catch (e: Exception) {
.compact(); when (e) {
is ExpiredJwtException -> throw RoomEscapeException(ErrorType.EXPIRED_TOKEN, HttpStatus.UNAUTHORIZED)
return new TokenDto(accessToken); is UnsupportedJwtException -> throw RoomEscapeException(ErrorType.UNSUPPORTED_TOKEN, HttpStatus.UNAUTHORIZED)
} is MalformedJwtException -> throw RoomEscapeException(ErrorType.MALFORMED_TOKEN, HttpStatus.UNAUTHORIZED)
is SignatureException -> throw RoomEscapeException(ErrorType.INVALID_SIGNATURE_TOKEN, HttpStatus.UNAUTHORIZED)
public Long getMemberIdFromToken(String token) { is IllegalArgumentException -> throw RoomEscapeException(ErrorType.INVALID_TOKEN, HttpStatus.UNAUTHORIZED)
validateToken(token); else -> throw RoomEscapeException(ErrorType.UNEXPECTED_ERROR, HttpStatus.INTERNAL_SERVER_ERROR)
}
return Jwts.parser().setSigningKey(secretKey.getBytes()).parseClaimsJws(token) }
.getBody() }
.get("memberId", Long.class);
}
public void validateToken(String token) {
try {
Jwts.parser().setSigningKey(secretKey.getBytes()).parseClaimsJws(token);
} catch (ExpiredJwtException e) {
throw new RoomEscapeException(ErrorType.EXPIRED_TOKEN, HttpStatus.UNAUTHORIZED);
} catch (UnsupportedJwtException e) {
throw new RoomEscapeException(ErrorType.UNSUPPORTED_TOKEN, HttpStatus.UNAUTHORIZED);
} catch (MalformedJwtException e) {
throw new RoomEscapeException(ErrorType.MALFORMED_TOKEN, HttpStatus.UNAUTHORIZED);
} catch (SignatureException e) {
throw new RoomEscapeException(ErrorType.INVALID_SIGNATURE_TOKEN, HttpStatus.UNAUTHORIZED);
} catch (IllegalArgumentException e) {
throw new RoomEscapeException(ErrorType.ILLEGAL_TOKEN, HttpStatus.UNAUTHORIZED);
}
}
} }