feat: JwtHandler에서 사용하는 예외 타입을 새로 정의한 AuthException으로 변경

This commit is contained in:
이상진 2025-07-23 11:31:21 +09:00
parent ef05a3ff9f
commit 91edc7bb29
2 changed files with 17 additions and 21 deletions

View File

@ -1,11 +1,12 @@
package roomescape.auth.infrastructure.jwt
import io.jsonwebtoken.*
import io.jsonwebtoken.ExpiredJwtException
import io.jsonwebtoken.Jwts
import io.jsonwebtoken.SignatureAlgorithm
import org.springframework.beans.factory.annotation.Value
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Component
import roomescape.common.exception.ErrorType
import roomescape.common.exception.RoomescapeException
import roomescape.auth.exception.AuthErrorCode
import roomescape.auth.exception.AuthException
import java.util.*
@Component
@ -36,15 +37,10 @@ class JwtHandler(
.body
.get(MEMBER_ID_CLAIM_KEY, Number::class.java)
.toLong()
} catch (e: Exception) {
when (e) {
is ExpiredJwtException -> throw RoomescapeException(ErrorType.EXPIRED_TOKEN, HttpStatus.UNAUTHORIZED)
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)
is IllegalArgumentException -> throw RoomescapeException(ErrorType.INVALID_TOKEN, HttpStatus.UNAUTHORIZED)
else -> throw RoomescapeException(ErrorType.UNEXPECTED_ERROR, HttpStatus.INTERNAL_SERVER_ERROR)
}
} catch (_: ExpiredJwtException) {
throw AuthException(AuthErrorCode.EXPIRED_TOKEN)
} catch (_: Exception) {
throw AuthException(AuthErrorCode.INVALID_TOKEN)
}
}

View File

@ -5,8 +5,8 @@ import io.jsonwebtoken.SignatureAlgorithm
import io.kotest.assertions.throwables.shouldThrow
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.shouldBe
import roomescape.common.exception.ErrorType
import roomescape.common.exception.RoomescapeException
import roomescape.auth.exception.AuthErrorCode
import roomescape.auth.exception.AuthException
import roomescape.util.JwtFixture
import java.util.*
import kotlin.random.Random
@ -33,15 +33,15 @@ class JwtHandlerTest : FunSpec({
Thread.sleep(expirationTime) // 만료 시간 이후로 대기
// when & then
shouldThrow<RoomescapeException> {
shouldThrow<AuthException> {
shortExpirationTimeJwtHandler.getMemberIdFromToken(token)
}.errorType shouldBe ErrorType.EXPIRED_TOKEN
}.errorCode shouldBe AuthErrorCode.EXPIRED_TOKEN
}
test("토큰이 빈 값이면 예외를 던진다.") {
shouldThrow<RoomescapeException> {
shouldThrow<AuthException> {
jwtHandler.getMemberIdFromToken("")
}.errorType shouldBe ErrorType.INVALID_TOKEN
}.errorCode shouldBe AuthErrorCode.INVALID_TOKEN
}
test("시크릿 키가 잘못된 경우 예외를 던진다.") {
@ -53,9 +53,9 @@ class JwtHandlerTest : FunSpec({
.signWith(SignatureAlgorithm.HS256, JwtFixture.SECRET_KEY.substring(1).toByteArray())
.compact()
shouldThrow<RoomescapeException> {
shouldThrow<AuthException> {
jwtHandler.getMemberIdFromToken(invalidSignatureToken)
}.errorType shouldBe ErrorType.INVALID_SIGNATURE_TOKEN
}.errorCode shouldBe AuthErrorCode.INVALID_TOKEN
}
}
})