Support multi-value x-forwarded-host header
Issue: SPR-11140
This commit is contained in:
@@ -96,16 +96,18 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
|
||||
int port = request.getServerPort();
|
||||
String host = request.getServerName();
|
||||
|
||||
String xForwardedHostHeader = request.getHeader("X-Forwarded-Host");
|
||||
String header = request.getHeader("X-Forwarded-Host");
|
||||
|
||||
if (StringUtils.hasText(xForwardedHostHeader)) {
|
||||
if (StringUtils.countOccurrencesOf(xForwardedHostHeader, ":") == 1) {
|
||||
String[] hostAndPort = StringUtils.split(xForwardedHostHeader, ":");
|
||||
if (StringUtils.hasText(header)) {
|
||||
String[] hosts = StringUtils.commaDelimitedListToStringArray(header);
|
||||
String hostToUse = hosts[0];
|
||||
if (hostToUse.contains(":")) {
|
||||
String[] hostAndPort = StringUtils.split(hostToUse, ":");
|
||||
host = hostAndPort[0];
|
||||
port = Integer.parseInt(hostAndPort[1]);
|
||||
}
|
||||
else {
|
||||
host = xForwardedHostHeader;
|
||||
host = hostToUse;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -107,6 +107,15 @@ public class ServletUriComponentsBuilderTests {
|
||||
assertEquals(443, result.getPort());
|
||||
}
|
||||
|
||||
// SPR-11140
|
||||
|
||||
@Test
|
||||
public void fromRequestWithForwardedHostMultiValuedHeader() {
|
||||
this.request.addHeader("X-Forwarded-Host", "a.example.org, b.example.org, c.example.org");
|
||||
|
||||
assertEquals("a.example.org", ServletUriComponentsBuilder.fromRequest(this.request).build().getHost());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromContextPath() {
|
||||
request.setRequestURI("/mvc-showcase/data/param");
|
||||
|
||||
Reference in New Issue
Block a user