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 {