From dc17316856f735278a8f76303b75cb2835a3a4e9 Mon Sep 17 00:00:00 2001 From: pricelees Date: Wed, 1 Oct 2025 10:51:58 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20Base64=20=ED=98=95=EC=8B=9D=EC=9D=98=20?= =?UTF-8?q?=EC=8B=9C=ED=81=AC=EB=A6=BF=ED=82=A4=EB=A5=BC=20=EA=B2=80?= =?UTF-8?q?=EC=A6=9D=ED=95=98=EB=8A=94=20=EC=9D=B8=ED=84=B0=EC=85=89?= =?UTF-8?q?=ED=84=B0=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/supports/SecretKeyInterceptor.kt | 39 +++++++++++++++++++ .../web/supports/TosspayMvcConfig.kt | 15 +++++++ 2 files changed, 54 insertions(+) create mode 100644 tosspay-mock/src/main/kotlin/com/sangdol/tosspaymock/web/supports/SecretKeyInterceptor.kt create mode 100644 tosspay-mock/src/main/kotlin/com/sangdol/tosspaymock/web/supports/TosspayMvcConfig.kt diff --git a/tosspay-mock/src/main/kotlin/com/sangdol/tosspaymock/web/supports/SecretKeyInterceptor.kt b/tosspay-mock/src/main/kotlin/com/sangdol/tosspaymock/web/supports/SecretKeyInterceptor.kt new file mode 100644 index 00000000..9608478d --- /dev/null +++ b/tosspay-mock/src/main/kotlin/com/sangdol/tosspaymock/web/supports/SecretKeyInterceptor.kt @@ -0,0 +1,39 @@ +package com.sangdol.tosspaymock.web.supports + +import com.sangdol.tosspaymock.exception.TosspayException +import com.sangdol.tosspaymock.exception.code.TosspayConfirmErrorCode +import jakarta.servlet.http.HttpServletRequest +import jakarta.servlet.http.HttpServletResponse +import org.springframework.http.HttpHeaders +import org.springframework.stereotype.Component +import org.springframework.web.method.HandlerMethod +import org.springframework.web.servlet.HandlerInterceptor +import java.util.* + +@Component +class SecretKeyInterceptor : HandlerInterceptor { + + companion object { + val basicAuthRegex = Regex("Basic (.*)") + } + + override fun preHandle( + request: HttpServletRequest, + response: HttpServletResponse, + handler: Any + ): Boolean { + if (handler !is HandlerMethod) return true + + val basicAuthSecretKey: String = request.getHeader(HttpHeaders.AUTHORIZATION) + ?: throw TosspayException(TosspayConfirmErrorCode.INVALID_API_KEY) + + return try { + val secretKey = basicAuthRegex.find(basicAuthSecretKey)!!.groupValues[1] + Base64.getDecoder().decode(secretKey) + + true + } catch (_: Exception) { + throw TosspayException(TosspayConfirmErrorCode.UNAUTHORIZED_KEY) + } + } +} diff --git a/tosspay-mock/src/main/kotlin/com/sangdol/tosspaymock/web/supports/TosspayMvcConfig.kt b/tosspay-mock/src/main/kotlin/com/sangdol/tosspaymock/web/supports/TosspayMvcConfig.kt new file mode 100644 index 00000000..952b49f8 --- /dev/null +++ b/tosspay-mock/src/main/kotlin/com/sangdol/tosspaymock/web/supports/TosspayMvcConfig.kt @@ -0,0 +1,15 @@ +package com.sangdol.tosspaymock.web.supports + +import org.springframework.context.annotation.Configuration +import org.springframework.web.servlet.config.annotation.InterceptorRegistry +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer + +@Configuration +class TosspayMvcConfig( + private val secretKeyInterceptor: SecretKeyInterceptor +) : WebMvcConfigurer { + + override fun addInterceptors(registry: InterceptorRegistry) { + registry.addInterceptor(secretKeyInterceptor) + } +}