fix host predicate use host of InetSocketAddress insted of headers
Fixes gh-3037
This commit is contained in:
@@ -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 class HostRoutePredicateFactory extends AbstractRoutePredicateFactory<Hos
|
||||
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<String, String> variables = pathMatcher.extractUriTemplateVariables(match, host);
|
||||
ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables);
|
||||
return true;
|
||||
}
|
||||
if (match != null) {
|
||||
Map<String, String> variables = pathMatcher.extractUriTemplateVariables(match, host);
|
||||
ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -74,6 +74,11 @@ public class HostRoutePredicateFactoryTests extends BaseWebClientTests {
|
||||
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"));
|
||||
|
||||
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user