From c35d16a5737a96d12004e0a3b5cc76b1a44fe7b9 Mon Sep 17 00:00:00 2001 From: DongGeon0908 Date: Wed, 24 Jul 2024 21:47:17 +0900 Subject: [PATCH] fix: JwtConfig --- .../com/hero/alignlab/config/jwt/JwtConfig.kt | 20 ----------- .../hero/alignlab/config/jwt/JwtProperties.kt | 35 +++++++++++++++++++ .../auth/application/JwtTokenService.kt | 30 ++++++++-------- 3 files changed, 49 insertions(+), 36 deletions(-) delete mode 100644 src/main/kotlin/com/hero/alignlab/config/jwt/JwtConfig.kt create mode 100644 src/main/kotlin/com/hero/alignlab/config/jwt/JwtProperties.kt diff --git a/src/main/kotlin/com/hero/alignlab/config/jwt/JwtConfig.kt b/src/main/kotlin/com/hero/alignlab/config/jwt/JwtConfig.kt deleted file mode 100644 index 056da5d..0000000 --- a/src/main/kotlin/com/hero/alignlab/config/jwt/JwtConfig.kt +++ /dev/null @@ -1,20 +0,0 @@ -package com.hero.alignlab.config.jwt - -import jakarta.validation.constraints.NotBlank -import org.springframework.boot.context.properties.ConfigurationProperties -import org.springframework.boot.context.properties.ConfigurationPropertiesBinding -import org.springframework.context.annotation.Configuration - -@Configuration -@ConfigurationProperties(prefix = "auth.jwt") -@ConfigurationPropertiesBinding -data class JwtConfig( - @field:NotBlank - var secret: String = "", - @field:NotBlank - var accessExp: Int = 0, - @field:NotBlank - var refreshExp: Int = 0, - val issuer: String = "hero-alignlab-api", - val audience: String = "hero-alignlab-api", -) diff --git a/src/main/kotlin/com/hero/alignlab/config/jwt/JwtProperties.kt b/src/main/kotlin/com/hero/alignlab/config/jwt/JwtProperties.kt new file mode 100644 index 0000000..ee74eac --- /dev/null +++ b/src/main/kotlin/com/hero/alignlab/config/jwt/JwtProperties.kt @@ -0,0 +1,35 @@ +package com.hero.alignlab.config.jwt + +import com.hero.alignlab.domain.auth.application.JwtTokenService +import io.github.oshai.kotlinlogging.KotlinLogging +import jakarta.validation.constraints.NotBlank +import org.springframework.boot.context.properties.ConfigurationProperties +import org.springframework.boot.context.properties.EnableConfigurationProperties +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration + +@Configuration +@EnableConfigurationProperties(JwtConfig.JwtProperties::class) +class JwtConfig { + private val logger = KotlinLogging.logger {} + + @ConfigurationProperties(prefix = "auth.jwt") + data class JwtProperties( + @field:NotBlank + var secret: String = "", + @field:NotBlank + var accessExp: Int = 0, + @field:NotBlank + var refreshExp: Int = 0, + val issuer: String = "hero-alignlab-api", + val audience: String = "hero-alignlab-api", + ) + + @Bean + fun jwtTokenService(jwtProperties: JwtProperties): JwtTokenService { + logger.info { "initialized jwtTokenService. $jwtProperties" } + return JwtTokenService(jwtProperties) + } +} + + diff --git a/src/main/kotlin/com/hero/alignlab/domain/auth/application/JwtTokenService.kt b/src/main/kotlin/com/hero/alignlab/domain/auth/application/JwtTokenService.kt index 18a7424..9e59df1 100644 --- a/src/main/kotlin/com/hero/alignlab/domain/auth/application/JwtTokenService.kt +++ b/src/main/kotlin/com/hero/alignlab/domain/auth/application/JwtTokenService.kt @@ -3,51 +3,49 @@ package com.hero.alignlab.domain.auth.application import com.auth0.jwt.JWT import com.auth0.jwt.algorithms.Algorithm import com.fasterxml.jackson.module.kotlin.readValue +import com.hero.alignlab.common.extension.decodeBase64 +import com.hero.alignlab.common.extension.mapper +import com.hero.alignlab.common.extension.toInstant import com.hero.alignlab.config.jwt.JwtConfig import com.hero.alignlab.domain.auth.model.AuthUserToken import com.hero.alignlab.domain.auth.model.AuthUserTokenPayload import com.hero.alignlab.exception.ErrorCode import com.hero.alignlab.exception.InvalidTokenException -import com.hero.alignlab.common.extension.decodeBase64 -import com.hero.alignlab.common.extension.mapper -import com.hero.alignlab.common.extension.toInstant import io.github.oshai.kotlinlogging.KotlinLogging -import org.springframework.stereotype.Service import reactor.core.publisher.Mono import java.time.LocalDateTime import java.util.* private const val ACCESS_TOKEN = "accessToken" -@Service class JwtTokenService( - private val jwtConfig: JwtConfig, + private val jwtProperties: JwtConfig.JwtProperties, ) { private val logger = KotlinLogging.logger {} private val accessJwtVerifier = JWT - .require(Algorithm.HMAC256(jwtConfig.secret)) - .withIssuer(jwtConfig.issuer) - .withAudience(jwtConfig.audience) + .require(Algorithm.HMAC256(jwtProperties.secret)) + .withIssuer(jwtProperties.issuer) + .withAudience(jwtProperties.audience) .withClaim("type", ACCESS_TOKEN) .build() private val accessJwtVerifierWithExtendedExpiredAt = JWT - .require(Algorithm.HMAC256(jwtConfig.secret)) - .withIssuer(jwtConfig.issuer) - .withAudience(jwtConfig.audience) + .require(Algorithm.HMAC256(jwtProperties.secret)) + .withIssuer(jwtProperties.issuer) + .withAudience(jwtProperties.audience) .withClaim("type", ACCESS_TOKEN) - .acceptExpiresAt(jwtConfig.refreshExp.toLong()) + .acceptExpiresAt(jwtProperties.refreshExp.toLong()) .build() fun createToken(id: Long, tokenExpiredAt: LocalDateTime): String { return JWT.create().apply { - this.withIssuer(jwtConfig.issuer) - this.withAudience(jwtConfig.audience) + this.withIssuer(jwtProperties.issuer) + this.withAudience(jwtProperties.audience) this.withClaim("id", id) this.withClaim("type", ACCESS_TOKEN) this.withExpiresAt(Date.from(tokenExpiredAt.toInstant())) - }.sign(Algorithm.HMAC256(jwtConfig.secret)) + }.sign(Algorithm.HMAC256(jwtProperties.secret)) } fun verifyToken(token: AuthUserToken): AuthUserTokenPayload {