@@ -81,10 +81,15 @@ protected ServletUriComponentsBuilder(ServletUriComponentsBuilder other) {
8181 *
8282 * <p><strong>Note:</strong> This method extracts values from "Forwarded"
8383 * 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}.
8488 */
8589 public static ServletUriComponentsBuilder fromContextPath (HttpServletRequest request ) {
8690 ServletUriComponentsBuilder builder = initFromRequest (request );
87- builder .replacePath (prependForwardedPrefix (request , request .getContextPath ()));
91+ String forwardedPrefix = getForwardedPrefix (request );
92+ builder .replacePath (forwardedPrefix != null ? forwardedPrefix : request .getContextPath ());
8893 return builder ;
8994 }
9095
@@ -98,6 +103,10 @@ public static ServletUriComponentsBuilder fromContextPath(HttpServletRequest req
98103 *
99104 * <p><strong>Note:</strong> This method extracts values from "Forwarded"
100105 * 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}.
101110 */
102111 public static ServletUriComponentsBuilder fromServletMapping (HttpServletRequest request ) {
103112 ServletUriComponentsBuilder builder = fromContextPath (request );
@@ -113,10 +122,14 @@ public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest
113122 *
114123 * <p><strong>Note:</strong> This method extracts values from "Forwarded"
115124 * 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}.
116129 */
117130 public static ServletUriComponentsBuilder fromRequestUri (HttpServletRequest request ) {
118131 ServletUriComponentsBuilder builder = initFromRequest (request );
119- builder .initPath (prependForwardedPrefix (request , request . getRequestURI () ));
132+ builder .initPath (getRequestUriWithForwardedPrefix (request ));
120133 return builder ;
121134 }
122135
@@ -126,10 +139,14 @@ public static ServletUriComponentsBuilder fromRequestUri(HttpServletRequest requ
126139 *
127140 * <p><strong>Note:</strong> This method extracts values from "Forwarded"
128141 * 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}.
129146 */
130147 public static ServletUriComponentsBuilder fromRequest (HttpServletRequest request ) {
131148 ServletUriComponentsBuilder builder = initFromRequest (request );
132- builder .initPath (prependForwardedPrefix (request , request . getRequestURI () ));
149+ builder .initPath (getRequestUriWithForwardedPrefix (request ));
133150 builder .query (request .getQueryString ());
134151 return builder ;
135152 }
@@ -153,7 +170,7 @@ private static ServletUriComponentsBuilder initFromRequest(HttpServletRequest re
153170 return builder ;
154171 }
155172
156- private static String prependForwardedPrefix (HttpServletRequest request , String path ) {
173+ private static String getForwardedPrefix (HttpServletRequest request ) {
157174 String prefix = null ;
158175 Enumeration <String > names = request .getHeaderNames ();
159176 while (names .hasMoreElements ()) {
@@ -163,7 +180,22 @@ private static String prependForwardedPrefix(HttpServletRequest request, String
163180 }
164181 }
165182 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 ;
167199 }
168200 return path ;
169201 }
@@ -177,6 +209,10 @@ private static String prependForwardedPrefix(HttpServletRequest request, String
177209 *
178210 * <p><strong>Note:</strong> This method extracts values from "Forwarded"
179211 * 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}.
180216 */
181217 public static ServletUriComponentsBuilder fromCurrentContextPath () {
182218 return fromContextPath (getCurrentRequest ());
@@ -199,6 +235,10 @@ public static ServletUriComponentsBuilder fromCurrentServletMapping() {
199235 *
200236 * <p><strong>Note:</strong> This method extracts values from "Forwarded"
201237 * 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}.
202242 */
203243 public static ServletUriComponentsBuilder fromCurrentRequestUri () {
204244 return fromRequestUri (getCurrentRequest ());
@@ -210,6 +250,10 @@ public static ServletUriComponentsBuilder fromCurrentRequestUri() {
210250 *
211251 * <p><strong>Note:</strong> This method extracts values from "Forwarded"
212252 * 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}.
213257 */
214258 public static ServletUriComponentsBuilder fromCurrentRequest () {
215259 return fromRequest (getCurrentRequest ());
0 commit comments