虽然官方为Gateway提供了很多filter,但其实并不使用,我们更多的还是使用自己的配置。
在9527网关模块中新建一个filter包,在里面写一个类来实现自定义filter
@Component @Slf4j public class MyLogGateWayFilter implements GlobalFilter, Ordered { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { log.info("============= come in MyLogGateWayFilter: "+ new Date()); String uname = exchange.getRequest().getQueryParams().getFirst("uname"); if (uname == null){ log.info("============ 用户名为null,非法用户 =============="); exchange.getResponse().setStatusCode(HttpStatus.NOT_ACCEPTABLE); return exchange.getResponse().setComplete(); } return chain.filter(exchange); } @Override public int getOrder() { return 0; } }
测试一下:
http://localhost:9527/payment/lb?uname=hhh