From 50af2ca84ec467faebc3a70f90cc9dac7a34901c Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Tue, 24 Apr 2018 15:58:58 -0400 Subject: [PATCH] Add filter which will set the path in the request to be the path in the forward URI. Fixes #285. --- .../main/asciidoc/spring-cloud-gateway.adoc | 2 +- .../config/GatewayAutoConfiguration.java | 6 +++ .../gateway/filter/ForwardPathFilter.java | 53 +++++++++++++++++++ .../cloud/gateway/test/ForwardTests.java | 9 ++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ForwardPathFilter.java diff --git a/docs/src/main/asciidoc/spring-cloud-gateway.adoc b/docs/src/main/asciidoc/spring-cloud-gateway.adoc index fd12b00b..b80b7ce4 100644 --- a/docs/src/main/asciidoc/spring-cloud-gateway.adoc +++ b/docs/src/main/asciidoc/spring-cloud-gateway.adoc @@ -705,7 +705,7 @@ TODO: document ordering === Forward Routing Filter -The `ForwardRoutingFilter` looks for a URI in the exchange attribute `ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR`. If the url has a `forward` scheme (ie `forward:///localendpoint`), it will use the Spring `DispatcherHandler` to handler the request. The unmodified original url is appended to the list in the `ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR` attribute. +The `ForwardRoutingFilter` looks for a URI in the exchange attribute `ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR`. If the url has a `forward` scheme (ie `forward:///localendpoint`), it will use the Spring `DispatcherHandler` to handler the request. The path part of the request URL will be overridden with the path in the forward URL. The unmodified original url is appended to the list in the `ServerWebExchangeUtils.GATEWAY_ORIGINAL_REQUEST_URL_ATTR` attribute. === LoadBalancerClient 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 9e4cb377..282f4d50 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 @@ -35,6 +35,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.boot.context.properties.PropertyMapper; import org.springframework.cloud.gateway.actuate.GatewayControllerEndpoint; import org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter; +import org.springframework.cloud.gateway.filter.ForwardPathFilter; import org.springframework.cloud.gateway.filter.ForwardRoutingFilter; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter; @@ -330,6 +331,11 @@ public class GatewayAutoConfiguration { return new ForwardRoutingFilter(dispatcherHandler); } + @Bean + public ForwardPathFilter forwardPathFilter() { + return new ForwardPathFilter(); + } + @Bean public WebSocketService webSocketService() { return new HandshakeWebSocketService(); diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ForwardPathFilter.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ForwardPathFilter.java new file mode 100644 index 00000000..ebea7f3a --- /dev/null +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ForwardPathFilter.java @@ -0,0 +1,53 @@ +/* + * Copyright 2013-2018 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; + +import reactor.core.publisher.Mono; + +import java.net.URI; +import org.springframework.cloud.gateway.route.Route; +import org.springframework.core.Ordered; +import org.springframework.web.server.ServerWebExchange; + +import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR; +import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.isAlreadyRouted; + +/** + * Filter to set the path in the request URI if the {@link Route} URI has the scheme + * forward. + * @author Ryan Baxter + */ +public class ForwardPathFilter implements GlobalFilter, Ordered{ + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + Route route = exchange.getAttribute(GATEWAY_ROUTE_ATTR); + URI routeUri = route.getUri(); + String scheme = routeUri.getScheme(); + if (isAlreadyRouted(exchange) || !"forward".equals(scheme)) { + return chain.filter(exchange); + } + exchange = exchange.mutate().request( + exchange.getRequest().mutate().path(routeUri.getPath()).build()) + .build(); + return chain.filter(exchange); + } + + @Override + public int getOrder() { + return 0; + } +} diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/ForwardTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/ForwardTests.java index f3c25b87..c3cd3e2c 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/ForwardTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/ForwardTests.java @@ -64,6 +64,15 @@ public class ForwardTests { .expectBody().json("{\"from\":\"localcontroller\"}"); } + @Test + public void forwardWithCorrectPath() { + this.client.get().uri("/foo") + .header(HttpHeaders.HOST, "www.forward.org") + .exchange() + .expectStatus().isOk() + .expectBody().json("{\"from\":\"localcontroller\"}"); + } + @EnableAutoConfiguration @SpringBootConfiguration @RestController