} else {
Iterator var3 = cookies.iterator();
HttpCookie cookie;
do {
if (!var3.hasNext()) {
return false;
}
cookie = (HttpCookie)var3.next();
} while(!cookie.getValue().matches(config.regexp));
return true;
}
};
}
2.5 HeaderRoutePredicateFactory
-
Route 匹配 :请求头满足匹配
-
配置:
spring:
cloud:
gateway:
routes:
- id: header_route
uri: http://example.org
predicates:
- Header=X-Request-Id, \d+
- 代码:
public Predicate apply(HeaderRoutePredicateFactory.Config config) {
boolean hasRegex = !StringUtils.isEmpty(config.regexp);
return (exchange) -> {
List values = (List)exchange.getRequest().getHeaders().getOrDefault(config.header, Collections.emptyList());
if (values.isEmpty()) {
return false;
} else {
return hasRegex ? values.stream().anyMatch((value) -> {
return value.matches(config.regexp);
}) : true;
}
};
}
2.6 HostRoutePredicateFactory
-
Route 匹配 :请求 Host 匹配指定值
-
配置:
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://example.org
predicates:
- Host=**.somehost.org
- 代码:
public Predicate apply(HostRoutePredicateFactory.Config config) {
return (exchange) -> {
String host = exchange.getRequest().getHeaders().getFirst(“Host”);
Optional optionalPattern = config.getPatterns().stream().filter((pattern) -> {
return this.pathMatcher.match(pattern, host);
}).findFirst();
if (optionalPattern.isPresent()) {
Map<String, String> variables = this.pathMatcher.extractUriTemplateVariables((String)optionalPattern.get(), host);
ServerWebExchangeUtils.putUriTemplateVariables(exchange, variables);
return true;
} else {
return false;
}
};
}
2.7 MethodRoutePredicateFactory
-
Route 匹配 :请求 Method 匹配指定值
-
配置:
spring:
cloud:
gateway:
routes:
- id: method_route
uri: http://example.o