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 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.beans.factory.annotation.Value
import org.springframework.http.HttpStatus
import org.springframework.stereotype.Component import org.springframework.stereotype.Component
import roomescape.common.exception.ErrorType import roomescape.auth.exception.AuthErrorCode
import roomescape.common.exception.RoomescapeException import roomescape.auth.exception.AuthException
import java.util.* import java.util.*
@Component @Component
@ -36,15 +37,10 @@ class JwtHandler(
.body .body
.get(MEMBER_ID_CLAIM_KEY, Number::class.java) .get(MEMBER_ID_CLAIM_KEY, Number::class.java)
.toLong() .toLong()
} catch (e: Exception) { } catch (_: ExpiredJwtException) {
when (e) { throw AuthException(AuthErrorCode.EXPIRED_TOKEN)
is ExpiredJwtException -> throw RoomescapeException(ErrorType.EXPIRED_TOKEN, HttpStatus.UNAUTHORIZED) } catch (_: Exception) {
is UnsupportedJwtException -> throw RoomescapeException(ErrorType.UNSUPPORTED_TOKEN, HttpStatus.UNAUTHORIZED) throw AuthException(AuthErrorCode.INVALID_TOKEN)
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)
}
} }
} }

View File

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