I'd like to do something like below in my WebFilter to optionally bypass subsequent filters and RestControllers:
if(shouldRedirect(exchange)){
//do redirect
}else if(!canAccess(exchange)){
//return a 403 response
}else{
chain.filter(exchange);
}
How am I supposed to do that?
Thanks
Leon
Technically, the contract says that the filter has to return Mono<Void>
, which completes when the exchange (request+response) has been processed.
The usual WebFilter
does something on the request/response and then forwards the exchange to the next filter in the chain. But you can also unilaterally process the exchange and call it done.
In this sample, we're setting the status code and calling the response done.
class MyFilter implements WebFilter {
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
ServerHttpResponse response = exchange.getResponse();
response.setStatusCode(HttpStatus.NOT_FOUND);
return response.setComplete();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With