Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fs-server支持客户端会话管理 #1275 #1312

Merged
merged 1 commit into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -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<ServerWebExchange> {
Expand Down
Loading