Spring cloud Gateway designated to perform the filter
Configuration needed in the filter configuration file
gateway configuration is divided into two filter
①: global filters
Global Filters do not need to profile configuration, all requests to the service will intercept,
/**
* @author 荡漾
* @title: TokenFilter
* @projectName gateway
* @description: TODO token校验全局过滤器
* @date 2019/6/21 12:41
*/
@Configuration
@Slf4j
public class TokenFilter implements GlobalFilter, Ordered {
@Override
public int getOrder() {
return -10000;
}
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return chain.filter(exchange);
}
}
②: Specifies the filter
Need to inherit AbstractGatewayFilterFactory
Advantages, can be configured in the configuration file, the filter can be specified for a service,
Simply filter corresponding to the name of the configuration file as shown:
It is worth noting that if you filter name suffix is GatewayFilterFactory as TestGatewayFilterFactory just write Test in configuration files if you can not then write the full name on it
@Component
@Slf4j
public class Test extends AbstractGatewayFilterFactory<Test.NameConfig> {
public Test() {
super(NameConfig.class);
log.info("Loaded GatewayFilterFactory [Authorize]");
}
@Override
public GatewayFilter apply(Test.NameConfig config) {
return (exchange, chain) -> {
System.out.println("= = = = = = 执行了= = = = = = = =");
return chain.filter(exchange);
};
}
public static class NameConfig {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
}