Support X-Forwarded-Ssl
Issue: SPR-16863
This commit is contained in:
@@ -743,6 +743,10 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
|
|||||||
scheme(matcher.group(1).trim());
|
scheme(matcher.group(1).trim());
|
||||||
port(null);
|
port(null);
|
||||||
}
|
}
|
||||||
|
else if (isForwardedSslOn(headers)) {
|
||||||
|
scheme("https");
|
||||||
|
port(null);
|
||||||
|
}
|
||||||
matcher = FORWARDED_HOST_PATTERN.matcher(forwardedToUse);
|
matcher = FORWARDED_HOST_PATTERN.matcher(forwardedToUse);
|
||||||
if (matcher.find()) {
|
if (matcher.find()) {
|
||||||
adaptForwardedHost(matcher.group(1).trim());
|
adaptForwardedHost(matcher.group(1).trim());
|
||||||
@@ -754,6 +758,10 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
|
|||||||
scheme(StringUtils.tokenizeToStringArray(protocolHeader, ",")[0]);
|
scheme(StringUtils.tokenizeToStringArray(protocolHeader, ",")[0]);
|
||||||
port(null);
|
port(null);
|
||||||
}
|
}
|
||||||
|
else if (isForwardedSslOn(headers)) {
|
||||||
|
scheme("https");
|
||||||
|
port(null);
|
||||||
|
}
|
||||||
|
|
||||||
String hostHeader = headers.getFirst("X-Forwarded-Host");
|
String hostHeader = headers.getFirst("X-Forwarded-Host");
|
||||||
if (StringUtils.hasText(hostHeader)) {
|
if (StringUtils.hasText(hostHeader)) {
|
||||||
@@ -780,6 +788,11 @@ public class UriComponentsBuilder implements UriBuilder, Cloneable {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private boolean isForwardedSslOn(HttpHeaders headers) {
|
||||||
|
String forwardedSsl = headers.getFirst("X-Forwarded-Ssl");
|
||||||
|
return StringUtils.hasText(forwardedSsl) && forwardedSsl.equalsIgnoreCase("on");
|
||||||
|
}
|
||||||
|
|
||||||
private void adaptForwardedHost(String hostToUse) {
|
private void adaptForwardedHost(String hostToUse) {
|
||||||
int portSeparatorIdx = hostToUse.lastIndexOf(':');
|
int portSeparatorIdx = hostToUse.lastIndexOf(':');
|
||||||
if (portSeparatorIdx > hostToUse.lastIndexOf(']')) {
|
if (portSeparatorIdx > hostToUse.lastIndexOf(']')) {
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import org.springframework.mock.web.test.MockHttpServletRequest;
|
|||||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
|
import static org.mockito.Mockito.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unit tests for {@link ForwardedHeaderFilter}.
|
* Unit tests for {@link ForwardedHeaderFilter}.
|
||||||
@@ -458,7 +459,7 @@ public class ForwardedHeaderFilterTests {
|
|||||||
};
|
};
|
||||||
|
|
||||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
FilterChain filterChain = new MockFilterChain(new HttpServlet() {}, this.filter, filter);
|
FilterChain filterChain = new MockFilterChain(mock(HttpServlet.class), this.filter, filter);
|
||||||
filterChain.doFilter(request, response);
|
filterChain.doFilter(request, response);
|
||||||
|
|
||||||
return response.getRedirectedUrl();
|
return response.getRedirectedUrl();
|
||||||
|
|||||||
@@ -426,6 +426,21 @@ public class UriComponentsBuilderTests {
|
|||||||
assertEquals(-1, result.getPort());
|
assertEquals(-1, result.getPort());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test // SPR-16863
|
||||||
|
public void fromHttpRequestWithForwardedSsl() {
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
request.setScheme("http");
|
||||||
|
request.setServerName("example.org");
|
||||||
|
request.setServerPort(10080);
|
||||||
|
request.addHeader("X-Forwarded-Ssl", "on");
|
||||||
|
|
||||||
|
HttpRequest httpRequest = new ServletServerHttpRequest(request);
|
||||||
|
UriComponents result = UriComponentsBuilder.fromHttpRequest(httpRequest).build();
|
||||||
|
|
||||||
|
assertEquals("https", result.getScheme());
|
||||||
|
assertEquals("example.org", result.getHost());
|
||||||
|
assertEquals(-1, result.getPort());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void fromHttpRequestWithForwardedHostWithForwardedScheme() {
|
public void fromHttpRequestWithForwardedHostWithForwardedScheme() {
|
||||||
|
|||||||
Reference in New Issue
Block a user