Support X-Forwarded-For header

This commit is contained in:
hongqiaowei
2023-02-11 12:06:09 +08:00
parent f409c0db44
commit 581657cc74
5 changed files with 40 additions and 11 deletions

View File

@@ -126,7 +126,9 @@ fizz:
fast-fail-when-registry-center-down: false
web-client:
x-forwarded-for: false
x-forwarded-for:
enable: true # default
append-gateway-ip: true # default
# dedicated-line:
# server:

View File

@@ -21,7 +21,6 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
@@ -133,11 +132,18 @@ public class SystemConfig {
@Value("${fizz.fast-fail-when-registry-center-down:false}")
private boolean fastFailWhenRegistryCenterDown;
@Value("${fizz.web-client.x-forwarded-for:false}")
private boolean fizzWebClientXForwardedFor;
@Value("${fizz.web-client.x-forwarded-for.enable:true}")
private boolean fizzWebClientXForwardedForEnable;
public boolean isFizzWebClientXForwardedFor() {
return fizzWebClientXForwardedFor;
@Value("${fizz.web-client.x-forwarded-for.append-gateway-ip:true}")
private boolean fizzWebClientXForwardedForAppendGatewayIp;
public boolean isFizzWebClientXForwardedForAppendGatewayIp() {
return fizzWebClientXForwardedForAppendGatewayIp;
}
public boolean isFizzWebClientXForwardedForEnable() {
return fizzWebClientXForwardedForEnable;
}
public boolean isFastFailWhenRegistryCenterDown() {

View File

@@ -108,6 +108,7 @@ public class RouteFilter extends FizzWebFilter {
if (route != null && route.type != ApiConfig.Type.DUBBO) {
hdrs = WebUtils.mergeAppendHeaders(exchange);
WebUtils.setXForwardedFor(exchange, hdrs);
}
if (route == null) {

View File

@@ -26,6 +26,7 @@ import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ClientResponse;
@@ -51,6 +52,8 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static com.google.common.net.HttpHeaders.X_FORWARDED_FOR;
/**
* @author hongqiaowei
*/
@@ -239,8 +242,16 @@ public class FizzWebClient {
);
}
setHostHeader(uri, hdrs);
if (systemConfig.isFizzWebClientXForwardedFor()) {
hdrs.add(com.google.common.net.HttpHeaders.X_FORWARDED_FOR, NetworkUtils.getServerIp());
if (systemConfig.isFizzWebClientXForwardedForEnable()) {
List<String> values = hdrs.get(X_FORWARDED_FOR);
/* if (CollectionUtils.isEmpty(values)) {
hdrs.add(X_FORWARDED_FOR, WebUtils.getOriginIp(null));
} */
if (systemConfig.isFizzWebClientXForwardedForAppendGatewayIp()) {
hdrs.add(X_FORWARDED_FOR, NetworkUtils.getServerIp());
}
} else {
hdrs.remove(X_FORWARDED_FOR);
}
}
);

View File

@@ -28,6 +28,7 @@ import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.client.ClientResponse;
import org.springframework.web.server.ServerWebExchange;
@@ -67,7 +68,7 @@ public abstract class WebUtils {
private static final String clientService = "cs@";
private static final String xForwardedFor = "X-FORWARDED-FOR";
private static final String xForwardedFor = "X-Forwarded-For";
private static final String unknown = "unknown";
@@ -527,9 +528,9 @@ public abstract class WebUtils {
public static HttpHeaders mergeAppendHeaders(ServerWebExchange exchange) {
ServerHttpRequest req = exchange.getRequest();
Map<String, String> appendHeaders = getAppendHeaders(exchange);
if (appendHeaders.isEmpty()) {
/* if (appendHeaders.isEmpty()) {
return req.getHeaders();
}
} */
HttpHeaders hdrs = new HttpHeaders();
req.getHeaders().forEach(
(h, vs) -> {
@@ -963,4 +964,12 @@ public abstract class WebUtils {
content = StringUtils.isBlank(content) ? Consts.S.EMPTY : content;
return buildDirectResponseAndBindContext(exchange, httpStatus, headers, content);
}
public static void setXForwardedFor(ServerWebExchange exchange, HttpHeaders headers) {
List<String> values = headers.get(xForwardedFor);
if (CollectionUtils.isEmpty(values)) {
String originIp = getOriginIp(exchange);
headers.add(xForwardedFor, originIp);
}
}
}