From f4d39871fe308336555ba4d436519b6098b73771 Mon Sep 17 00:00:00 2001 From: yaoxuwan Date: Fri, 20 Oct 2023 15:13:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20fs-server=E6=94=AF=E6=8C=81=E5=AE=A2?= =?UTF-8?q?=E6=88=B7=E7=AB=AF=E4=BC=9A=E8=AF=9D=E7=AE=A1=E7=90=86=20#1275?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../context/ReactiveRequestContextHolder.kt | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/backend/fs/boot-fs-server/src/main/kotlin/com/tencent/bkrepo/fs/server/context/ReactiveRequestContextHolder.kt b/src/backend/fs/boot-fs-server/src/main/kotlin/com/tencent/bkrepo/fs/server/context/ReactiveRequestContextHolder.kt index a1aac374de..21ec05cd42 100644 --- a/src/backend/fs/boot-fs-server/src/main/kotlin/com/tencent/bkrepo/fs/server/context/ReactiveRequestContextHolder.kt +++ b/src/backend/fs/boot-fs-server/src/main/kotlin/com/tencent/bkrepo/fs/server/context/ReactiveRequestContextHolder.kt @@ -27,12 +27,14 @@ package com.tencent.bkrepo.fs.server.context +import com.tencent.bkrepo.common.api.constant.HttpHeaders import com.tencent.bkrepo.common.api.constant.StringPool import kotlinx.coroutines.reactor.awaitSingle import org.springframework.http.server.reactive.ServerHttpRequest import org.springframework.http.server.reactive.ServerHttpResponse import org.springframework.web.server.ServerWebExchange import reactor.core.publisher.Mono +import java.util.StringTokenizer object ReactiveRequestContextHolder { @@ -58,7 +60,24 @@ object ReactiveRequestContextHolder { suspend fun getClientAddress(): String { val request = getRequest() - return request.remoteAddress?.address?.hostAddress ?: StringPool.UNKNOWN + val headers = request.headers + var address = headers[HttpHeaders.X_FORWARDED_FOR]?.first() + address = if (address.isNullOrBlank()) { + headers[HttpHeaders.X_REAL_IP]?.first() + } else { + StringTokenizer(address, StringPool.COMMA).nextToken() + } + if (address.isNullOrBlank()) { + address = headers[HttpHeaders.PROXY_CLIENT_IP]?.first() + } + if (address.isNullOrBlank()) { + address = request.remoteAddress?.address?.hostAddress + } + if (address.isNullOrBlank()) { + address = StringPool.UNKNOWN + } + // 对于通过多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割 + return address } fun getWebExchangeMono(): Mono {