gateway自定义filter
gateway有很多自带的filter和自带的global filter
这里我们自定义一个filter
官方文档这样的:
模仿写一个:
@Slf4j public class MyLogGatewayFilter implements GlobalFilter, Ordered { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { String username = exchange.getRequest().getQueryParams().getFirst("username"); if(!"shi".equals(username)) { log.info("---username illegal---"); exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST); return exchange.getResponse().setComplete(); } log.info("---username legal----"); return chain.filter(exchange); } @Override public int getOrder() { return 0; } }
@Configuration public class GatewayConfig { @Bean public MyLogGatewayFilter getFilter() { return new MyLogGatewayFilter(); } }
测试: