|
|
@ -19,18 +19,25 @@ package org.springframework.cloud.gateway.filter.headers; |
|
|
|
|
|
|
|
|
|
|
|
import java.net.InetAddress; |
|
|
|
import java.net.InetAddress; |
|
|
|
import java.net.InetSocketAddress; |
|
|
|
import java.net.InetSocketAddress; |
|
|
|
|
|
|
|
import java.net.URI; |
|
|
|
|
|
|
|
import java.util.LinkedHashSet; |
|
|
|
|
|
|
|
|
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.Test; |
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.mock.http.server.reactive.MockServerHttpRequest; |
|
|
|
import org.springframework.mock.http.server.reactive.MockServerHttpRequest; |
|
|
|
import org.springframework.mock.web.server.MockServerWebExchange; |
|
|
|
import org.springframework.mock.web.server.MockServerWebExchange; |
|
|
|
|
|
|
|
import org.springframework.web.server.ServerWebExchange; |
|
|
|
|
|
|
|
import org.springframework.web.util.UriComponentsBuilder; |
|
|
|
|
|
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.assertj.core.api.Assertions.assertThat; |
|
|
|
import static org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter.X_FORWARDED_FOR_HEADER; |
|
|
|
import static org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter.X_FORWARDED_FOR_HEADER; |
|
|
|
import static org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter.X_FORWARDED_HOST_HEADER; |
|
|
|
import static org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter.X_FORWARDED_HOST_HEADER; |
|
|
|
import static org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter.X_FORWARDED_PORT_HEADER; |
|
|
|
import static org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter.X_FORWARDED_PORT_HEADER; |
|
|
|
|
|
|
|
import static org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter.X_FORWARDED_PREFIX_HEADER; |
|
|
|
import static org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter.X_FORWARDED_PROTO_HEADER; |
|
|
|
import static org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter.X_FORWARDED_PROTO_HEADER; |
|
|
|
|
|
|
|
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR; |
|
|
|
|
|
|
|
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* @author Spencer Gibb |
|
|
|
* @author Spencer Gibb |
|
|
@ -131,6 +138,7 @@ public class XForwardedHeadersFilterTests { |
|
|
|
.header(X_FORWARDED_HOST_HEADER, "example.com") |
|
|
|
.header(X_FORWARDED_HOST_HEADER, "example.com") |
|
|
|
.header(X_FORWARDED_PORT_HEADER, "443") |
|
|
|
.header(X_FORWARDED_PORT_HEADER, "443") |
|
|
|
.header(X_FORWARDED_PROTO_HEADER, "https") |
|
|
|
.header(X_FORWARDED_PROTO_HEADER, "https") |
|
|
|
|
|
|
|
.header(X_FORWARDED_PREFIX_HEADER,"/prefix") |
|
|
|
.build(); |
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
|
XForwardedHeadersFilter filter = new XForwardedHeadersFilter(); |
|
|
|
XForwardedHeadersFilter filter = new XForwardedHeadersFilter(); |
|
|
@ -138,16 +146,98 @@ public class XForwardedHeadersFilterTests { |
|
|
|
filter.setHostAppend(false); |
|
|
|
filter.setHostAppend(false); |
|
|
|
filter.setPortAppend(false); |
|
|
|
filter.setPortAppend(false); |
|
|
|
filter.setProtoAppend(false); |
|
|
|
filter.setProtoAppend(false); |
|
|
|
|
|
|
|
filter.setPrefixAppend(false); |
|
|
|
|
|
|
|
|
|
|
|
HttpHeaders headers = filter.filter(request.getHeaders(), MockServerWebExchange.from(request)); |
|
|
|
HttpHeaders headers = filter.filter(request.getHeaders(), MockServerWebExchange.from(request)); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(headers).containsKeys(X_FORWARDED_FOR_HEADER, X_FORWARDED_HOST_HEADER, |
|
|
|
assertThat(headers).containsKeys(X_FORWARDED_FOR_HEADER, X_FORWARDED_HOST_HEADER, |
|
|
|
X_FORWARDED_PORT_HEADER, X_FORWARDED_PROTO_HEADER); |
|
|
|
X_FORWARDED_PORT_HEADER, X_FORWARDED_PROTO_HEADER,X_FORWARDED_PREFIX_HEADER); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(headers.getFirst(X_FORWARDED_FOR_HEADER)).isEqualTo("10.0.0.1"); |
|
|
|
assertThat(headers.getFirst(X_FORWARDED_FOR_HEADER)).isEqualTo("10.0.0.1"); |
|
|
|
assertThat(headers.getFirst(X_FORWARDED_HOST_HEADER)).isEqualTo("localhost:8080"); |
|
|
|
assertThat(headers.getFirst(X_FORWARDED_HOST_HEADER)).isEqualTo("localhost:8080"); |
|
|
|
assertThat(headers.getFirst(X_FORWARDED_PORT_HEADER)).isEqualTo("8080"); |
|
|
|
assertThat(headers.getFirst(X_FORWARDED_PORT_HEADER)).isEqualTo("8080"); |
|
|
|
assertThat(headers.getFirst(X_FORWARDED_PROTO_HEADER)).isEqualTo("http"); |
|
|
|
assertThat(headers.getFirst(X_FORWARDED_PROTO_HEADER)).isEqualTo("http"); |
|
|
|
|
|
|
|
assertThat(headers.getFirst(X_FORWARDED_PREFIX_HEADER)).isEqualTo("/prefix"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void prefixToInfer() throws Exception { |
|
|
|
|
|
|
|
MockServerHttpRequest request = MockServerHttpRequest |
|
|
|
|
|
|
|
.get("http://originalhost:8080/prefix/get") |
|
|
|
|
|
|
|
.remoteAddress(new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 80)) |
|
|
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
XForwardedHeadersFilter filter = new XForwardedHeadersFilter(); |
|
|
|
|
|
|
|
filter.setPrefixAppend(true); |
|
|
|
|
|
|
|
filter.setPrefixEnabled(true); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ServerWebExchange exchange = MockServerWebExchange.from(request); |
|
|
|
|
|
|
|
LinkedHashSet<URI> originalUris = new LinkedHashSet<>(); |
|
|
|
|
|
|
|
originalUris.add(UriComponentsBuilder.fromUriString("http://originalhost:8080/prefix/get/").build().toUri()); //trailing slash
|
|
|
|
|
|
|
|
exchange.getAttributes().put(GATEWAY_ORIGINAL_REQUEST_URL_ATTR, originalUris); |
|
|
|
|
|
|
|
URI requestUri = UriComponentsBuilder.fromUriString("http://routedservice:8090/get").build().toUri(); |
|
|
|
|
|
|
|
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUri); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpHeaders headers = filter.filter(request.getHeaders(), exchange); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(headers).containsKeys(X_FORWARDED_PREFIX_HEADER); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(headers.getFirst(X_FORWARDED_PREFIX_HEADER)).isEqualTo("/prefix"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void noPrefixToInfer() throws Exception { |
|
|
|
|
|
|
|
MockServerHttpRequest request = MockServerHttpRequest |
|
|
|
|
|
|
|
.get("http://originalhost:8080/get") |
|
|
|
|
|
|
|
.remoteAddress(new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 80)) |
|
|
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
XForwardedHeadersFilter filter = new XForwardedHeadersFilter(); |
|
|
|
|
|
|
|
filter.setPrefixAppend(true); |
|
|
|
|
|
|
|
filter.setPrefixEnabled(true); |
|
|
|
|
|
|
|
filter.setForEnabled(false); |
|
|
|
|
|
|
|
filter.setHostEnabled(false); |
|
|
|
|
|
|
|
filter.setPortEnabled(false); |
|
|
|
|
|
|
|
filter.setProtoEnabled(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ServerWebExchange exchange = MockServerWebExchange.from(request); |
|
|
|
|
|
|
|
LinkedHashSet<URI> originalUris = new LinkedHashSet<>(); |
|
|
|
|
|
|
|
originalUris.add(UriComponentsBuilder.fromUriString("http://originalhost:8080/get/").build().toUri()); |
|
|
|
|
|
|
|
exchange.getAttributes().put(GATEWAY_ORIGINAL_REQUEST_URL_ATTR, originalUris); |
|
|
|
|
|
|
|
URI requestUri = UriComponentsBuilder.fromUriString("http://routedservice:8090/get").build().toUri(); |
|
|
|
|
|
|
|
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUri); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpHeaders headers = filter.filter(request.getHeaders(), exchange); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(headers).isEmpty(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
|
|
|
|
public void routedPathInRequestPathButNotPrefix() throws Exception { |
|
|
|
|
|
|
|
MockServerHttpRequest request = MockServerHttpRequest |
|
|
|
|
|
|
|
.get("http://originalhost:8080/get") |
|
|
|
|
|
|
|
.remoteAddress(new InetSocketAddress(InetAddress.getByName("10.0.0.1"), 80)) |
|
|
|
|
|
|
|
.build(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
XForwardedHeadersFilter filter = new XForwardedHeadersFilter(); |
|
|
|
|
|
|
|
filter.setPrefixAppend(true); |
|
|
|
|
|
|
|
filter.setPrefixEnabled(true); |
|
|
|
|
|
|
|
filter.setForEnabled(false); |
|
|
|
|
|
|
|
filter.setHostEnabled(false); |
|
|
|
|
|
|
|
filter.setPortEnabled(false); |
|
|
|
|
|
|
|
filter.setProtoEnabled(false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ServerWebExchange exchange = MockServerWebExchange.from(request); |
|
|
|
|
|
|
|
LinkedHashSet<URI> originalUris = new LinkedHashSet<>(); |
|
|
|
|
|
|
|
originalUris.add(UriComponentsBuilder.fromUriString("http://originalhost:8080/one/two/three").build().toUri()); |
|
|
|
|
|
|
|
exchange.getAttributes().put(GATEWAY_ORIGINAL_REQUEST_URL_ATTR, originalUris); |
|
|
|
|
|
|
|
URI requestUri = UriComponentsBuilder.fromUriString("http://routedservice:8090/two").build().toUri(); |
|
|
|
|
|
|
|
exchange.getAttributes().put(GATEWAY_REQUEST_URL_ATTR, requestUri); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpHeaders headers = filter.filter(request.getHeaders(), exchange); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
assertThat(headers).isEmpty(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Test |
|
|
|
@Test |
|
|
@ -162,6 +252,7 @@ public class XForwardedHeadersFilterTests { |
|
|
|
filter.setHostEnabled(false); |
|
|
|
filter.setHostEnabled(false); |
|
|
|
filter.setPortEnabled(false); |
|
|
|
filter.setPortEnabled(false); |
|
|
|
filter.setProtoEnabled(false); |
|
|
|
filter.setProtoEnabled(false); |
|
|
|
|
|
|
|
filter.setPrefixEnabled(false); |
|
|
|
|
|
|
|
|
|
|
|
HttpHeaders headers = filter.filter(request.getHeaders(), MockServerWebExchange.from(request)); |
|
|
|
HttpHeaders headers = filter.filter(request.getHeaders(), MockServerWebExchange.from(request)); |
|
|
|
|
|
|
|
|
|
|
|