@@ -81,10 +81,15 @@ protected ServletUriComponentsBuilder(ServletUriComponentsBuilder other) {
81
81
*
82
82
* <p><strong>Note:</strong> This method extracts values from "Forwarded"
83
83
* and "X-Forwarded-*" headers if found. See class-level docs.
84
+ *
85
+ * <p>As of 4.3.15, this method replaces the contextPath with the value
86
+ * of "X-Forwarded-Prefix" rather than prepending, thus aligning with
87
+ * {@code ForwardedHeaderFiller}.
84
88
*/
85
89
public static ServletUriComponentsBuilder fromContextPath (HttpServletRequest request ) {
86
90
ServletUriComponentsBuilder builder = initFromRequest (request );
87
- builder .replacePath (prependForwardedPrefix (request , request .getContextPath ()));
91
+ String forwardedPrefix = getForwardedPrefix (request );
92
+ builder .replacePath (forwardedPrefix != null ? forwardedPrefix : request .getContextPath ());
88
93
return builder ;
89
94
}
90
95
@@ -98,6 +103,10 @@ public static ServletUriComponentsBuilder fromContextPath(HttpServletRequest req
98
103
*
99
104
* <p><strong>Note:</strong> This method extracts values from "Forwarded"
100
105
* and "X-Forwarded-*" headers if found. See class-level docs.
106
+ *
107
+ * <p>As of 4.3.15, this method replaces the contextPath with the value
108
+ * of "X-Forwarded-Prefix" rather than prepending, thus aligning with
109
+ * {@code ForwardedHeaderFiller}.
101
110
*/
102
111
public static ServletUriComponentsBuilder fromServletMapping (HttpServletRequest request ) {
103
112
ServletUriComponentsBuilder builder = fromContextPath (request );
@@ -113,10 +122,14 @@ public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest
113
122
*
114
123
* <p><strong>Note:</strong> This method extracts values from "Forwarded"
115
124
* and "X-Forwarded-*" headers if found. See class-level docs.
125
+ *
126
+ * <p>As of 4.3.15, this method replaces the contextPath with the value
127
+ * of "X-Forwarded-Prefix" rather than prepending, thus aligning with
128
+ * {@code ForwardedHeaderFiller}.
116
129
*/
117
130
public static ServletUriComponentsBuilder fromRequestUri (HttpServletRequest request ) {
118
131
ServletUriComponentsBuilder builder = initFromRequest (request );
119
- builder .initPath (prependForwardedPrefix (request , request . getRequestURI () ));
132
+ builder .initPath (getRequestUriWithForwardedPrefix (request ));
120
133
return builder ;
121
134
}
122
135
@@ -126,10 +139,14 @@ public static ServletUriComponentsBuilder fromRequestUri(HttpServletRequest requ
126
139
*
127
140
* <p><strong>Note:</strong> This method extracts values from "Forwarded"
128
141
* and "X-Forwarded-*" headers if found. See class-level docs.
142
+ *
143
+ * <p>As of 4.3.15, this method replaces the contextPath with the value
144
+ * of "X-Forwarded-Prefix" rather than prepending, thus aligning with
145
+ * {@code ForwardedHeaderFiller}.
129
146
*/
130
147
public static ServletUriComponentsBuilder fromRequest (HttpServletRequest request ) {
131
148
ServletUriComponentsBuilder builder = initFromRequest (request );
132
- builder .initPath (prependForwardedPrefix (request , request . getRequestURI () ));
149
+ builder .initPath (getRequestUriWithForwardedPrefix (request ));
133
150
builder .query (request .getQueryString ());
134
151
return builder ;
135
152
}
@@ -153,7 +170,7 @@ private static ServletUriComponentsBuilder initFromRequest(HttpServletRequest re
153
170
return builder ;
154
171
}
155
172
156
- private static String prependForwardedPrefix (HttpServletRequest request , String path ) {
173
+ private static String getForwardedPrefix (HttpServletRequest request ) {
157
174
String prefix = null ;
158
175
Enumeration <String > names = request .getHeaderNames ();
159
176
while (names .hasMoreElements ()) {
@@ -163,7 +180,22 @@ private static String prependForwardedPrefix(HttpServletRequest request, String
163
180
}
164
181
}
165
182
if (prefix != null ) {
166
- path = prefix + path ;
183
+ while (prefix .endsWith ("/" )) {
184
+ prefix = prefix .substring (0 , prefix .length () - 1 );
185
+ }
186
+ }
187
+ return prefix ;
188
+ }
189
+
190
+ private static String getRequestUriWithForwardedPrefix (HttpServletRequest request ) {
191
+ String path = request .getRequestURI ();
192
+ String forwardedPrefix = getForwardedPrefix (request );
193
+ if (forwardedPrefix != null ) {
194
+ String contextPath = request .getContextPath ();
195
+ if (!StringUtils .isEmpty (contextPath ) && !contextPath .equals ("/" ) && path .startsWith (contextPath )) {
196
+ path = path .substring (contextPath .length ());
197
+ }
198
+ path = forwardedPrefix + path ;
167
199
}
168
200
return path ;
169
201
}
@@ -177,6 +209,10 @@ private static String prependForwardedPrefix(HttpServletRequest request, String
177
209
*
178
210
* <p><strong>Note:</strong> This method extracts values from "Forwarded"
179
211
* and "X-Forwarded-*" headers if found. See class-level docs.
212
+ *
213
+ * <p>As of 4.3.15, this method replaces the contextPath with the value
214
+ * of "X-Forwarded-Prefix" rather than prepending, thus aligning with
215
+ * {@code ForwardedHeaderFiller}.
180
216
*/
181
217
public static ServletUriComponentsBuilder fromCurrentContextPath () {
182
218
return fromContextPath (getCurrentRequest ());
@@ -199,6 +235,10 @@ public static ServletUriComponentsBuilder fromCurrentServletMapping() {
199
235
*
200
236
* <p><strong>Note:</strong> This method extracts values from "Forwarded"
201
237
* and "X-Forwarded-*" headers if found. See class-level docs.
238
+ *
239
+ * <p>As of 4.3.15, this method replaces the contextPath with the value
240
+ * of "X-Forwarded-Prefix" rather than prepending, thus aligning with
241
+ * {@code ForwardedHeaderFiller}.
202
242
*/
203
243
public static ServletUriComponentsBuilder fromCurrentRequestUri () {
204
244
return fromRequestUri (getCurrentRequest ());
@@ -210,6 +250,10 @@ public static ServletUriComponentsBuilder fromCurrentRequestUri() {
210
250
*
211
251
* <p><strong>Note:</strong> This method extracts values from "Forwarded"
212
252
* and "X-Forwarded-*" headers if found. See class-level docs.
253
+ *
254
+ * <p>As of 4.3.15, this method replaces the contextPath with the value
255
+ * of "X-Forwarded-Prefix" rather than prepending, thus aligning with
256
+ * {@code ForwardedHeaderFiller}.
213
257
*/
214
258
public static ServletUriComponentsBuilder fromCurrentRequest () {
215
259
return fromRequest (getCurrentRequest ());
0 commit comments