Skip to content

Commit ca9721f

Browse files
committed
Migrates proxy-exchange-webmvc properties to new namespace
from spring.cloud.gateway.proxy to spring.cloud.gateway.proxy-exchange.webmvc Fixes gh-3361
1 parent 9cd6b4a commit ca9721f

File tree

6 files changed

+200
-9
lines changed

6 files changed

+200
-9
lines changed

spring-cloud-gateway-mvc/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@
4141
<version>4.5.14</version>
4242
<scope>test</scope>
4343
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-properties-migrator</artifactId>
47+
<scope>test</scope>
48+
</dependency>
4449
<dependency>
4550
<groupId>org.springframework.boot</groupId>
4651
<artifactId>spring-boot-configuration-processor</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright 2016-2019 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.mvc.config;
18+
19+
import java.util.HashSet;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
22+
import java.util.Set;
23+
24+
import org.springframework.boot.context.properties.ConfigurationProperties;
25+
import org.springframework.cloud.gateway.mvc.ProxyExchange;
26+
import org.springframework.http.HttpHeaders;
27+
28+
/**
29+
* Configuration properties for the {@link ProxyExchange} argument handler in
30+
* <code>@RequestMapping</code> methods.
31+
*
32+
* @author Dave Syer
33+
* @author Tim Ysewyn
34+
* @author Joris Kuipers
35+
*
36+
*/
37+
@ConfigurationProperties(ProxyExchangeWebMvcProperties.PREFIX)
38+
public class ProxyExchangeWebMvcProperties {
39+
40+
/**
41+
* Properties prefix.
42+
*/
43+
public static final String PREFIX = "spring.cloud.gateway.proxy-exchange.webmvc";
44+
45+
/**
46+
* Contains headers that are considered sensitive by default.
47+
*/
48+
public static Set<String> DEFAULT_SENSITIVE = Set.of("cookie", "authorization");
49+
50+
/**
51+
* Contains headers that are skipped by default.
52+
*/
53+
public static Set<String> DEFAULT_SKIPPED = Set.of("content-length", "host");
54+
55+
/**
56+
* Fixed header values that will be added to all downstream requests.
57+
*/
58+
private Map<String, String> headers = new LinkedHashMap<>();
59+
60+
/**
61+
* A set of header names that should be sent downstream by default.
62+
*/
63+
private Set<String> autoForward = new HashSet<>();
64+
65+
/**
66+
* A set of sensitive header names that will not be sent downstream by default.
67+
*/
68+
private Set<String> sensitive = DEFAULT_SENSITIVE;
69+
70+
/**
71+
* A set of header names that will not be sent downstream because they could be
72+
* problematic.
73+
*/
74+
private Set<String> skipped = DEFAULT_SKIPPED;
75+
76+
public Map<String, String> getHeaders() {
77+
return headers;
78+
}
79+
80+
public void setHeaders(Map<String, String> headers) {
81+
this.headers = headers;
82+
}
83+
84+
public Set<String> getAutoForward() {
85+
return autoForward;
86+
}
87+
88+
public void setAutoForward(Set<String> autoForward) {
89+
this.autoForward = autoForward;
90+
}
91+
92+
public Set<String> getSensitive() {
93+
return sensitive;
94+
}
95+
96+
public void setSensitive(Set<String> sensitive) {
97+
this.sensitive = sensitive;
98+
}
99+
100+
public Set<String> getSkipped() {
101+
return skipped;
102+
}
103+
104+
public void setSkipped(Set<String> skipped) {
105+
this.skipped = skipped;
106+
}
107+
108+
public HttpHeaders convertHeaders() {
109+
HttpHeaders headers = new HttpHeaders();
110+
for (String key : this.headers.keySet()) {
111+
headers.set(key, this.headers.get(key));
112+
}
113+
return headers;
114+
}
115+
116+
}

spring-cloud-gateway-mvc/src/main/java/org/springframework/cloud/gateway/mvc/config/ProxyProperties.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.util.Set;
2323

2424
import org.springframework.boot.context.properties.ConfigurationProperties;
25+
import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
2526
import org.springframework.cloud.gateway.mvc.ProxyExchange;
2627
import org.springframework.http.HttpHeaders;
2728

@@ -32,8 +33,10 @@
3233
* @author Dave Syer
3334
* @author Tim Ysewyn
3435
* @author Joris Kuipers
35-
*
36+
* @author Spencer Gibb
37+
* @deprecated {@link ProxyExchangeWebMvcProperties}
3638
*/
39+
@Deprecated
3740
@ConfigurationProperties("spring.cloud.gateway.proxy")
3841
public class ProxyProperties {
3942

@@ -68,6 +71,7 @@ public class ProxyProperties {
6871
*/
6972
private Set<String> skipped = DEFAULT_SKIPPED;
7073

74+
@DeprecatedConfigurationProperty(replacement = ProxyExchangeWebMvcProperties.PREFIX + ".headers", since = "4.3.0")
7175
public Map<String, String> getHeaders() {
7276
return headers;
7377
}
@@ -76,6 +80,8 @@ public void setHeaders(Map<String, String> headers) {
7680
this.headers = headers;
7781
}
7882

83+
@DeprecatedConfigurationProperty(replacement = ProxyExchangeWebMvcProperties.PREFIX + ".auto-forward",
84+
since = "4.3.0")
7985
public Set<String> getAutoForward() {
8086
return autoForward;
8187
}
@@ -84,6 +90,7 @@ public void setAutoForward(Set<String> autoForward) {
8490
this.autoForward = autoForward;
8591
}
8692

93+
@DeprecatedConfigurationProperty(replacement = ProxyExchangeWebMvcProperties.PREFIX + ".sensitive", since = "4.3.0")
8794
public Set<String> getSensitive() {
8895
return sensitive;
8996
}
@@ -92,6 +99,7 @@ public void setSensitive(Set<String> sensitive) {
9299
this.sensitive = sensitive;
93100
}
94101

102+
@DeprecatedConfigurationProperty(replacement = ProxyExchangeWebMvcProperties.PREFIX + ".skipped", since = "4.3.0")
95103
public Set<String> getSkipped() {
96104
return skipped;
97105
}

spring-cloud-gateway-mvc/src/main/java/org/springframework/cloud/gateway/mvc/config/ProxyResponseAutoConfiguration.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
@Configuration(proxyBeanMethods = false)
5858
@ConditionalOnWebApplication
5959
@ConditionalOnClass({ HandlerMethodReturnValueHandler.class })
60-
@EnableConfigurationProperties(ProxyProperties.class)
60+
@EnableConfigurationProperties({ ProxyExchangeWebMvcProperties.class, ProxyProperties.class })
6161
public class ProxyResponseAutoConfiguration implements WebMvcConfigurer {
6262

6363
@Autowired
@@ -66,7 +66,7 @@ public class ProxyResponseAutoConfiguration implements WebMvcConfigurer {
6666
@Bean
6767
@ConditionalOnMissingBean
6868
public ProxyExchangeArgumentResolver proxyExchangeArgumentResolver(Optional<RestTemplateBuilder> optional,
69-
ProxyProperties proxy) {
69+
ProxyExchangeWebMvcProperties properties) {
7070
RestTemplateBuilder builder = optional.orElse(new RestTemplateBuilder());
7171
RestTemplate template = builder.build();
7272
template.setErrorHandler(new NoOpResponseErrorHandler());
@@ -77,14 +77,14 @@ public boolean supports(Class<?> clazz) {
7777
}
7878
});
7979
ProxyExchangeArgumentResolver resolver = new ProxyExchangeArgumentResolver(template);
80-
resolver.setHeaders(proxy.convertHeaders());
81-
resolver.setAutoForwardedHeaders(proxy.getAutoForward());
80+
resolver.setHeaders(properties.convertHeaders());
81+
resolver.setAutoForwardedHeaders(properties.getAutoForward());
8282
Set<String> excludedHeaderNames = new HashSet<>();
83-
if (proxy.getSensitive() != null) {
84-
excludedHeaderNames.addAll(proxy.getSensitive());
83+
if (properties.getSensitive() != null) {
84+
excludedHeaderNames.addAll(properties.getSensitive());
8585
}
86-
if (proxy.getSkipped() != null) {
87-
excludedHeaderNames.addAll(proxy.getSkipped());
86+
if (properties.getSkipped() != null) {
87+
excludedHeaderNames.addAll(properties.getSkipped());
8888
}
8989
resolver.setExcluded(excludedHeaderNames);
9090
return resolver;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2013-2025 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.mvc.config;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.boot.SpringBootConfiguration;
23+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
24+
import org.springframework.boot.test.context.SpringBootTest;
25+
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
26+
import org.springframework.test.context.ActiveProfiles;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
@SuppressWarnings("unchecked")
31+
@SpringBootTest(properties = {}, webEnvironment = WebEnvironment.RANDOM_PORT)
32+
@ActiveProfiles("propertiesmigrationtests")
33+
public class ProxyExchangeWebmvcPropertiesMigrationTests {
34+
35+
@Autowired
36+
ProxyExchangeWebMvcProperties properties;
37+
38+
@Test
39+
public void deprecatedRoutePropertiesWork() {
40+
assertThat(properties.getHeaders()).hasSize(2);
41+
assertThat(properties.getAutoForward()).hasSize(2);
42+
assertThat(properties.getSensitive()).hasSize(2);
43+
assertThat(properties.getSkipped()).hasSize(3);
44+
}
45+
46+
@SpringBootConfiguration
47+
@EnableAutoConfiguration
48+
protected static class TestConfiguration {
49+
50+
}
51+
52+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
spring.cloud.gateway.proxy:
2+
headers:
3+
X-Foo: xfooval
4+
X-Bar: xbarval
5+
auto-forward: X-FWD1, X-FWD2
6+
sensitive: X-S1, X-S21
7+
skipped: X-SK1, X-SK2, X-SK3
8+
logging:
9+
level:
10+
org.springframework.cloud.gateway.server.mvc: TRACE

0 commit comments

Comments
 (0)