diff --git a/fizz-bootstrap/src/main/resources/application.yml b/fizz-bootstrap/src/main/resources/application.yml index 7dd11e5..9bd105e 100644 --- a/fizz-bootstrap/src/main/resources/application.yml +++ b/fizz-bootstrap/src/main/resources/application.yml @@ -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: diff --git a/fizz-core/src/main/java/com/fizzgate/config/SystemConfig.java b/fizz-core/src/main/java/com/fizzgate/config/SystemConfig.java index d3f992c..12768cb 100644 --- a/fizz-core/src/main/java/com/fizzgate/config/SystemConfig.java +++ b/fizz-core/src/main/java/com/fizzgate/config/SystemConfig.java @@ -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() { diff --git a/fizz-core/src/main/java/com/fizzgate/filter/RouteFilter.java b/fizz-core/src/main/java/com/fizzgate/filter/RouteFilter.java index d88b018..65d9863 100644 --- a/fizz-core/src/main/java/com/fizzgate/filter/RouteFilter.java +++ b/fizz-core/src/main/java/com/fizzgate/filter/RouteFilter.java @@ -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) { diff --git a/fizz-core/src/main/java/com/fizzgate/proxy/FizzWebClient.java b/fizz-core/src/main/java/com/fizzgate/proxy/FizzWebClient.java index f9c4dd9..a29fc01 100644 --- a/fizz-core/src/main/java/com/fizzgate/proxy/FizzWebClient.java +++ b/fizz-core/src/main/java/com/fizzgate/proxy/FizzWebClient.java @@ -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 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); } } ); diff --git a/fizz-core/src/main/java/com/fizzgate/util/WebUtils.java b/fizz-core/src/main/java/com/fizzgate/util/WebUtils.java index 79e2b32..7690831 100644 --- a/fizz-core/src/main/java/com/fizzgate/util/WebUtils.java +++ b/fizz-core/src/main/java/com/fizzgate/util/WebUtils.java @@ -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 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 values = headers.get(xForwardedFor); + if (CollectionUtils.isEmpty(values)) { + String originIp = getOriginIp(exchange); + headers.add(xForwardedFor, originIp); + } + } }