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:
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user