Skip to content

Commit

Permalink
Support "X-Forwarded-Host" in UriComponentsBuilder
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rstoyanchev committed Jan 7, 2013
1 parent 895feda commit 153508a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -95,9 +95,12 @@ public static ServletUriComponentsBuilder fromRequest(HttpServletRequest request
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);
}
Expand Down Expand Up @@ -138,7 +141,10 @@ public static ServletUriComponentsBuilder fromCurrentRequest() {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -83,6 +83,16 @@ public void fromRequestUri() {
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");
Expand Down

0 comments on commit 153508a

Please sign in to comment.