Skip to content

Commit 153508a

Browse files
committed
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
1 parent 895feda commit 153508a

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -95,9 +95,12 @@ public static ServletUriComponentsBuilder fromRequest(HttpServletRequest request
9595
String scheme = request.getScheme();
9696
int port = request.getServerPort();
9797

98+
String header = request.getHeader("X-Forwarded-Host");
99+
String host = StringUtils.hasText(header) ? header: request.getServerName();
100+
98101
ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
99102
builder.scheme(scheme);
100-
builder.host(request.getServerName());
103+
builder.host(host);
101104
if ((scheme.equals("http") && port != 80) || (scheme.equals("https") && port != 443)) {
102105
builder.port(port);
103106
}
@@ -138,7 +141,10 @@ public static ServletUriComponentsBuilder fromCurrentRequest() {
138141
return fromRequest(getCurrentRequest());
139142
}
140143

141-
private static HttpServletRequest getCurrentRequest() {
144+
/**
145+
* Obtain the request through {@link RequestContextHolder}.
146+
*/
147+
protected static HttpServletRequest getCurrentRequest() {
142148
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
143149
Assert.state(requestAttributes != null, "Could not find current request via RequestContextHolder");
144150
Assert.isInstanceOf(ServletRequestAttributes.class, requestAttributes);

spring-webmvc/src/test/java/org/springframework/web/servlet/support/ServletUriComponentsBuilderTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2013 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -83,6 +83,16 @@ public void fromRequestUri() {
8383
assertEquals("http://localhost/mvc-showcase/data/param", result);
8484
}
8585

86+
@Test
87+
public void fromRequestWithForwardedHostHeader() {
88+
request.addHeader("X-Forwarded-Host", "anotherHost");
89+
request.setRequestURI("/mvc-showcase/data/param");
90+
request.setQueryString("foo=123");
91+
String result = ServletUriComponentsBuilder.fromRequest(request).build().toUriString();
92+
93+
assertEquals("http://anotherHost/mvc-showcase/data/param?foo=123", result);
94+
}
95+
8696
@Test
8797
public void fromContextPath() {
8898
request.setRequestURI("/mvc-showcase/data/param");

0 commit comments

Comments
 (0)