diff --git a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactory.java b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactory.java index f9b5111485..9cf82025f8 100644 --- a/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactory.java +++ b/spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactory.java @@ -16,6 +16,7 @@ package org.springframework.cloud.gateway.handler.predicate; +import java.net.InetSocketAddress; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -59,22 +60,25 @@ public Predicate apply(Config config) { return new GatewayPredicate() { @Override public boolean test(ServerWebExchange exchange) { - String host = exchange.getRequest().getHeaders().getFirst("Host"); - String match = null; - for (int i = 0; i < config.getPatterns().size(); i++) { - String pattern = config.getPatterns().get(i); - if (pathMatcher.match(pattern, host)) { - match = pattern; - break; + InetSocketAddress address = exchange.getRequest().getHeaders().getHost(); + if (address != null) { + String match = null; + String host = address.getHostName(); + for (int i = 0; i < config.getPatterns().size(); i++) { + String pattern = config.getPatterns().get(i); + if (pathMatcher.match(pattern, host)) { + match = pattern; + break; + } } - } - if (match != null) { - Map variables = pathMatcher.extractUriTemplateVariables(match, host); - ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables); - return true; - } + if (match != null) { + Map variables = pathMatcher.extractUriTemplateVariables(match, host); + ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables); + return true; + } + } return false; } diff --git a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactoryTests.java b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactoryTests.java index 10fb4f857f..b6a528d190 100644 --- a/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactoryTests.java +++ b/spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/handler/predicate/HostRoutePredicateFactoryTests.java @@ -74,6 +74,11 @@ public void mulitHostRouteDslWorks() { expectHostRoute("www.hostmultidsl2.org", "host_multi_dsl"); } + @Test + public void sameHostWithPort() { + expectHostRoute("hostpatternarg.org:8080", "without_pattern"); + } + @Test public void toStringFormat() { Config config = new Config().setPatterns(Arrays.asList("pattern1", "pattern2")); diff --git a/spring-cloud-gateway-server/src/test/resources/application.yml b/spring-cloud-gateway-server/src/test/resources/application.yml index b031e4139a..5e62bd4b28 100644 --- a/spring-cloud-gateway-server/src/test/resources/application.yml +++ b/spring-cloud-gateway-server/src/test/resources/application.yml @@ -214,6 +214,15 @@ spring: predicates: - Header=Foo, .* + + # ===================================== + - id: without_pattern + uri: ${test.uri} + predicates: + - name: Host + args: + pattern: 'hostpatternarg.org' + # ===================================== - id: host_backwards_compatible_test uri: ${test.uri}