From 9ea11b375d7d20ca6bda410d7e651dee528cabcb Mon Sep 17 00:00:00 2001 From: sgibb Date: Tue, 19 Mar 2024 14:27:15 -0400 Subject: [PATCH] Adds properties to disable filters if needed Fixes gh-3310 --- .../GatewayServerMvcAutoConfiguration.java | 11 ++++ ...itional-spring-configuration-metadata.json | 36 +++++++++++ ...atewayServerMvcAutoConfigurationTests.java | 63 +++++++++++++++++++ 3 files changed, 110 insertions(+) create mode 100644 spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/GatewayServerMvcAutoConfigurationTests.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 062be411..a3e94fac 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 @@ -93,6 +93,7 @@ public class GatewayServerMvcAutoConfiguration { @Bean @ConditionalOnMissingBean + @ConditionalOnProperty(prefix = GatewayMvcProperties.PREFIX, name = "form-filter.enabled", matchIfMissing = true) public FormFilter formFilter() { return new FormFilter(); } @@ -156,30 +157,40 @@ public class GatewayServerMvcAutoConfiguration { @Bean @ConditionalOnMissingBean + @ConditionalOnProperty(prefix = GatewayMvcProperties.PREFIX, + name = "remove-content-length-request-headers-filter.enabled", matchIfMissing = true) public RemoveContentLengthRequestHeadersFilter removeContentLengthRequestHeadersFilter() { return new RemoveContentLengthRequestHeadersFilter(); } @Bean @ConditionalOnMissingBean + @ConditionalOnProperty(prefix = GatewayMvcProperties.PREFIX, + name = "remove-hop-by-hop-request-headers-filter.enabled", matchIfMissing = true) public RemoveHopByHopRequestHeadersFilter removeHopByHopRequestHeadersFilter() { return new RemoveHopByHopRequestHeadersFilter(); } @Bean @ConditionalOnMissingBean + @ConditionalOnProperty(prefix = GatewayMvcProperties.PREFIX, + name = "remove-hop-by-hop-response-headers-filter.enabled", matchIfMissing = true) public RemoveHopByHopResponseHeadersFilter removeHopByHopResponseHeadersFilter() { return new RemoveHopByHopResponseHeadersFilter(); } @Bean @ConditionalOnMissingBean + @ConditionalOnProperty(prefix = GatewayMvcProperties.PREFIX, + name = "transfer-encoding-normalization-request-headers-filter.enabled", matchIfMissing = true) public TransferEncodingNormalizationRequestHeadersFilter transferEncodingNormalizationRequestHeadersFilter() { return new TransferEncodingNormalizationRequestHeadersFilter(); } @Bean @ConditionalOnMissingBean + @ConditionalOnProperty(prefix = GatewayMvcProperties.PREFIX, name = "weight-calculator-filter.enabled", + matchIfMissing = true) public WeightCalculatorFilter weightCalculatorFilter() { return new WeightCalculatorFilter(); } diff --git a/spring-cloud-gateway-server-mvc/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-gateway-server-mvc/src/main/resources/META-INF/additional-spring-configuration-metadata.json index df60874b..ff054ce3 100644 --- a/spring-cloud-gateway-server-mvc/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-gateway-server-mvc/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -1,10 +1,46 @@ { "properties": [ + { + "name": "spring.cloud.gateway.mvc.form-filter.enabled", + "type": "java.lang.Boolean", + "description": "Enables the form-filter.", + "defaultValue": "true" + }, { "name": "spring.cloud.gateway.mvc.forwarded-request-headers-filter.enabled", "type": "java.lang.Boolean", "description": "Enables the forwarded-request-headers-filter.", "defaultValue": "true" + }, + { + "name": "spring.cloud.gateway.mvc.remove-content-length-request-headers-filter.enabled", + "type": "java.lang.Boolean", + "description": "Enables the remove-content-length-request-headers-filter.", + "defaultValue": "true" + }, + { + "name": "spring.cloud.gateway.mvc.remove-hop-by-hop-request-headers-filter.enabled", + "type": "java.lang.Boolean", + "description": "Enables the forwarded-request-headers-filter.", + "defaultValue": "true" + }, + { + "name": "spring.cloud.gateway.mvc.remove-hop-by-hop-response-headers-filter.enabled", + "type": "java.lang.Boolean", + "description": "Enables the forwarded-request-headers-filter.", + "defaultValue": "true" + }, + { + "name": "spring.cloud.gateway.mvc.transfer-encoding-normalization-request-headers-filter.enabled", + "type": "java.lang.Boolean", + "description": "Enables the transfer-encoding-normalization-request-headers-filter.", + "defaultValue": "true" + }, + { + "name": "spring.cloud.gateway.mvc.weight-calculator-filter.enabled", + "type": "java.lang.Boolean", + "description": "Enables the weight-calculator-filter.", + "defaultValue": "true" } ] } diff --git a/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/GatewayServerMvcAutoConfigurationTests.java b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/GatewayServerMvcAutoConfigurationTests.java new file mode 100644 index 00000000..2b45a297 --- /dev/null +++ b/spring-cloud-gateway-server-mvc/src/test/java/org/springframework/cloud/gateway/server/mvc/GatewayServerMvcAutoConfigurationTests.java @@ -0,0 +1,63 @@ +/* + * Copyright 2013-2024 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; + +import org.junit.jupiter.api.Test; + +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration; +import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration; +import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.cloud.gateway.server.mvc.filter.FormFilter; +import org.springframework.cloud.gateway.server.mvc.filter.ForwardedRequestHeadersFilter; +import org.springframework.cloud.gateway.server.mvc.filter.RemoveContentLengthRequestHeadersFilter; +import org.springframework.cloud.gateway.server.mvc.filter.RemoveHopByHopRequestHeadersFilter; +import org.springframework.cloud.gateway.server.mvc.filter.RemoveHopByHopResponseHeadersFilter; +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 static org.assertj.core.api.Assertions.assertThat; + +public class GatewayServerMvcAutoConfigurationTests { + + @Test + void filterEnabledPropertiesWork() { + new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GatewayServerMvcAutoConfiguration.class, + RestTemplateAutoConfiguration.class, RestClientAutoConfiguration.class, SslAutoConfiguration.class)) + .withPropertyValues("spring.cloud.gateway.mvc.form-filter.enabled=false", + "spring.cloud.gateway.mvc.forwarded-request-headers-filter.enabled=false", + "spring.cloud.gateway.mvc.remove-content-length-request-headers-filter.enabled=false", + "spring.cloud.gateway.mvc.remove-hop-by-hop-request-headers-filter.enabled=false", + "spring.cloud.gateway.mvc.remove-hop-by-hop-response-headers-filter.enabled=false", + "spring.cloud.gateway.mvc.transfer-encoding-normalization-request-headers-filter.enabled=false", + "spring.cloud.gateway.mvc.weight-calculator-filter.enabled=false", + "spring.cloud.gateway.mvc.x-forwarded-request-headers-filter.enabled=false") + .run(context -> { + assertThat(context).doesNotHaveBean(FormFilter.class); + assertThat(context).doesNotHaveBean(ForwardedRequestHeadersFilter.class); + assertThat(context).doesNotHaveBean(RemoveContentLengthRequestHeadersFilter.class); + assertThat(context).doesNotHaveBean(RemoveHopByHopRequestHeadersFilter.class); + assertThat(context).doesNotHaveBean(RemoveHopByHopResponseHeadersFilter.class); + assertThat(context).doesNotHaveBean(TransferEncodingNormalizationRequestHeadersFilter.class); + assertThat(context).doesNotHaveBean(WeightCalculatorFilter.class); + assertThat(context).doesNotHaveBean(XForwardedRequestHeadersFilter.class); + }); + } + +}