在以下Spring Cloud Gateway配置中,我试图匹配路径以/favicon.ico
结尾,但是它不能匹配路径:/gitBooks/100/abc/favicon.ico
spring:
cloud:
gateway:
routes:
- id: favicon
uri: forward:///favicon.ico
predicates:
- Path=**/favicon.ico
spring cloud网关打印以下日志:
2020-02-04 22:57:35.703 TRACE 28356 --- [ctor-http-nio-2] o.s.c.g.h.p.RoutePredicateFactory : Pattern "[**/favicon.ico]" does not match against value "/gitBooks/100/abc/favicon.ico"
2020-02-04 22:57:35.703 TRACE 28356 --- [ctor-http-nio-2] o.s.c.g.h.RoutePredicateHandlerMapping : No RouteDefinition found for [Exchange: GET http://localhost:8080/gitBooks/100/abc/favicon.ico]
我不明白,我想知道如何编写路径谓词以与favicon.ico
匹配路径结尾
答案 0 :(得分:0)
我不知道为什么PathMatcher不起作用,最后我写了一个定制的谓词:
spring:
cloud:
gateway:
routes:
- id: favicon
uri: forward:///favicon.ico
predicates:
- Favicon=
@Component
public class FaviconRoutePredicateFactory extends AbstractRoutePredicateFactory<FaviconRoutePredicateFactory.Config> {
private static final Log log = LogFactory.getLog(FaviconRoutePredicateFactory.class);
public FaviconRoutePredicateFactory() {
super(Config.class);
}
@Override
public Predicate<ServerWebExchange> apply(Config config) {
return exchange -> {
String path = exchange.getRequest().getURI().getRawPath();
return !path.equals("/favicon.ico") && path.endsWith("favicon.ico");
};
}
public static class Config {
}
}