Skip to content

Commit

Permalink
Make host ignoring port opt in.
Browse files Browse the repository at this point in the history
This makes gh-3037 opt in by setting spring.cloud.gateway.predicate.host.include-port=false

Fixes gh-3190
  • Loading branch information
spencergibb committed Dec 21, 2023
1 parent ed637f5 commit 09ecf0a
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,9 @@ public HeaderRoutePredicateFactory headerRoutePredicateFactory() {

@Bean
@ConditionalOnEnabledPredicate
public HostRoutePredicateFactory hostRoutePredicateFactory() {
return new HostRoutePredicateFactory();
public HostRoutePredicateFactory hostRoutePredicateFactory(Environment env) {
boolean includePort = env.getProperty("spring.cloud.gateway.predicate.host.include-port", Boolean.class, true);
return new HostRoutePredicateFactory(includePort);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,27 @@
*/
public class HostRoutePredicateFactory extends AbstractRoutePredicateFactory<HostRoutePredicateFactory.Config> {

private boolean includePort = true;

private PathMatcher pathMatcher = new AntPathMatcher(".");

public HostRoutePredicateFactory() {
this(true);
}

public HostRoutePredicateFactory(boolean includePort) {
super(Config.class);
this.includePort = includePort;
}

public void setPathMatcher(PathMatcher pathMatcher) {
this.pathMatcher = pathMatcher;
}

/* for testing */ void setIncludePort(boolean includePort) {
this.includePort = includePort;
}

@Override
public List<String> shortcutFieldOrder() {
return Collections.singletonList("patterns");
Expand All @@ -60,25 +71,35 @@ public Predicate<ServerWebExchange> apply(Config config) {
return new GatewayPredicate() {
@Override
public boolean test(ServerWebExchange exchange) {
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;
}
String host;
if (includePort) {
host = exchange.getRequest().getHeaders().getFirst("Host");
}
else {
InetSocketAddress address = exchange.getRequest().getHeaders().getHost();
if (address != null) {
host = address.getHostString();
}
else {
return false;
}
}

if (match != null) {
Map<String, String> variables = pathMatcher.extractUriTemplateVariables(match, host);
ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables);
return true;
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;
}
}

if (match != null) {
Map<String, String> variables = pathMatcher.extractUriTemplateVariables(match, host);
ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables);
return true;
}

return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,12 @@
"type": "java.lang.Boolean",
"description": "If Micrometer Observability support should be turned on.",
"defaultValue": "true"
},
{
"name": "spring.cloud.gateway.predicate.host.include-port",
"type": "java.lang.Boolean",
"description": "Include the port in matching the host name.",
"defaultValue": "true"
}
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
Expand All @@ -41,6 +42,9 @@
@DirtiesContext
public class HostRoutePredicateFactoryTests extends BaseWebClientTests {

@Autowired
private HostRoutePredicateFactory hostPredicate;

@Test
public void hostRouteWorks() {
expectHostRoute("www.example.org", "host_example_to_httpbin");
Expand Down Expand Up @@ -76,7 +80,11 @@ public void mulitHostRouteDslWorks() {

@Test
public void sameHostWithPort() {
expectHostRoute("hostpatternarg.org:8080", "without_pattern");
expectHostRoute("hostpatternarg.org:8080", "host_with_port_pattern");

hostPredicate.setIncludePort(false);
expectHostRoute("hostpatternarg.org:8080", "host_without_port_pattern");
hostPredicate.setIncludePort(true);
}

@Test
Expand Down
10 changes: 9 additions & 1 deletion spring-cloud-gateway-server/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,15 @@ spring:


# =====================================
- id: without_pattern
- id: host_with_port_pattern
uri: ${test.uri}
predicates:
- name: Host
args:
pattern: 'hostpatternarg.org:8080'

# =====================================
- id: host_without_port_pattern
uri: ${test.uri}
predicates:
- name: Host
Expand Down

0 comments on commit 09ecf0a

Please sign in to comment.