Adds setRequestHeader filter

fixes gh-138
This commit is contained in:
Spencer Gibb
2017-12-18 15:04:32 -05:00
parent ae206985e2
commit fb9c671123
4 changed files with 147 additions and 0 deletions

View File

@@ -53,6 +53,7 @@ import org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilter
import org.springframework.cloud.gateway.filter.factory.SecureHeadersGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.SecureHeadersProperties;
import org.springframework.cloud.gateway.filter.factory.SetPathGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.SetRequestHeaderGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.SetResponseHeaderGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.SetStatusGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
@@ -368,6 +369,11 @@ public class GatewayAutoConfiguration {
return new SecureHeadersGatewayFilterFactory(properties);
}
@Bean
public SetRequestHeaderGatewayFilterFactory setRequestHeaderGatewayFilterFactory() {
return new SetRequestHeaderGatewayFilterFactory();
}
@Bean
public SetResponseHeaderGatewayFilterFactory setResponseHeaderGatewayFilterFactory() {
return new SetResponseHeaderGatewayFilterFactory();

View File

@@ -0,0 +1,53 @@
/*
* 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.Arrays;
import java.util.List;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.tuple.Tuple;
/**
* @author Spencer Gibb
*/
public class SetRequestHeaderGatewayFilterFactory implements GatewayFilterFactory {
@Override
public List<String> argNames() {
return Arrays.asList(NAME_KEY, VALUE_KEY);
}
@Override
public GatewayFilter apply(Tuple args) {
String name = args.getString(NAME_KEY);
String value = args.getString(VALUE_KEY);
return apply(name, value);
}
public GatewayFilter apply(String name, String value) {
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest().mutate()
.headers(httpHeaders -> httpHeaders.set(name, value))
.build();
return chain.filter(exchange.mutate().request(request).build());
};
}
}

View File

@@ -38,6 +38,7 @@ import org.springframework.cloud.gateway.filter.factory.RequestRateLimiterGatewa
import org.springframework.cloud.gateway.filter.factory.RewritePathGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.SecureHeadersGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.SetPathGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.SetRequestHeaderGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.SetResponseHeaderGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.SetStatusGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
@@ -164,6 +165,10 @@ public class GatewayFilterSpec extends UriSpec {
return filter(getBean(SetPathGatewayFilterFactory.class).apply(template));
}
public GatewayFilterSpec setRequestHeader(String headerName, String headerValue) {
return filter(getBean(SetRequestHeaderGatewayFilterFactory.class).apply(headerName, headerValue));
}
public GatewayFilterSpec setResponseHeader(String headerName, String headerValue) {
return filter(getBean(SetResponseHeaderGatewayFilterFactory.class).apply(headerName, headerValue));
}

View File

@@ -0,0 +1,83 @@
/*
* 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
* @author Biju Kunjummen
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@DirtiesContext
public class SetRequestHeaderGatewayFilterFactoryTests extends BaseWebClientTests {
@Test
public void setRequestHeaderFilterWorks() {
testClient.get().uri("/headers")
.header("Host", "www.setrequestheader.org")
.exchange()
.expectStatus().isOk()
.expectBody(Map.class)
.consumeWith(result -> {
Map<String, Object> headers = getMap(result.getResponseBody(), "headers");
assertThat(headers).containsEntry("X-Req-Foo", "Second");
});
}
@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_set_request_header",
r -> r.order(-1)
.host("**.setrequestheader.org")
.prefixPath("/httpbin")
.addRequestHeader("X-Req-Foo", "First")
.setRequestHeader("X-Req-Foo", "Second")
.uri(uri))
.build();
}
}
}