Adds properties to disable filters if needed

Fixes gh-3310
This commit is contained in:
sgibb
2024-03-19 14:27:15 -04:00
parent 37c2837d2b
commit 9ea11b375d
3 changed files with 110 additions and 0 deletions

View File

@@ -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();
}

View File

@@ -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"
}
]
}

View File

@@ -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);
});
}
}