Adds PreserveHostHeader filter
This filter sets a request attribute to true. The NettyRoutingFilter then inspects this attribute and resets the host header, overwritting the value set by the netty http client. fixes gh-133
This commit is contained in:
@@ -349,6 +349,25 @@ spring:
|
||||
|
||||
This will prefix `/mypath` to the path of all matching requests. So a request to `/hello`, would be sent to `/mypath/hello`.
|
||||
|
||||
=== PreserveHostHeader GatewayFilter Factory
|
||||
The PreserveHostHeader GatewayFilter Factory has not parameters. This filter, sets a request attribute that the routing filter will inspect to determine if the original host header should be sent, rather than the host header determined by the http client.
|
||||
|
||||
.application.yml
|
||||
[source,yaml]
|
||||
----
|
||||
spring:
|
||||
cloud:
|
||||
gateway:
|
||||
routes:
|
||||
# =====================================
|
||||
- id: preserve_host_route
|
||||
uri: http://example.org
|
||||
filters:
|
||||
- PreserveHostHeader
|
||||
----
|
||||
|
||||
This will prefix `/mypath` to the path of all matching requests. So a request to `/hello`, would be sent to `/mypath/hello`.
|
||||
|
||||
=== RequestRateLimiter GatewayFilter Factory
|
||||
|
||||
The RequestRateLimiter GatewayFilter Factory takes three parameters: `replenishRate`, `burstCapacity` & `keyResolverName`.
|
||||
|
||||
@@ -44,6 +44,7 @@ import org.springframework.cloud.gateway.filter.factory.AddResponseHeaderGateway
|
||||
import org.springframework.cloud.gateway.filter.factory.GatewayFilterFactory;
|
||||
import org.springframework.cloud.gateway.filter.factory.HystrixGatewayFilterFactory;
|
||||
import org.springframework.cloud.gateway.filter.factory.PrefixPathGatewayFilterFactory;
|
||||
import org.springframework.cloud.gateway.filter.factory.PreserveHostHeaderGatewayFilterFactory;
|
||||
import org.springframework.cloud.gateway.filter.factory.RedirectToGatewayFilterFactory;
|
||||
import org.springframework.cloud.gateway.filter.factory.RemoveNonProxyHeadersGatewayFilterFactory;
|
||||
import org.springframework.cloud.gateway.filter.factory.RemoveRequestHeaderGatewayFilterFactory;
|
||||
@@ -322,6 +323,11 @@ public class GatewayAutoConfiguration {
|
||||
return new PrefixPathGatewayFilterFactory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PreserveHostHeaderGatewayFilterFactory preserveHostHeaderGatewayFilterFactory() {
|
||||
return new PreserveHostHeaderGatewayFilterFactory();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedirectToGatewayFilterFactory redirectToGatewayFilterFactory() {
|
||||
return new RedirectToGatewayFilterFactory();
|
||||
|
||||
@@ -27,7 +27,11 @@ import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.http.server.reactive.ServerHttpResponse;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.*;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.CLIENT_RESPONSE_ATTR;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.PRESERVE_HOST_HEADER_ATTRIBUTE;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.isAlreadyRouted;
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setAlreadyRouted;
|
||||
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.handler.codec.http.DefaultHttpHeaders;
|
||||
@@ -71,11 +75,18 @@ public class NettyRoutingFilter implements GlobalFilter, Ordered {
|
||||
final DefaultHttpHeaders httpHeaders = new DefaultHttpHeaders();
|
||||
request.getHeaders().forEach(httpHeaders::set);
|
||||
|
||||
boolean preserveHost = exchange.getAttributeOrDefault(PRESERVE_HOST_HEADER_ATTRIBUTE, false);
|
||||
|
||||
return this.httpClient.request(method, url, req -> {
|
||||
final HttpClientRequest proxyRequest = req.options(NettyPipeline.SendOptions::flushOnEach)
|
||||
.failOnClientError(false)
|
||||
.headers(httpHeaders);
|
||||
|
||||
if (preserveHost) {
|
||||
String host = request.getHeaders().getFirst(HttpHeaders.HOST);
|
||||
proxyRequest.header(HttpHeaders.HOST, host);
|
||||
}
|
||||
|
||||
return proxyRequest.sendHeaders() //I shouldn't need this
|
||||
.send(request.getBody()
|
||||
.map(DataBuffer::asByteBuffer)
|
||||
|
||||
@@ -65,14 +65,13 @@ public class WebClientHttpRoutingFilter implements GlobalFilter, Ordered {
|
||||
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
|
||||
//TODO: support forms
|
||||
|
||||
HttpMethod method = request.getMethod();
|
||||
|
||||
RequestBodySpec bodySpec = this.webClient.method(method)
|
||||
.uri(requestUrl)
|
||||
.headers(httpHeaders -> {
|
||||
httpHeaders.addAll(request.getHeaders());
|
||||
//TODO: can this support preserviceHostHeader?
|
||||
httpHeaders.remove(HttpHeaders.HOST);
|
||||
});
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* Copyright 2013-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.gateway.filter.factory;
|
||||
|
||||
import org.springframework.cloud.gateway.filter.GatewayFilter;
|
||||
import org.springframework.tuple.Tuple;
|
||||
|
||||
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.PRESERVE_HOST_HEADER_ATTRIBUTE;
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class PreserveHostHeaderGatewayFilterFactory implements GatewayFilterFactory {
|
||||
|
||||
@Override
|
||||
public GatewayFilter apply(Tuple args) {
|
||||
return apply();
|
||||
}
|
||||
|
||||
public GatewayFilter apply() {
|
||||
return (exchange, chain) -> {
|
||||
exchange.getAttributes().put(PRESERVE_HOST_HEADER_ATTRIBUTE, true);
|
||||
return chain.filter(exchange);
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ import org.springframework.cloud.gateway.filter.factory.AddRequestParameterGatew
|
||||
import org.springframework.cloud.gateway.filter.factory.AddResponseHeaderGatewayFilterFactory;
|
||||
import org.springframework.cloud.gateway.filter.factory.HystrixGatewayFilterFactory;
|
||||
import org.springframework.cloud.gateway.filter.factory.PrefixPathGatewayFilterFactory;
|
||||
import org.springframework.cloud.gateway.filter.factory.PreserveHostHeaderGatewayFilterFactory;
|
||||
import org.springframework.cloud.gateway.filter.factory.RedirectToGatewayFilterFactory;
|
||||
import org.springframework.cloud.gateway.filter.factory.RemoveNonProxyHeadersGatewayFilterFactory;
|
||||
import org.springframework.cloud.gateway.filter.factory.RemoveRequestHeaderGatewayFilterFactory;
|
||||
@@ -106,6 +107,10 @@ public class GatewayFilterSpec extends UriSpec {
|
||||
return filter(getBean(PrefixPathGatewayFilterFactory.class).apply(prefix));
|
||||
}
|
||||
|
||||
public GatewayFilterSpec preserveHostHeader() {
|
||||
return filter(getBean(PreserveHostHeaderGatewayFilterFactory.class).apply());
|
||||
}
|
||||
|
||||
public GatewayFilterSpec redirect(int status, URI url) {
|
||||
return redirect(String.valueOf(status), url.toString());
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ public class ServerWebExchangeUtils {
|
||||
|
||||
private static final Log logger = LogFactory.getLog(ServerWebExchangeUtils.class);
|
||||
|
||||
public static final String PRESERVE_HOST_HEADER_ATTRIBUTE = qualify("preserveHostHeader");
|
||||
public static final String URI_TEMPLATE_VARIABLES_ATTRIBUTE = qualify("uriTemplateVariables");
|
||||
|
||||
public static final String CLIENT_RESPONSE_ATTR = qualify("webHandlerClientResponse");
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright 2013-2017 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.gateway.filter.factory;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
||||
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
|
||||
import org.springframework.cloud.gateway.test.BaseWebClientTests;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
|
||||
import static org.springframework.cloud.gateway.test.TestUtils.getMap;
|
||||
|
||||
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(webEnvironment = RANDOM_PORT)
|
||||
@DirtiesContext
|
||||
public class PreserveHostHeaderGatewayFilterFactoryTests extends BaseWebClientTests {
|
||||
|
||||
@Test
|
||||
public void setRequestHeaderFilterWorks() {
|
||||
testClient.get().uri("/headers")
|
||||
.header("Host", "www.preservehostheader.org")
|
||||
.exchange()
|
||||
.expectStatus().isOk()
|
||||
.expectBody(Map.class)
|
||||
.consumeWith(result -> {
|
||||
Map<String, Object> headers = getMap(result.getResponseBody(), "headers");
|
||||
assertThat(headers).containsEntry("Host", "myhost.net");
|
||||
});
|
||||
}
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@SpringBootConfiguration
|
||||
@Import(DefaultTestConfig.class)
|
||||
public static class TestConfig {
|
||||
@Value("${test.uri}")
|
||||
String uri;
|
||||
|
||||
@Bean
|
||||
public RouteLocator testRouteLocator(RouteLocatorBuilder builder) {
|
||||
return builder.routes().route("test_preserve_host_header",
|
||||
r -> r.order(-1)
|
||||
.host("**.preservehostheader.org")
|
||||
.prefixPath("/httpbin")
|
||||
.preserveHostHeader()
|
||||
.setRequestHeader("Host", "myhost.net")
|
||||
.uri(uri))
|
||||
.build();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user