From 67b612d4af1ad9882a95cd7db0025ef0c7633089 Mon Sep 17 00:00:00 2001 From: sgibb Date: Mon, 11 Mar 2024 15:32:58 -0400 Subject: [PATCH] Moves x-forwarded filter properties to correct namespace. From spring.cloud.gateway.x-forwarded to spring.cloud.gateway.mvc.x-forwarded-request-headers-filter. Deprecates the later. The XForwardedRequestHeadersFilter bean is conditional on the x-forwarded-request-headers-filter.enabled property. Fixes gh-3238 --- .../GatewayServerMvcAutoConfiguration.java | 13 +- .../XForwardedRequestHeadersFilter.java | 60 +++++++ ...rwardedRequestHeadersFilterProperties.java | 162 ++++++++++++++++++ 3 files changed, 233 insertions(+), 2 deletions(-) create mode 100644 spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/XForwardedRequestHeadersFilterProperties.java diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/GatewayServerMvcAutoConfiguration.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/GatewayServerMvcAutoConfiguration.java index 5d31f72d3f..87a7ee81b3 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/GatewayServerMvcAutoConfiguration.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/GatewayServerMvcAutoConfiguration.java @@ -40,6 +40,7 @@ import org.springframework.cloud.gateway.server.mvc.filter.TransferEncodingNormalizationRequestHeadersFilter; import org.springframework.cloud.gateway.server.mvc.filter.WeightCalculatorFilter; import org.springframework.cloud.gateway.server.mvc.filter.XForwardedRequestHeadersFilter; +import org.springframework.cloud.gateway.server.mvc.filter.XForwardedRequestHeadersFilterProperties; import org.springframework.cloud.gateway.server.mvc.handler.ProxyExchange; import org.springframework.cloud.gateway.server.mvc.handler.ProxyExchangeHandlerFunction; import org.springframework.cloud.gateway.server.mvc.handler.RestClientProxyExchange; @@ -169,8 +170,16 @@ public WeightCalculatorFilter weightCalculatorFilter() { @Bean @ConditionalOnMissingBean - public XForwardedRequestHeadersFilter xForwardedRequestHeadersFilter() { - return new XForwardedRequestHeadersFilter(); + @ConditionalOnProperty(prefix = XForwardedRequestHeadersFilterProperties.PREFIX, name = ".enabled", + matchIfMissing = true) + public XForwardedRequestHeadersFilter xForwardedRequestHeadersFilter( + XForwardedRequestHeadersFilterProperties props) { + return new XForwardedRequestHeadersFilter(props); + } + + @Bean + public XForwardedRequestHeadersFilterProperties xForwardedRequestHeadersFilterProperties() { + return new XForwardedRequestHeadersFilterProperties(); } } diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/XForwardedRequestHeadersFilter.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/XForwardedRequestHeadersFilter.java index c77807cab7..8dee49db4c 100644 --- a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/XForwardedRequestHeadersFilter.java +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/XForwardedRequestHeadersFilter.java @@ -23,12 +23,16 @@ import java.util.Map; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.boot.context.properties.DeprecatedConfigurationProperty; +import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.core.Ordered; import org.springframework.http.HttpHeaders; import org.springframework.util.ObjectUtils; import org.springframework.util.StringUtils; import org.springframework.web.servlet.function.ServerRequest; +import static org.springframework.cloud.gateway.server.mvc.filter.XForwardedRequestHeadersFilterProperties.PREFIX; + @ConfigurationProperties("spring.cloud.gateway.x-forwarded") public class XForwardedRequestHeadersFilter implements HttpHeadersFilter.RequestHttpHeadersFilter, Ordered { @@ -95,55 +99,97 @@ public class XForwardedRequestHeadersFilter implements HttpHeadersFilter.Request /** If appending X-Forwarded-Prefix as a list is enabled. */ private boolean prefixAppend = true; + @Deprecated + public XForwardedRequestHeadersFilter() { + this(new XForwardedRequestHeadersFilterProperties()); + } + + public XForwardedRequestHeadersFilter(XForwardedRequestHeadersFilterProperties props) { + // TODO: remove individual properties in 4.2.0 + // this.properties = properties; + PropertyMapper map = PropertyMapper.get(); + map.from(props::getOrder).to(this::setOrder); + map.from(props::isEnabled).to(this::setEnabled); + map.from(props::isForEnabled).to(this::setForEnabled); + map.from(props::isHostEnabled).to(this::setHostEnabled); + map.from(props::isPortEnabled).to(this::setPortEnabled); + map.from(props::isProtoEnabled).to(this::setProtoEnabled); + map.from(props::isPrefixEnabled).to(this::setPrefixEnabled); + map.from(props::isForAppend).to(this::setForAppend); + map.from(props::isHostAppend).to(this::setHostAppend); + map.from(props::isPortAppend).to(this::setPortAppend); + map.from(props::isProtoAppend).to(this::setProtoAppend); + map.from(props::isPrefixAppend).to(this::setPrefixAppend); + } + + @DeprecatedConfigurationProperty(replacement = PREFIX + ".order") @Override public int getOrder() { return this.order; } + @Deprecated public void setOrder(int order) { this.order = order; } + @Deprecated + @DeprecatedConfigurationProperty(replacement = PREFIX + ".enabled") public boolean isEnabled() { return enabled; } + @Deprecated public void setEnabled(boolean enabled) { this.enabled = enabled; } + @Deprecated + @DeprecatedConfigurationProperty(replacement = PREFIX + ".for-enabled") public boolean isForEnabled() { return forEnabled; } + @Deprecated public void setForEnabled(boolean forEnabled) { this.forEnabled = forEnabled; } + @Deprecated + @DeprecatedConfigurationProperty(replacement = PREFIX + ".host-enabled") public boolean isHostEnabled() { return hostEnabled; } + @Deprecated public void setHostEnabled(boolean hostEnabled) { this.hostEnabled = hostEnabled; } + @Deprecated + @DeprecatedConfigurationProperty(replacement = PREFIX + ".port-enabled") public boolean isPortEnabled() { return portEnabled; } + @Deprecated public void setPortEnabled(boolean portEnabled) { this.portEnabled = portEnabled; } + @Deprecated + @DeprecatedConfigurationProperty(replacement = PREFIX + ".proto-enabled") public boolean isProtoEnabled() { return protoEnabled; } + @Deprecated public void setProtoEnabled(boolean protoEnabled) { this.protoEnabled = protoEnabled; } + @Deprecated + @DeprecatedConfigurationProperty(replacement = PREFIX + ".prefix-enabled") public boolean isPrefixEnabled() { return prefixEnabled; } @@ -152,30 +198,41 @@ public void setPrefixEnabled(boolean prefixEnabled) { this.prefixEnabled = prefixEnabled; } + @Deprecated + @DeprecatedConfigurationProperty(replacement = PREFIX + ".for-append") public boolean isForAppend() { return forAppend; } + @Deprecated public void setForAppend(boolean forAppend) { this.forAppend = forAppend; } + @Deprecated + @DeprecatedConfigurationProperty(replacement = PREFIX + ".host-append") public boolean isHostAppend() { return hostAppend; } + @Deprecated public void setHostAppend(boolean hostAppend) { this.hostAppend = hostAppend; } + @Deprecated + @DeprecatedConfigurationProperty(replacement = PREFIX + ".port-append") public boolean isPortAppend() { return portAppend; } + @Deprecated public void setPortAppend(boolean portAppend) { this.portAppend = portAppend; } + @Deprecated + @DeprecatedConfigurationProperty(replacement = PREFIX + ".proto-append") public boolean isProtoAppend() { return protoAppend; } @@ -184,10 +241,13 @@ public void setProtoAppend(boolean protoAppend) { this.protoAppend = protoAppend; } + @Deprecated + @DeprecatedConfigurationProperty(replacement = PREFIX + ".prefix-append") public boolean isPrefixAppend() { return prefixAppend; } + @Deprecated public void setPrefixAppend(boolean prefixAppend) { this.prefixAppend = prefixAppend; } diff --git a/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/XForwardedRequestHeadersFilterProperties.java b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/XForwardedRequestHeadersFilterProperties.java new file mode 100644 index 0000000000..eef0aba0a4 --- /dev/null +++ b/spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/filter/XForwardedRequestHeadersFilterProperties.java @@ -0,0 +1,162 @@ +/* + * Copyright 2013-2020 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.gateway.server.mvc.filter; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.gateway.server.mvc.config.GatewayMvcProperties; + +@ConfigurationProperties(XForwardedRequestHeadersFilterProperties.PREFIX) +public class XForwardedRequestHeadersFilterProperties { + + /** + * XForwardedRequestHeadersFilterProperties prefix. + */ + public static final String PREFIX = GatewayMvcProperties.PREFIX + ".x-forwarded-request-headers-filter"; + + /** The order of the XForwardedHeadersFilter. */ + private int order = 0; + + /** If the XForwardedHeadersFilter is enabled. */ + private boolean enabled = true; + + /** If X-Forwarded-For is enabled. */ + private boolean forEnabled = true; + + /** If X-Forwarded-Host is enabled. */ + private boolean hostEnabled = true; + + /** If X-Forwarded-Port is enabled. */ + private boolean portEnabled = true; + + /** If X-Forwarded-Proto is enabled. */ + private boolean protoEnabled = true; + + /** If X-Forwarded-Prefix is enabled. */ + private boolean prefixEnabled = true; + + /** If appending X-Forwarded-For as a list is enabled. */ + private boolean forAppend = true; + + /** If appending X-Forwarded-Host as a list is enabled. */ + private boolean hostAppend = true; + + /** If appending X-Forwarded-Port as a list is enabled. */ + private boolean portAppend = true; + + /** If appending X-Forwarded-Proto as a list is enabled. */ + private boolean protoAppend = true; + + /** If appending X-Forwarded-Prefix as a list is enabled. */ + private boolean prefixAppend = true; + + public int getOrder() { + return this.order; + } + + public void setOrder(int order) { + this.order = order; + } + + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public boolean isForEnabled() { + return forEnabled; + } + + public void setForEnabled(boolean forEnabled) { + this.forEnabled = forEnabled; + } + + public boolean isHostEnabled() { + return hostEnabled; + } + + public void setHostEnabled(boolean hostEnabled) { + this.hostEnabled = hostEnabled; + } + + public boolean isPortEnabled() { + return portEnabled; + } + + public void setPortEnabled(boolean portEnabled) { + this.portEnabled = portEnabled; + } + + public boolean isProtoEnabled() { + return protoEnabled; + } + + public void setProtoEnabled(boolean protoEnabled) { + this.protoEnabled = protoEnabled; + } + + public boolean isPrefixEnabled() { + return prefixEnabled; + } + + public void setPrefixEnabled(boolean prefixEnabled) { + this.prefixEnabled = prefixEnabled; + } + + public boolean isForAppend() { + return forAppend; + } + + public void setForAppend(boolean forAppend) { + this.forAppend = forAppend; + } + + public boolean isHostAppend() { + return hostAppend; + } + + public void setHostAppend(boolean hostAppend) { + this.hostAppend = hostAppend; + } + + public boolean isPortAppend() { + return portAppend; + } + + public void setPortAppend(boolean portAppend) { + this.portAppend = portAppend; + } + + public boolean isProtoAppend() { + return protoAppend; + } + + public void setProtoAppend(boolean protoAppend) { + this.protoAppend = protoAppend; + } + + public boolean isPrefixAppend() { + return prefixAppend; + } + + public void setPrefixAppend(boolean prefixAppend) { + this.prefixAppend = prefixAppend; + } + +}