diff --git a/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java b/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java index 80ccb56d9d..9c552f9660 100644 --- a/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java +++ b/spring-test/src/main/java/org/springframework/mock/web/reactive/function/server/MockServerRequest.java @@ -38,7 +38,6 @@ import org.springframework.http.HttpCookie; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.HttpRange; -import org.springframework.http.HttpRequest; import org.springframework.http.MediaType; import org.springframework.http.codec.HttpMessageReader; import org.springframework.http.codec.multipart.Part; @@ -141,7 +140,7 @@ public class MockServerRequest implements ServerRequest { @Override public UriBuilder uriBuilder() { - return UriComponentsBuilder.fromHttpRequest(new ServerRequestAdapter()); + return UriComponentsBuilder.fromUri(this.uri); } @Override @@ -571,22 +570,4 @@ public class MockServerRequest implements ServerRequest { } - private final class ServerRequestAdapter implements HttpRequest { - - @Override - public String getMethodValue() { - return methodName(); - } - - @Override - public URI getURI() { - return MockServerRequest.this.uri; - } - - @Override - public HttpHeaders getHeaders() { - return MockServerRequest.this.headers.headers; - } - } - } diff --git a/spring-web/src/main/java/org/springframework/web/cors/reactive/CorsUtils.java b/spring-web/src/main/java/org/springframework/web/cors/reactive/CorsUtils.java index 23380447ed..da8569d78c 100644 --- a/spring-web/src/main/java/org/springframework/web/cors/reactive/CorsUtils.java +++ b/spring-web/src/main/java/org/springframework/web/cors/reactive/CorsUtils.java @@ -16,6 +16,8 @@ package org.springframework.web.cors.reactive; +import java.net.URI; + import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.server.reactive.ServerHttpRequest; @@ -49,18 +51,16 @@ public abstract class CorsUtils { } /** - * Check if the request is a same-origin one, based on {@code Origin}, {@code Host}, - * {@code Forwarded}, {@code X-Forwarded-Proto}, {@code X-Forwarded-Host} and - * @code X-Forwarded-Port} headers. + * Check if the request is a same-origin one, based on {@code Origin}, and + * {@code Host} headers. + * + *
Note: as of 5.1 this method ignores + * {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the + * client-originated address. Consider using the {@code ForwardedHeaderFilter} + * to extract and use, or to discard such headers. + * * @return {@code true} if the request is a same-origin one, {@code false} in case * of a cross-origin request - *
Note: this method uses values from "Forwarded"
- * (RFC 7239),
- * "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers,
- * if present, in order to reflect the client-originated address.
- * Consider using the {@code ForwardedHeaderFilter} in order to choose from a
- * central place whether to extract and use, or to discard such headers.
- * See the Spring Framework reference for more on this filter.
*/
public static boolean isSameOrigin(ServerHttpRequest request) {
String origin = request.getHeaders().getOrigin();
@@ -68,9 +68,9 @@ public abstract class CorsUtils {
return true;
}
- UriComponents actualUrl = UriComponentsBuilder.fromHttpRequest(request).build();
- String actualHost = actualUrl.getHost();
- int actualPort = getPort(actualUrl.getScheme(), actualUrl.getPort());
+ URI uri = request.getURI();
+ String actualHost = uri.getHost();
+ int actualPort = getPort(uri.getScheme(), uri.getPort());
Assert.notNull(actualHost, "Actual request host must not be null");
Assert.isTrue(actualPort != -1, "Actual request port must not be undefined");
diff --git a/spring-web/src/main/java/org/springframework/web/util/WebUtils.java b/spring-web/src/main/java/org/springframework/web/util/WebUtils.java
index 6af42f4546..7dd736a7cb 100644
--- a/spring-web/src/main/java/org/springframework/web/util/WebUtils.java
+++ b/spring-web/src/main/java/org/springframework/web/util/WebUtils.java
@@ -18,11 +18,10 @@ package org.springframework.web.util;
import java.io.File;
import java.io.FileNotFoundException;
+import java.net.URI;
import java.util.Collection;
import java.util.Enumeration;
-import java.util.LinkedHashSet;
import java.util.Map;
-import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import javax.servlet.ServletContext;
@@ -138,16 +137,6 @@ public abstract class WebUtils {
/** Key for the mutex session attribute */
public static final String SESSION_MUTEX_ATTRIBUTE = WebUtils.class.getName() + ".MUTEX";
- private static final Set Note: this method may use values from "Forwarded"
- * (RFC 7239),
- * "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers,
- * if present, in order to reflect the client-originated address.
- * Consider using the {@code ForwardedHeaderFilter} in order to choose from a
- * central place whether to extract and use, or to discard such headers.
- * See the Spring Framework reference for more on this filter.
+ *
+ * Note: as of 5.1 this method ignores
+ * {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the
+ * client-originated address. Consider using the {@code ForwardedHeaderFilter}
+ * to extract and use, or to discard such headers.
+ *
* @return {@code true} if the request origin is valid, {@code false} otherwise
* @since 4.1.5
* @see RFC 6454: The Web Origin Concept
@@ -708,13 +696,12 @@ public abstract class WebUtils {
* Check if the request is a same-origin one, based on {@code Origin}, {@code Host},
* {@code Forwarded}, {@code X-Forwarded-Proto}, {@code X-Forwarded-Host} and
* @code X-Forwarded-Port} headers.
- * Note: this method uses values from "Forwarded"
- * (RFC 7239),
- * "X-Forwarded-Host", "X-Forwarded-Port", and "X-Forwarded-Proto" headers,
- * if present, in order to reflect the client-originated address.
- * Consider using the {@code ForwardedHeaderFilter} in order to choose from a
- * central place whether to extract and use, or to discard such headers.
- * See the Spring Framework reference for more on this filter.
+ *
+ * Note: as of 5.1 this method ignores
+ * {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the
+ * client-originated address. Consider using the {@code ForwardedHeaderFilter}
+ * to extract and use, or to discard such headers.
+
* @return {@code true} if the request is a same-origin one, {@code false} in case
* of cross-origin request
* @since 4.2
@@ -735,21 +722,12 @@ public abstract class WebUtils {
scheme = servletRequest.getScheme();
host = servletRequest.getServerName();
port = servletRequest.getServerPort();
- if (containsForwardedHeaders(servletRequest)) {
- UriComponents actualUrl = new UriComponentsBuilder()
- .scheme(scheme).host(host).port(port)
- .adaptFromForwardedHeaders(headers)
- .build();
- scheme = actualUrl.getScheme();
- host = actualUrl.getHost();
- port = actualUrl.getPort();
- }
}
else {
- UriComponents actualUrl = UriComponentsBuilder.fromHttpRequest(request).build();
- scheme = actualUrl.getScheme();
- host = actualUrl.getHost();
- port = actualUrl.getPort();
+ URI uri = request.getURI();
+ scheme = uri.getScheme();
+ host = uri.getHost();
+ port = uri.getPort();
}
UriComponents originUrl = UriComponentsBuilder.fromOriginHeader(origin).build();
@@ -757,15 +735,6 @@ public abstract class WebUtils {
getPort(scheme, port) == getPort(originUrl.getScheme(), originUrl.getPort()));
}
- private static boolean containsForwardedHeaders(HttpServletRequest request) {
- for (String headerName : FORWARDED_HEADER_NAMES) {
- if (request.getHeader(headerName) != null) {
- return true;
- }
- }
- return false;
- }
-
private static int getPort(@Nullable String scheme, int port) {
if (port == -1) {
if ("http".equals(scheme) || "ws".equals(scheme)) {
diff --git a/spring-web/src/test/java/org/springframework/web/cors/reactive/CorsUtilsTests.java b/spring-web/src/test/java/org/springframework/web/cors/reactive/CorsUtilsTests.java
index 53dec54593..96ee57a298 100644
--- a/spring-web/src/test/java/org/springframework/web/cors/reactive/CorsUtilsTests.java
+++ b/spring-web/src/test/java/org/springframework/web/cors/reactive/CorsUtilsTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2015 the original author or authors.
+ * Copyright 2002-2018 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.
@@ -16,15 +16,19 @@
package org.springframework.web.cors.reactive;
+import java.util.concurrent.atomic.AtomicReference;
+
import org.junit.Test;
+import reactor.core.publisher.Mono;
import org.springframework.http.HttpHeaders;
+import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.mock.http.server.reactive.test.MockServerHttpRequest;
+import org.springframework.mock.web.test.server.MockServerWebExchange;
+import org.springframework.web.filter.reactive.ForwardedHeaderFilter;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.get;
-import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.options;
+import static org.junit.Assert.*;
+import static org.springframework.mock.http.server.reactive.test.MockServerHttpRequest.*;
/**
* Test case for reactive {@link CorsUtils}.
@@ -35,19 +39,19 @@ public class CorsUtilsTests {
@Test
public void isCorsRequest() {
- MockServerHttpRequest request = get("/").header(HttpHeaders.ORIGIN, "http://domain.com").build();
+ ServerHttpRequest request = get("/").header(HttpHeaders.ORIGIN, "http://domain.com").build();
assertTrue(CorsUtils.isCorsRequest(request));
}
@Test
public void isNotCorsRequest() {
- MockServerHttpRequest request = get("/").build();
+ ServerHttpRequest request = get("/").build();
assertFalse(CorsUtils.isCorsRequest(request));
}
@Test
public void isPreFlightRequest() {
- MockServerHttpRequest request = options("/")
+ ServerHttpRequest request = options("/")
.header(HttpHeaders.ORIGIN, "http://domain.com")
.header(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "GET")
.build();
@@ -56,7 +60,7 @@ public class CorsUtilsTests {
@Test
public void isNotPreFlightRequest() {
- MockServerHttpRequest request = get("/").build();
+ ServerHttpRequest request = get("/").build();
assertFalse(CorsUtils.isPreFlightRequest(request));
request = options("/").header(HttpHeaders.ORIGIN, "http://domain.com").build();
@@ -68,31 +72,35 @@ public class CorsUtilsTests {
@Test // SPR-16262
public void isSameOriginWithXForwardedHeaders() {
- assertTrue(checkSameOriginWithXForwardedHeaders("mydomain1.com", -1, "https", null, -1, "https://mydomain1.com"));
- assertTrue(checkSameOriginWithXForwardedHeaders("mydomain1.com", 123, "https", null, -1, "https://mydomain1.com"));
- assertTrue(checkSameOriginWithXForwardedHeaders("mydomain1.com", -1, "https", "mydomain2.com", -1, "https://mydomain2.com"));
- assertTrue(checkSameOriginWithXForwardedHeaders("mydomain1.com", 123, "https", "mydomain2.com", -1, "https://mydomain2.com"));
- assertTrue(checkSameOriginWithXForwardedHeaders("mydomain1.com", -1, "https", "mydomain2.com", 456, "https://mydomain2.com:456"));
- assertTrue(checkSameOriginWithXForwardedHeaders("mydomain1.com", 123, "https", "mydomain2.com", 456, "https://mydomain2.com:456"));
+ String server = "mydomain1.com";
+ testWithXForwardedHeaders(server, -1, "https", null, -1, "https://mydomain1.com");
+ testWithXForwardedHeaders(server, 123, "https", null, -1, "https://mydomain1.com");
+ testWithXForwardedHeaders(server, -1, "https", "mydomain2.com", -1, "https://mydomain2.com");
+ testWithXForwardedHeaders(server, 123, "https", "mydomain2.com", -1, "https://mydomain2.com");
+ testWithXForwardedHeaders(server, -1, "https", "mydomain2.com", 456, "https://mydomain2.com:456");
+ testWithXForwardedHeaders(server, 123, "https", "mydomain2.com", 456, "https://mydomain2.com:456");
}
@Test // SPR-16262
public void isSameOriginWithForwardedHeader() {
- assertTrue(checkSameOriginWithForwardedHeader("mydomain1.com", -1, "proto=https", "https://mydomain1.com"));
- assertTrue(checkSameOriginWithForwardedHeader("mydomain1.com", 123, "proto=https", "https://mydomain1.com"));
- assertTrue(checkSameOriginWithForwardedHeader("mydomain1.com", -1, "proto=https; host=mydomain2.com", "https://mydomain2.com"));
- assertTrue(checkSameOriginWithForwardedHeader("mydomain1.com", 123, "proto=https; host=mydomain2.com", "https://mydomain2.com"));
- assertTrue(checkSameOriginWithForwardedHeader("mydomain1.com", -1, "proto=https; host=mydomain2.com:456", "https://mydomain2.com:456"));
- assertTrue(checkSameOriginWithForwardedHeader("mydomain1.com", 123, "proto=https; host=mydomain2.com:456", "https://mydomain2.com:456"));
+ String server = "mydomain1.com";
+ testWithForwardedHeader(server, -1, "proto=https", "https://mydomain1.com");
+ testWithForwardedHeader(server, 123, "proto=https", "https://mydomain1.com");
+ testWithForwardedHeader(server, -1, "proto=https; host=mydomain2.com", "https://mydomain2.com");
+ testWithForwardedHeader(server, 123, "proto=https; host=mydomain2.com", "https://mydomain2.com");
+ testWithForwardedHeader(server, -1, "proto=https; host=mydomain2.com:456", "https://mydomain2.com:456");
+ testWithForwardedHeader(server, 123, "proto=https; host=mydomain2.com:456", "https://mydomain2.com:456");
}
- private boolean checkSameOriginWithXForwardedHeaders(String serverName, int port, String forwardedProto, String forwardedHost, int forwardedPort, String originHeader) {
+ private void testWithXForwardedHeaders(String serverName, int port,
+ String forwardedProto, String forwardedHost, int forwardedPort, String originHeader) {
+
String url = "http://" + serverName;
if (port != -1) {
url = url + ":" + port;
}
- MockServerHttpRequest.BaseBuilder> builder = get(url)
- .header(HttpHeaders.ORIGIN, originHeader);
+
+ MockServerHttpRequest.BaseBuilder> builder = get(url).header(HttpHeaders.ORIGIN, originHeader);
if (forwardedProto != null) {
builder.header("X-Forwarded-Proto", forwardedProto);
}
@@ -102,18 +110,36 @@ public class CorsUtilsTests {
if (forwardedPort != -1) {
builder.header("X-Forwarded-Port", String.valueOf(forwardedPort));
}
- return CorsUtils.isSameOrigin(builder.build());
+
+ ServerHttpRequest request = adaptFromForwardedHeaders(builder);
+ assertTrue(CorsUtils.isSameOrigin(request));
}
- private boolean checkSameOriginWithForwardedHeader(String serverName, int port, String forwardedHeader, String originHeader) {
+ private void testWithForwardedHeader(String serverName, int port,
+ String forwardedHeader, String originHeader) {
+
String url = "http://" + serverName;
if (port != -1) {
url = url + ":" + port;
}
+
MockServerHttpRequest.BaseBuilder> builder = get(url)
.header("Forwarded", forwardedHeader)
.header(HttpHeaders.ORIGIN, originHeader);
- return CorsUtils.isSameOrigin(builder.build());
+
+ ServerHttpRequest request = adaptFromForwardedHeaders(builder);
+ assertTrue(CorsUtils.isSameOrigin(request));
+ }
+
+ // SPR-16668
+ private ServerHttpRequest adaptFromForwardedHeaders(MockServerHttpRequest.BaseBuilder> builder) {
+ AtomicReference Note: as of 5.1 this method ignores
+ * {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the
+ * client-originated address. Consider using the {@code ForwardedHeaderFilter}
+ * to extract and use, or to discard such headers.
+ *
* @return a URI builder
*/
UriBuilder uriBuilder();
diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeArgumentResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeArgumentResolver.java
index 7577b9ae35..44ed9865a4 100644
--- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeArgumentResolver.java
+++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeArgumentResolver.java
@@ -109,7 +109,7 @@ public class ServerWebExchangeArgumentResolver extends HandlerMethodArgumentReso
return timeZone != null ? timeZone.toZoneId() : ZoneId.systemDefault();
}
else if (UriBuilder.class == paramType || UriComponentsBuilder.class == paramType) {
- return UriComponentsBuilder.fromHttpRequest(exchange.getRequest());
+ return UriComponentsBuilder.fromUri(exchange.getRequest().getURI());
}
else {
// should never happen...
diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java
index e48b16deb0..530e826122 100644
--- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java
+++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/server/MockServerRequest.java
@@ -38,7 +38,6 @@ import org.springframework.http.HttpCookie;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpRange;
-import org.springframework.http.HttpRequest;
import org.springframework.http.MediaType;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.http.codec.multipart.Part;
@@ -139,7 +138,7 @@ public class MockServerRequest implements ServerRequest {
@Override
public UriBuilder uriBuilder() {
- return UriComponentsBuilder.fromHttpRequest(new ServerRequestAdapter());
+ return UriComponentsBuilder.fromUri(this.uri);
}
@Override
@@ -569,23 +568,4 @@ public class MockServerRequest implements ServerRequest {
}
- private final class ServerRequestAdapter implements HttpRequest {
-
- @Override
- public String getMethodValue() {
- return methodName();
- }
-
- @Override
- public URI getURI() {
- return MockServerRequest.this.uri;
- }
-
- @Override
- public HttpHeaders getHeaders() {
- return MockServerRequest.this.headers.headers;
- }
- }
-
-
}
diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java
index 237ad3d9e0..04fac7210d 100644
--- a/spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java
+++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/support/ServletUriComponentsBuilder.java
@@ -16,18 +16,14 @@
package org.springframework.web.servlet.support;
-import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
-import org.springframework.http.HttpRequest;
-import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
-import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UriUtils;
import org.springframework.web.util.UrlPathHelper;
@@ -81,17 +77,14 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
* Prepare a builder from the host, port, scheme, and context path of the
* given HttpServletRequest.
*
- * Note: This method extracts values from "Forwarded"
- * and "X-Forwarded-*" headers if found. See class-level docs.
- *
- * As of 4.3.15, this method replaces the contextPath with the value
- * of "X-Forwarded-Prefix" rather than prepending, thus aligning with
- * {@code ForwardedHeaderFiller}.
+ * Note: as of 5.1 this method ignores
+ * {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the
+ * client-originated address. Consider using the {@code ForwardedHeaderFilter}
+ * to extract and use, or to discard such headers.
*/
public static ServletUriComponentsBuilder fromContextPath(HttpServletRequest request) {
ServletUriComponentsBuilder builder = initFromRequest(request);
- String forwardedPrefix = getForwardedPrefix(request);
- builder.replacePath(forwardedPrefix != null ? forwardedPrefix : request.getContextPath());
+ builder.replacePath(request.getContextPath());
return builder;
}
@@ -103,12 +96,10 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
* {@code "/"} or {@code "*.do"}, the result will be the same as
* if calling {@link #fromContextPath(HttpServletRequest)}.
*
- * Note: This method extracts values from "Forwarded"
- * and "X-Forwarded-*" headers if found. See class-level docs.
- *
- * As of 4.3.15, this method replaces the contextPath with the value
- * of "X-Forwarded-Prefix" rather than prepending, thus aligning with
- * {@code ForwardedHeaderFiller}.
+ * Note: as of 5.1 this method ignores
+ * {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the
+ * client-originated address. Consider using the {@code ForwardedHeaderFilter}
+ * to extract and use, or to discard such headers.
*/
public static ServletUriComponentsBuilder fromServletMapping(HttpServletRequest request) {
ServletUriComponentsBuilder builder = fromContextPath(request);
@@ -122,16 +113,14 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
* Prepare a builder from the host, port, scheme, and path (but not the query)
* of the HttpServletRequest.
*
- * Note: This method extracts values from "Forwarded"
- * and "X-Forwarded-*" headers if found. See class-level docs.
- *
- * As of 4.3.15, this method replaces the contextPath with the value
- * of "X-Forwarded-Prefix" rather than prepending, thus aligning with
- * {@code ForwardedHeaderFiller}.
+ * Note: as of 5.1 this method ignores
+ * {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the
+ * client-originated address. Consider using the {@code ForwardedHeaderFilter}
+ * to extract and use, or to discard such headers.
*/
public static ServletUriComponentsBuilder fromRequestUri(HttpServletRequest request) {
ServletUriComponentsBuilder builder = initFromRequest(request);
- builder.initPath(getRequestUriWithForwardedPrefix(request));
+ builder.initPath(request.getRequestURI());
return builder;
}
@@ -139,16 +128,14 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
* Prepare a builder by copying the scheme, host, port, path, and
* query string of an HttpServletRequest.
*
- * Note: This method extracts values from "Forwarded"
- * and "X-Forwarded-*" headers if found. See class-level docs.
- *
- * As of 4.3.15, this method replaces the contextPath with the value
- * of "X-Forwarded-Prefix" rather than prepending, thus aligning with
- * {@code ForwardedHeaderFiller}.
+ * Note: as of 5.1 this method ignores
+ * {@code "Forwarded"} and {@code "X-Forwarded-*"} headers that specify the
+ * client-originated address. Consider using the {@code ForwardedHeaderFilter}
+ * to extract and use, or to discard such headers.
*/
public static ServletUriComponentsBuilder fromRequest(HttpServletRequest request) {
ServletUriComponentsBuilder builder = initFromRequest(request);
- builder.initPath(getRequestUriWithForwardedPrefix(request));
+ builder.initPath(request.getRequestURI());
builder.query(request.getQueryString());
return builder;
}
@@ -157,11 +144,9 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
* Initialize a builder with a scheme, host,and port (but not path and query).
*/
private static ServletUriComponentsBuilder initFromRequest(HttpServletRequest request) {
- HttpRequest httpRequest = new ServletServerHttpRequest(request);
- UriComponents uriComponents = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
- String scheme = uriComponents.getScheme();
- String host = uriComponents.getHost();
- int port = uriComponents.getPort();
+ String scheme = request.getScheme();
+ String host = request.getServerName();
+ int port = request.getServerPort();
ServletUriComponentsBuilder builder = new ServletUriComponentsBuilder();
builder.scheme(scheme);
@@ -172,37 +157,6 @@ public class ServletUriComponentsBuilder extends UriComponentsBuilder {
return builder;
}
- @Nullable
- private static String getForwardedPrefix(HttpServletRequest request) {
- String prefix = null;
- Enumeration