gpt4 book ai didi

spring-webflux - WebFlux : How to abort request processing in WebFilter with provided response

转载 作者:行者123 更新时间:2023-12-04 00:30:15 24 4
gpt4 key购买 nike

我想在我的 WebFilter 中做类似下面的事情来选择性地绕过后续的过滤器和 RestControllers:

if(shouldRedirect(exchange)){
//do redirect
}else if(!canAccess(exchange)){
//return a 403 response
}else{
chain.filter(exchange);
}

我该怎么做?

谢谢

莱昂

最佳答案

从技术上讲,契约(Contract)规定过滤器必须返回 Mono<Void> ,在交换(请求+响应)处理完毕后完成。

平常的 WebFilter对请求/响应做一些事情,然后将交换转发到链中的下一个过滤器。但是你也可以单方面处理交换并称之为完成。

在此示例中,我们设置状态代码并调用响应完成。

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();
}

}

关于spring-webflux - WebFlux : How to abort request processing in WebFilter with provided response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52367546/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com