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:
Rossen Stoyanchev
2013-01-07 16:55:09 -05:00
parent 895feda349
commit 153508a300
2 changed files with 20 additions and 4 deletions

View File

@@ -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");