File tree Expand file tree Collapse file tree 2 files changed +20
-4
lines changed
main/java/org/springframework/web/servlet/support
test/java/org/springframework/web/servlet/support Expand file tree Collapse file tree 2 files changed +20
-4
lines changed Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -95,9 +95,12 @@ public static ServletUriComponentsBuilder fromRequest(HttpServletRequest request
95
95
String scheme = request .getScheme ();
96
96
int port = request .getServerPort ();
97
97
98
+ String header = request .getHeader ("X-Forwarded-Host" );
99
+ String host = StringUtils .hasText (header ) ? header : request .getServerName ();
100
+
98
101
ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder ();
99
102
builder .scheme (scheme );
100
- builder .host (request . getServerName () );
103
+ builder .host (host );
101
104
if ((scheme .equals ("http" ) && port != 80 ) || (scheme .equals ("https" ) && port != 443 )) {
102
105
builder .port (port );
103
106
}
@@ -138,7 +141,10 @@ public static ServletUriComponentsBuilder fromCurrentRequest() {
138
141
return fromRequest (getCurrentRequest ());
139
142
}
140
143
141
- private static HttpServletRequest getCurrentRequest () {
144
+ /**
145
+ * Obtain the request through {@link RequestContextHolder}.
146
+ */
147
+ protected static HttpServletRequest getCurrentRequest () {
142
148
RequestAttributes requestAttributes = RequestContextHolder .getRequestAttributes ();
143
149
Assert .state (requestAttributes != null , "Could not find current request via RequestContextHolder" );
144
150
Assert .isInstanceOf (ServletRequestAttributes .class , requestAttributes );
Original file line number Diff line number Diff line change 1
1
/*
2
- * Copyright 2002-2012 the original author or authors.
2
+ * Copyright 2002-2013 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
@@ -83,6 +83,16 @@ public void fromRequestUri() {
83
83
assertEquals ("http://localhost/mvc-showcase/data/param" , result );
84
84
}
85
85
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
+
86
96
@ Test
87
97
public void fromContextPath () {
88
98
request .setRequestURI ("/mvc-showcase/data/param" );
You can’t perform that action at this time.
0 commit comments