Skip to content

Commit

Permalink
Merge pull request #1312 from yaoxuwan/issue_1275
Browse files Browse the repository at this point in the history
feat: fs-server支持客户端会话管理 #1275
  • Loading branch information
owenlxu authored Oct 20, 2023
2 parents 9f5d998 + f4d3987 commit 89f8db6
Showing 1 changed file with 20 additions and 1 deletion.
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

0 comments on commit 89f8db6

Please sign in to comment.