Support X-Forwarded-For header
This commit is contained in:
@@ -126,7 +126,9 @@ fizz:
|
|||||||
fast-fail-when-registry-center-down: false
|
fast-fail-when-registry-center-down: false
|
||||||
|
|
||||||
web-client:
|
web-client:
|
||||||
x-forwarded-for: false
|
x-forwarded-for:
|
||||||
|
enable: true # default
|
||||||
|
append-gateway-ip: true # default
|
||||||
|
|
||||||
# dedicated-line:
|
# dedicated-line:
|
||||||
# server:
|
# server:
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ import org.apache.commons.lang3.StringUtils;
|
|||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.util.ObjectUtils;
|
import org.springframework.util.ObjectUtils;
|
||||||
|
|
||||||
@@ -133,11 +132,18 @@ public class SystemConfig {
|
|||||||
@Value("${fizz.fast-fail-when-registry-center-down:false}")
|
@Value("${fizz.fast-fail-when-registry-center-down:false}")
|
||||||
private boolean fastFailWhenRegistryCenterDown;
|
private boolean fastFailWhenRegistryCenterDown;
|
||||||
|
|
||||||
@Value("${fizz.web-client.x-forwarded-for:false}")
|
@Value("${fizz.web-client.x-forwarded-for.enable:true}")
|
||||||
private boolean fizzWebClientXForwardedFor;
|
private boolean fizzWebClientXForwardedForEnable;
|
||||||
|
|
||||||
public boolean isFizzWebClientXForwardedFor() {
|
@Value("${fizz.web-client.x-forwarded-for.append-gateway-ip:true}")
|
||||||
return fizzWebClientXForwardedFor;
|
private boolean fizzWebClientXForwardedForAppendGatewayIp;
|
||||||
|
|
||||||
|
public boolean isFizzWebClientXForwardedForAppendGatewayIp() {
|
||||||
|
return fizzWebClientXForwardedForAppendGatewayIp;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isFizzWebClientXForwardedForEnable() {
|
||||||
|
return fizzWebClientXForwardedForEnable;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isFastFailWhenRegistryCenterDown() {
|
public boolean isFastFailWhenRegistryCenterDown() {
|
||||||
|
|||||||
@@ -108,6 +108,7 @@ public class RouteFilter extends FizzWebFilter {
|
|||||||
|
|
||||||
if (route != null && route.type != ApiConfig.Type.DUBBO) {
|
if (route != null && route.type != ApiConfig.Type.DUBBO) {
|
||||||
hdrs = WebUtils.mergeAppendHeaders(exchange);
|
hdrs = WebUtils.mergeAppendHeaders(exchange);
|
||||||
|
WebUtils.setXForwardedFor(exchange, hdrs);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (route == null) {
|
if (route == null) {
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ import org.springframework.http.HttpHeaders;
|
|||||||
import org.springframework.http.HttpMethod;
|
import org.springframework.http.HttpMethod;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.web.reactive.function.BodyInserter;
|
import org.springframework.web.reactive.function.BodyInserter;
|
||||||
import org.springframework.web.reactive.function.BodyInserters;
|
import org.springframework.web.reactive.function.BodyInserters;
|
||||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||||
@@ -51,6 +52,8 @@ import java.util.Arrays;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static com.google.common.net.HttpHeaders.X_FORWARDED_FOR;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author hongqiaowei
|
* @author hongqiaowei
|
||||||
*/
|
*/
|
||||||
@@ -239,8 +242,16 @@ public class FizzWebClient {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
setHostHeader(uri, hdrs);
|
setHostHeader(uri, hdrs);
|
||||||
if (systemConfig.isFizzWebClientXForwardedFor()) {
|
if (systemConfig.isFizzWebClientXForwardedForEnable()) {
|
||||||
hdrs.add(com.google.common.net.HttpHeaders.X_FORWARDED_FOR, NetworkUtils.getServerIp());
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -28,6 +28,7 @@ import org.springframework.http.MediaType;
|
|||||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||||
import org.springframework.lang.Nullable;
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||||
import org.springframework.web.server.ServerWebExchange;
|
import org.springframework.web.server.ServerWebExchange;
|
||||||
@@ -67,7 +68,7 @@ public abstract class WebUtils {
|
|||||||
|
|
||||||
private static final String clientService = "cs@";
|
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";
|
private static final String unknown = "unknown";
|
||||||
|
|
||||||
@@ -527,9 +528,9 @@ public abstract class WebUtils {
|
|||||||
public static HttpHeaders mergeAppendHeaders(ServerWebExchange exchange) {
|
public static HttpHeaders mergeAppendHeaders(ServerWebExchange exchange) {
|
||||||
ServerHttpRequest req = exchange.getRequest();
|
ServerHttpRequest req = exchange.getRequest();
|
||||||
Map<String, String> appendHeaders = getAppendHeaders(exchange);
|
Map<String, String> appendHeaders = getAppendHeaders(exchange);
|
||||||
if (appendHeaders.isEmpty()) {
|
/* if (appendHeaders.isEmpty()) {
|
||||||
return req.getHeaders();
|
return req.getHeaders();
|
||||||
}
|
} */
|
||||||
HttpHeaders hdrs = new HttpHeaders();
|
HttpHeaders hdrs = new HttpHeaders();
|
||||||
req.getHeaders().forEach(
|
req.getHeaders().forEach(
|
||||||
(h, vs) -> {
|
(h, vs) -> {
|
||||||
@@ -963,4 +964,12 @@ public abstract class WebUtils {
|
|||||||
content = StringUtils.isBlank(content) ? Consts.S.EMPTY : content;
|
content = StringUtils.isBlank(content) ? Consts.S.EMPTY : content;
|
||||||
return buildDirectResponseAndBindContext(exchange, httpStatus, headers, 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user