我在spring云网关中实现了自定义预过滤器,允许通过身份验证的请求通过下游流程。我想要的是,如果请求未经身份验证,则返回401 UNAUTHORIZE状态的响应并停止下游处理。我可以实现这个春天的云网关吗。
请帮忙。
我的过滤代码低于
public class ValidUserFilter implements GatewayFilterFactory {
@Override
public GatewayFilter apply(Object config) {
return (exchange, chain) -> {
ServerHttpRequest request = exchange.getRequest();
if (isValidRequest(request)) {
// Allow processing
} else {
// set UNAUTHORIZED 401 response and stop the processing
}
return chain.filter(exchange);
};
}
}
配置如下:
- id: myroute
uri: http://localhost:8080/bar
predicates:
- Path=/foo/**
filters:
- ValidUserFilter
参见SetStatusGatewayFilterFactory
中的代码
// set UNAUTHORIZED 401 response and stop the processing
exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
return exchange.getResponse().setComplete();