如何评估异步 WebFilter 中的 ServerWebExchangeMatcher?
How to evaluate ServerWebExchangeMatcher in async WebFilter?
如何在 WebFilter
这样的异步上下文中计算 ServerWebExchangeMatcher
?
以下失败,因为无法在异步上下文中调用 .block()
。但是我该如何评估匹配器,以根据该条件继续?
class MyFilter implements org.springframework.web.server.WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerWebExchangeMatcher matcher = ServerWebExchangeMatcher.pathMatchers("/some/path/**");
//block() is forbidden in async context
if (matcher.matches(exchange).block().isMatch()) {
//decorate the exchange
return exchange.filter(new ServerWebExchangeDecorator(exchange) {...}, chain);
} else {
//continue normal flow
return exchange.filter(exchance, chain);
}
}
}
我试过如下哪个有效,但不知道是否正确:
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerWebExchangeMatcher matcher = ServerWebExchangeMatcher.pathMatchers("/some/path/**");
return matcher.matches(exchange).flatMap(matchResult -> {
if (!matchResult.isMatch()) return chain.filter(exchange);
return chain.filter(new ServerWebExchangeDecorator(exchange) {..});
}
如何在 WebFilter
这样的异步上下文中计算 ServerWebExchangeMatcher
?
以下失败,因为无法在异步上下文中调用 .block()
。但是我该如何评估匹配器,以根据该条件继续?
class MyFilter implements org.springframework.web.server.WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerWebExchangeMatcher matcher = ServerWebExchangeMatcher.pathMatchers("/some/path/**");
//block() is forbidden in async context
if (matcher.matches(exchange).block().isMatch()) {
//decorate the exchange
return exchange.filter(new ServerWebExchangeDecorator(exchange) {...}, chain);
} else {
//continue normal flow
return exchange.filter(exchance, chain);
}
}
}
我试过如下哪个有效,但不知道是否正确:
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerWebExchangeMatcher matcher = ServerWebExchangeMatcher.pathMatchers("/some/path/**");
return matcher.matches(exchange).flatMap(matchResult -> {
if (!matchResult.isMatch()) return chain.filter(exchange);
return chain.filter(new ServerWebExchangeDecorator(exchange) {..});
}