Fix X-Forwarded-* behavior in ServletUriComponentsBuilder

When using ServletUriComponentsBuilder.fromRequest, this change
makes sure that:
* the default port is used when the "X-Forwarded-Host" header is set
and no port is defined in that header value
* to use the scheme defined in the "X-Forwarded-Proto" header if set

Issue: SPR-11872
This commit is contained in:
Brian Clozel
2014-07-11 00:55:17 +02:00
parent 2f371e5aeb
commit 2c77de10dd
2 changed files with 33 additions and 0 deletions

View File

@@ -111,6 +111,7 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
}
else {
host = hostToUse;
port = -1;
}
}
@@ -119,6 +120,11 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
port = Integer.parseInt(portHeader);
}
String protocolHeader = request.getHeader("X-Forwarded-Proto");
if (StringUtils.hasText(protocolHeader)) {
scheme = protocolHeader;
}
ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
builder.scheme(scheme);
builder.host(host);