diff --git a/docs/src/main/asciidoc/spring-cloud-gateway.adoc b/docs/src/main/asciidoc/spring-cloud-gateway.adoc index 85e37d248..42ae3a38d 100644 --- a/docs/src/main/asciidoc/spring-cloud-gateway.adoc +++ b/docs/src/main/asciidoc/spring-cloud-gateway.adoc @@ -143,6 +143,9 @@ TODO: document AddResponseHeader Route Filter === Hystrix Route Filter TODO: document Hystrix Route Filter +=== PrefixPath Route Filter +TODO: document PrefixPath Route Filter + === RedirectTo Route Filter TODO: document RedirectTo Route Filter diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java index 723d8ac83..4584225dc 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java @@ -27,6 +27,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.client.loadbalancer.LoadBalancerClient; import org.springframework.cloud.gateway.actuate.GatewayEndpoint; +import org.springframework.cloud.gateway.filter.route.PrefixPathRouteFilter; import org.springframework.cloud.gateway.model.RouteLocator; import org.springframework.cloud.gateway.model.RouteWriter; import org.springframework.cloud.gateway.filter.GlobalFilter; @@ -236,6 +237,11 @@ public class GatewayAutoConfiguration { } } + @Bean(name = "PrefixPathRouteFilter") + public PrefixPathRouteFilter prefixPathRouteFilter() { + return new PrefixPathRouteFilter(); + } + @Bean(name = "RedirectToRouteFilter") public RedirectToRouteFilter redirectToRouteFilter() { return new RedirectToRouteFilter(); diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/route/PrefixPathRouteFilter.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/route/PrefixPathRouteFilter.java new file mode 100644 index 000000000..7d33b1d74 --- /dev/null +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/route/PrefixPathRouteFilter.java @@ -0,0 +1,45 @@ +/* + * 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.route; + +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.web.server.WebFilter; + +/** + * @author Spencer Gibb + */ +public class PrefixPathRouteFilter implements RouteFilter { + + @Override + @SuppressWarnings("unchecked") + public WebFilter apply(String... args) { + validate(1, args); + final String prefix = args[0]; + + return (exchange, chain) -> { + ServerHttpRequest req = exchange.getRequest(); + String newPath = prefix + req.getURI().getPath(); + + ServerHttpRequest request = req.mutate() + .path(newPath) + .build(); + + return chain.filter(exchange.mutate().request(request).build()); + }; + } +} diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java index 5584854c3..49003aab3 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/BaseWebClientTests.java @@ -159,27 +159,6 @@ public class BaseWebClientTests { }); } - @Bean(name="PrefixPathRouteFilter") - public RouteFilter prefixPathRouteFilter() { - return new RouteFilter() { - @Override - public WebFilter apply(String... args) { - validate(1, args); - final String prefix = args[0]; - return (exchange, chain) -> { - ServerHttpRequest req = exchange.getRequest(); - String newPath = prefix + req.getURI().getPath(); - - ServerHttpRequest request = req.mutate() - .path(newPath) - .build(); - - return chain.filter(exchange.mutate().request(request).build()); - }; - } - }; - } - @Bean @Order(500) public GlobalFilter modifyResponseFilter() {