Support "X-Forwarded-Host" in UriComponentsBuilder
ServletUriComponentsBuilder now supports setting the host to the value held in the X-Forwarded-Host [0] header used in reverse proxy scenarios. [0] http://tools.ietf.org/html/draft-ietf-appsawg-http-forwarded-10 Issue: SPR-10110
This commit is contained in:
parent
895feda349
commit
153508a300
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2011 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -95,9 +95,12 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
|
|||
String scheme = request.getScheme();
|
||||
int port = request.getServerPort();
|
||||
|
||||
String header = request.getHeader("X-Forwarded-Host");
|
||||
String host = StringUtils.hasText(header) ? header: request.getServerName();
|
||||
|
||||
ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
|
||||
builder.scheme(scheme);
|
||||
builder.host(request.getServerName());
|
||||
builder.host(host);
|
||||
if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) {
|
||||
builder.port(port);
|
||||
}
|
||||
|
|
@ -138,7 +141,10 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
|
|||
return fromRequest(getCurrentRequest());
|
||||
}
|
||||
|
||||
private static HttpServletRequest getCurrentRequest() {
|
||||
/**
|
||||
* Obtain the request through {@link RequestContextHolder}.
|
||||
*/
|
||||
protected static HttpServletRequest getCurrentRequest() {
|
||||
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
|
||||
Assert.state(requestAttributes != null, "Could not find current request via RequestContextHolder");
|
||||
Assert.isInstanceOf(ServletRequestAttributes.class, requestAttributes);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2013 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
|
@ -83,6 +83,16 @@ public class ServletUriComponentsBuilderTests {
|
|||
assertEquals("http://localhost/mvc-showcase/data/param", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromRequestWithForwardedHostHeader() {
|
||||
request.addHeader("X-Forwarded-Host", "anotherHost");
|
||||
request.setRequestURI("/mvc-showcase/data/param");
|
||||
request.setQueryString("foo=123");
|
||||
String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString();
|
||||
|
||||
assertEquals("http://anotherHost/mvc-showcase/data/param?foo=123", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fromContextPath() {
|
||||
request.setRequestURI("/mvc-showcase/data/param");
|
||||
|
|
|
|||
Loading…
Reference in New Issue