From 1607cb457b2c2b9e1c505fc3ea75f9d0932d91b7 Mon Sep 17 00:00:00 2001 From: devxb Date: Sun, 28 Apr 2024 12:56:53 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20internal=20api=20interceptor=EA=B0=80=20?= =?UTF-8?q?X-Forwarded-For=EC=9D=B4=20=EC=97=86=EC=96=B4=EB=8F=84=20?= =?UTF-8?q?=EC=88=98=ED=96=89=EB=90=98=EB=8F=84=EB=A1=9D=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gitanimals/render/controller/PersonaController.kt | 5 +++++ .../controller/interceptor/InternalApiInterceptor.kt | 11 +++++------ .../render/controller/response/ErrorResponse.kt | 11 +++++++++++ .../org/gitanimals/render/infra/RestIdentityApi.kt | 7 +++++-- 4 files changed, 26 insertions(+), 8 deletions(-) create mode 100644 src/main/kotlin/org/gitanimals/render/controller/response/ErrorResponse.kt diff --git a/src/main/kotlin/org/gitanimals/render/controller/PersonaController.kt b/src/main/kotlin/org/gitanimals/render/controller/PersonaController.kt index 0babd28..bcd4f0e 100644 --- a/src/main/kotlin/org/gitanimals/render/controller/PersonaController.kt +++ b/src/main/kotlin/org/gitanimals/render/controller/PersonaController.kt @@ -2,6 +2,7 @@ package org.gitanimals.render.controller import org.gitanimals.render.app.UserFacade import org.gitanimals.render.controller.request.AddPersonaRequest +import org.gitanimals.render.controller.response.ErrorResponse import org.gitanimals.render.controller.response.PersonaResponse import org.gitanimals.render.controller.response.UserResponse import org.gitanimals.render.domain.UserService @@ -48,4 +49,8 @@ class PersonaController( return PersonaResponse(persona.id, persona.type, persona.level) } + + @ExceptionHandler(IllegalArgumentException::class) + fun handleIllegalArgumentException(exception: IllegalArgumentException): ErrorResponse = + ErrorResponse.from(exception) } diff --git a/src/main/kotlin/org/gitanimals/render/controller/interceptor/InternalApiInterceptor.kt b/src/main/kotlin/org/gitanimals/render/controller/interceptor/InternalApiInterceptor.kt index 8efddff..8441dc2 100644 --- a/src/main/kotlin/org/gitanimals/render/controller/interceptor/InternalApiInterceptor.kt +++ b/src/main/kotlin/org/gitanimals/render/controller/interceptor/InternalApiInterceptor.kt @@ -13,8 +13,7 @@ class InternalApiInterceptor( response: HttpServletResponse, handler: Any, ): Boolean { - val ip = extractIp(request) - return whiteIps.contains(ip) + return whiteIps.contains(extractIp(request)) } private fun extractIp(request: HttpServletRequest): String { @@ -24,15 +23,15 @@ class InternalApiInterceptor( "X-Real-IP", "X-RealIP", "REMOTE_ADDR" ) - var ip: String = request.getHeader("X-Forwarded-For") + var ip: String? = request.getHeader("X-Forwarded-For") for (header in headers) { - if (ip.isEmpty() || "unknown".equals(ip, ignoreCase = true)) { + if (ip.isNullOrEmpty() || "unknown".equals(ip, ignoreCase = true)) { ip = request.getHeader(header) } } - if (ip.isEmpty() || "unknown".equals(ip, ignoreCase = true)) { + if (ip.isNullOrEmpty() || "unknown".equals(ip, ignoreCase = true)) { ip = request.remoteAddr } @@ -40,6 +39,6 @@ class InternalApiInterceptor( ip = "127.0.0.1" } - return ip + return ip ?: throw IllegalStateException("Cannot extract ip") } } diff --git a/src/main/kotlin/org/gitanimals/render/controller/response/ErrorResponse.kt b/src/main/kotlin/org/gitanimals/render/controller/response/ErrorResponse.kt new file mode 100644 index 0000000..01f4a5b --- /dev/null +++ b/src/main/kotlin/org/gitanimals/render/controller/response/ErrorResponse.kt @@ -0,0 +1,11 @@ +package org.gitanimals.render.controller.response + +data class ErrorResponse( + val message: String, +) { + + companion object { + fun from(exception: Exception): ErrorResponse = + ErrorResponse(exception.message ?: exception.localizedMessage) + } +} diff --git a/src/main/kotlin/org/gitanimals/render/infra/RestIdentityApi.kt b/src/main/kotlin/org/gitanimals/render/infra/RestIdentityApi.kt index 512bc1d..9b394c5 100644 --- a/src/main/kotlin/org/gitanimals/render/infra/RestIdentityApi.kt +++ b/src/main/kotlin/org/gitanimals/render/infra/RestIdentityApi.kt @@ -15,8 +15,11 @@ class RestIdentityApi : IdentityApi { .uri("/users") .header(HttpHeaders.AUTHORIZATION, token) .exchange { _, response -> - response.bodyTo(IdentityApi.UserResponse::class.java) - ?: throw IllegalArgumentException("Unauthorized user") + runCatching { + response.bodyTo(IdentityApi.UserResponse::class.java) + }.getOrElse { + throw IllegalArgumentException("Unauthorized user") + } } } }