Skip to content

Commit 9ea11b3

Browse files
committed
Adds properties to disable filters if needed
Fixes gh-3310
1 parent 37c2837 commit 9ea11b3

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed

spring-cloud-gateway-server-mvc/src/main/java/org/springframework/cloud/gateway/server/mvc/GatewayServerMvcAutoConfiguration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public RestClientProxyExchange restClientProxyExchange(RestClient.Builder restCl
9393

9494
@Bean
9595
@ConditionalOnMissingBean
96+
@ConditionalOnProperty(prefix = GatewayMvcProperties.PREFIX, name = "form-filter.enabled", matchIfMissing = true)
9697
public FormFilter formFilter() {
9798
return new FormFilter();
9899
}
@@ -156,30 +157,40 @@ public ProxyExchangeHandlerFunction proxyExchangeHandlerFunction(ProxyExchange p
156157

157158
@Bean
158159
@ConditionalOnMissingBean
160+
@ConditionalOnProperty(prefix = GatewayMvcProperties.PREFIX,
161+
name = "remove-content-length-request-headers-filter.enabled", matchIfMissing = true)
159162
public RemoveContentLengthRequestHeadersFilter removeContentLengthRequestHeadersFilter() {
160163
return new RemoveContentLengthRequestHeadersFilter();
161164
}
162165

163166
@Bean
164167
@ConditionalOnMissingBean
168+
@ConditionalOnProperty(prefix = GatewayMvcProperties.PREFIX,
169+
name = "remove-hop-by-hop-request-headers-filter.enabled", matchIfMissing = true)
165170
public RemoveHopByHopRequestHeadersFilter removeHopByHopRequestHeadersFilter() {
166171
return new RemoveHopByHopRequestHeadersFilter();
167172
}
168173

169174
@Bean
170175
@ConditionalOnMissingBean
176+
@ConditionalOnProperty(prefix = GatewayMvcProperties.PREFIX,
177+
name = "remove-hop-by-hop-response-headers-filter.enabled", matchIfMissing = true)
171178
public RemoveHopByHopResponseHeadersFilter removeHopByHopResponseHeadersFilter() {
172179
return new RemoveHopByHopResponseHeadersFilter();
173180
}
174181

175182
@Bean
176183
@ConditionalOnMissingBean
184+
@ConditionalOnProperty(prefix = GatewayMvcProperties.PREFIX,
185+
name = "transfer-encoding-normalization-request-headers-filter.enabled", matchIfMissing = true)
177186
public TransferEncodingNormalizationRequestHeadersFilter transferEncodingNormalizationRequestHeadersFilter() {
178187
return new TransferEncodingNormalizationRequestHeadersFilter();
179188
}
180189

181190
@Bean
182191
@ConditionalOnMissingBean
192+
@ConditionalOnProperty(prefix = GatewayMvcProperties.PREFIX, name = "weight-calculator-filter.enabled",
193+
matchIfMissing = true)
183194
public WeightCalculatorFilter weightCalculatorFilter() {
184195
return new WeightCalculatorFilter();
185196
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,46 @@
11
{
22
"properties": [
3+
{
4+
"name": "spring.cloud.gateway.mvc.form-filter.enabled",
5+
"type": "java.lang.Boolean",
6+
"description": "Enables the form-filter.",
7+
"defaultValue": "true"
8+
},
39
{
410
"name": "spring.cloud.gateway.mvc.forwarded-request-headers-filter.enabled",
511
"type": "java.lang.Boolean",
612
"description": "Enables the forwarded-request-headers-filter.",
713
"defaultValue": "true"
14+
},
15+
{
16+
"name": "spring.cloud.gateway.mvc.remove-content-length-request-headers-filter.enabled",
17+
"type": "java.lang.Boolean",
18+
"description": "Enables the remove-content-length-request-headers-filter.",
19+
"defaultValue": "true"
20+
},
21+
{
22+
"name": "spring.cloud.gateway.mvc.remove-hop-by-hop-request-headers-filter.enabled",
23+
"type": "java.lang.Boolean",
24+
"description": "Enables the forwarded-request-headers-filter.",
25+
"defaultValue": "true"
26+
},
27+
{
28+
"name": "spring.cloud.gateway.mvc.remove-hop-by-hop-response-headers-filter.enabled",
29+
"type": "java.lang.Boolean",
30+
"description": "Enables the forwarded-request-headers-filter.",
31+
"defaultValue": "true"
32+
},
33+
{
34+
"name": "spring.cloud.gateway.mvc.transfer-encoding-normalization-request-headers-filter.enabled",
35+
"type": "java.lang.Boolean",
36+
"description": "Enables the transfer-encoding-normalization-request-headers-filter.",
37+
"defaultValue": "true"
38+
},
39+
{
40+
"name": "spring.cloud.gateway.mvc.weight-calculator-filter.enabled",
41+
"type": "java.lang.Boolean",
42+
"description": "Enables the weight-calculator-filter.",
43+
"defaultValue": "true"
844
}
945
]
1046
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2013-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.cloud.gateway.server.mvc;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.boot.autoconfigure.AutoConfigurations;
22+
import org.springframework.boot.autoconfigure.ssl.SslAutoConfiguration;
23+
import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.web.client.RestTemplateAutoConfiguration;
25+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
26+
import org.springframework.cloud.gateway.server.mvc.filter.FormFilter;
27+
import org.springframework.cloud.gateway.server.mvc.filter.ForwardedRequestHeadersFilter;
28+
import org.springframework.cloud.gateway.server.mvc.filter.RemoveContentLengthRequestHeadersFilter;
29+
import org.springframework.cloud.gateway.server.mvc.filter.RemoveHopByHopRequestHeadersFilter;
30+
import org.springframework.cloud.gateway.server.mvc.filter.RemoveHopByHopResponseHeadersFilter;
31+
import org.springframework.cloud.gateway.server.mvc.filter.TransferEncodingNormalizationRequestHeadersFilter;
32+
import org.springframework.cloud.gateway.server.mvc.filter.WeightCalculatorFilter;
33+
import org.springframework.cloud.gateway.server.mvc.filter.XForwardedRequestHeadersFilter;
34+
35+
import static org.assertj.core.api.Assertions.assertThat;
36+
37+
public class GatewayServerMvcAutoConfigurationTests {
38+
39+
@Test
40+
void filterEnabledPropertiesWork() {
41+
new ApplicationContextRunner().withConfiguration(AutoConfigurations.of(GatewayServerMvcAutoConfiguration.class,
42+
RestTemplateAutoConfiguration.class, RestClientAutoConfiguration.class, SslAutoConfiguration.class))
43+
.withPropertyValues("spring.cloud.gateway.mvc.form-filter.enabled=false",
44+
"spring.cloud.gateway.mvc.forwarded-request-headers-filter.enabled=false",
45+
"spring.cloud.gateway.mvc.remove-content-length-request-headers-filter.enabled=false",
46+
"spring.cloud.gateway.mvc.remove-hop-by-hop-request-headers-filter.enabled=false",
47+
"spring.cloud.gateway.mvc.remove-hop-by-hop-response-headers-filter.enabled=false",
48+
"spring.cloud.gateway.mvc.transfer-encoding-normalization-request-headers-filter.enabled=false",
49+
"spring.cloud.gateway.mvc.weight-calculator-filter.enabled=false",
50+
"spring.cloud.gateway.mvc.x-forwarded-request-headers-filter.enabled=false")
51+
.run(context -> {
52+
assertThat(context).doesNotHaveBean(FormFilter.class);
53+
assertThat(context).doesNotHaveBean(ForwardedRequestHeadersFilter.class);
54+
assertThat(context).doesNotHaveBean(RemoveContentLengthRequestHeadersFilter.class);
55+
assertThat(context).doesNotHaveBean(RemoveHopByHopRequestHeadersFilter.class);
56+
assertThat(context).doesNotHaveBean(RemoveHopByHopResponseHeadersFilter.class);
57+
assertThat(context).doesNotHaveBean(TransferEncodingNormalizationRequestHeadersFilter.class);
58+
assertThat(context).doesNotHaveBean(WeightCalculatorFilter.class);
59+
assertThat(context).doesNotHaveBean(XForwardedRequestHeadersFilter.class);
60+
});
61+
}
62+
63+
}

0 commit comments

Comments
 (0)