Match headers in spring-cloud-gateway-mvc case-insensitively

Fixes gh-1930
This commit is contained in:
Joris Kuipers
2020-09-05 19:24:25 +02:00
committed by spencergibb
parent 52930d2458
commit 00926981ca
2 changed files with 11 additions and 5 deletions

View File

@@ -33,6 +33,8 @@ import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import static java.util.stream.Collectors.toSet;
/**
* @author Dave Syer
* @author Tim Ysewyn
@@ -56,7 +58,8 @@ public class ProxyExchangeArgumentResolver implements HandlerMethodArgumentResol
}
public void setAutoForwardedHeaders(Set<String> autoForwardedHeaders) {
this.autoForwardedHeaders = autoForwardedHeaders;
this.autoForwardedHeaders = autoForwardedHeaders.stream().map(String::toLowerCase)
.collect(toSet());
}
public void setSensitive(Set<String> sensitive) {
@@ -100,7 +103,7 @@ public class ProxyExchangeArgumentResolver implements HandlerMethodArgumentResol
HttpHeaders headers = new HttpHeaders();
while (headerNames.hasMoreElements()) {
String header = headerNames.nextElement();
if (this.autoForwardedHeaders.contains(header)) {
if (this.autoForwardedHeaders.contains(header.toLowerCase())) {
headers.addAll(header,
Collections.list(nativeRequest.getHeaders(header)));
}

View File

@@ -58,7 +58,7 @@ import org.springframework.web.util.UriComponentsBuilder;
import static org.assertj.core.api.Assertions.assertThat;
@RunWith(SpringRunner.class)
@SpringBootTest(properties = { "spring.cloud.gateway.proxy.auto-forward=baz" },
@SpringBootTest(properties = { "spring.cloud.gateway.proxy.auto-forward=Baz" },
webEnvironment = WebEnvironment.RANDOM_PORT)
@ContextConfiguration(classes = TestApplication.class)
public class ProductionConfigurationTests {
@@ -277,11 +277,14 @@ public class ProductionConfigurationTests {
RequestEntity
.get(rest.getRestTemplate().getUriTemplateHandler()
.expand("/proxy/headers"))
.header("foo", "bar").header("abc", "xyz")
.header("foo", "bar")
.header("abc", "xyz")
.header("baz", "fob").build(),
Map.class)
.getBody();
assertThat(headers).doesNotContainKey("foo").doesNotContainKey("hello")
assertThat(headers)
.doesNotContainKey("foo")
.doesNotContainKey("hello")
.containsKeys("bar", "abc");
assertThat(headers.get("bar")).containsOnly("hello");