diff --git a/docs/src/main/asciidoc/spring-cloud-gateway.adoc b/docs/src/main/asciidoc/spring-cloud-gateway.adoc index 5fd2fe36..6739704b 100644 --- a/docs/src/main/asciidoc/spring-cloud-gateway.adoc +++ b/docs/src/main/asciidoc/spring-cloud-gateway.adoc @@ -34,6 +34,8 @@ image::{imagesurl}/spring_cloud_gateway_diagram.png[Spring Cloud Gateway Diagram Clients make requests to Spring Cloud Gateway. If the Gateway Handler Mapping determines that a request matches a Route, it is sent to the Gateway Web Handler. This handler runs sends the request through a filter chain that is specific to the request. The reason the filters are divided by the dotted line, is that filters may execute logic before the proxy request is sent or after. All "pre" filter logic is executed, then the proxy request is made. After the proxy request is made, the "post" filter logic is executed. +NOTE: URIs defined in routes without a port will get a default port set to 80 and 443 for HTTP and HTTPS URIs respectively. + [[gateway-request-predicates-factories]] == Route Predicate Factories diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/Route.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/Route.java index bacd72fc..88fff54b 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/Route.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/Route.java @@ -29,6 +29,7 @@ import org.springframework.core.Ordered; import org.springframework.util.Assert; import org.springframework.web.server.ServerWebExchange; import org.springframework.cloud.gateway.filter.GatewayFilter; +import org.springframework.web.util.UriComponentsBuilder; /** * @author Spencer Gibb @@ -56,7 +57,7 @@ public class Route implements Ordered { .order(routeDefinition.getOrder()); } - public Route(String id, URI uri, int order, Predicate predicate, List gatewayFilters) { + private Route(String id, URI uri, int order, Predicate predicate, List gatewayFilters) { this.id = id; this.uri = uri; this.order = order; @@ -82,18 +83,25 @@ public class Route implements Ordered { return this; } - public Builder uri(String uri) { - this.uri = URI.create(uri); - return this; - } - public Builder order(int order) { this.order = order; return this; } + public Builder uri(String uri) { + return uri(URI.create(uri)); + } + public Builder uri(URI uri) { this.uri = uri; + if (this.uri.getPort() < 0 && this.uri.getScheme().startsWith("http")) { + // default known http ports + int port = this.uri.getScheme().equals("https") ? 443 : 80; + this.uri = UriComponentsBuilder.fromUri(this.uri) + .port(port) + .build(false) + .toUri(); + } return this; } diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilterTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilterTests.java index e709a371..a0afe876 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilterTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/RouteToRequestUrlFilterTests.java @@ -150,8 +150,11 @@ public class RouteToRequestUrlFilterTests { } private ServerWebExchange testFilter(MockServerHttpRequest request, String url) { - Route value = new Route("1", URI.create(url), 0, - swe -> true, Collections.emptyList()); + Route value = Route.builder().id("1") + .uri(URI.create(url)) + .order(0) + .predicate(swe -> true) + .build(); ServerWebExchange exchange = MockServerWebExchange.from(request); exchange.getAttributes().put(GATEWAY_ROUTE_ATTR, value); diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/route/RouteTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/route/RouteTests.java new file mode 100644 index 00000000..8015e5eb --- /dev/null +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/route/RouteTests.java @@ -0,0 +1,62 @@ +/* + * 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.route; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class RouteTests { + + @Test + public void defeaultHttpPort() { + Route route = Route.builder().id("1") + .predicate(exchange -> true) + .uri("http://acme.com") + .build(); + + assertThat(route.getUri()).hasHost("acme.com") + .hasScheme("http") + .hasPort(80); + } + + @Test + public void defeaultHttpsPort() { + Route route = Route.builder().id("1") + .predicate(exchange -> true) + .uri("https://acme.com") + .build(); + + assertThat(route.getUri()).hasHost("acme.com") + .hasScheme("https") + .hasPort(443); + } + + + @Test + public void fullUri() { + Route route = Route.builder().id("1") + .predicate(exchange -> true) + .uri("http://acme.com:8080") + .build(); + + assertThat(route.getUri()).hasHost("acme.com") + .hasScheme("http") + .hasPort(8080); + } +} diff --git a/spring-cloud-gateway-core/src/test/kotlin/org/springframework/cloud/gateway/route/builder/RouteDslTests.kt b/spring-cloud-gateway-core/src/test/kotlin/org/springframework/cloud/gateway/route/builder/RouteDslTests.kt index a40bc2ae..a18485e3 100644 --- a/spring-cloud-gateway-core/src/test/kotlin/org/springframework/cloud/gateway/route/builder/RouteDslTests.kt +++ b/spring-cloud-gateway-core/src/test/kotlin/org/springframework/cloud/gateway/route/builder/RouteDslTests.kt @@ -92,7 +92,7 @@ class RouteDslTests { StepVerifier.create(routerLocator.routes) .expectNextMatches({ r -> r.id == "test" && - r.uri == URI.create("http://httpbin.org") && + r.uri == URI.create("http://httpbin.org:80") && r.order == 10 && r.id == "test" && r.predicate.test(MockServerWebExchange