Browse Source

Merge pull request #300 from ryanjbaxter/forward-filter-behavior

Use Path From Forward URI
pull/305/head
Ryan Baxter 7 years ago committed by GitHub
parent
commit
b07d805b78
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      docs/src/main/asciidoc/spring-cloud-gateway.adoc
  2. 6
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java
  3. 53
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ForwardPathFilter.java
  4. 9
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/ForwardTests.java

2
docs/src/main/asciidoc/spring-cloud-gateway.adoc

@ -705,7 +705,7 @@ TODO: document ordering @@ -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

6
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 @@ -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 { @@ -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();

53
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ForwardPathFilter.java

@ -0,0 +1,53 @@ @@ -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
* <code>forward</code>.
* @author Ryan Baxter
*/
public class ForwardPathFilter implements GlobalFilter, Ordered{
@Override
public Mono<Void> 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;
}
}

9
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/ForwardTests.java

@ -64,6 +64,15 @@ public class ForwardTests { @@ -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

Loading…
Cancel
Save