Default port for http{s} routes if not set.
fixes gh-198
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -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<ServerWebExchange> predicate, List<GatewayFilter> gatewayFilters) {
|
||||
private Route(String id, URI uri, int order, Predicate<ServerWebExchange> predicate, List<GatewayFilter> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user