You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
SOP/doc/docs/files/10088_自定义过滤器.md

944 B

自定义过滤器

演示在网关追加一个header

public class CustomFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        // 演示在网关追加header
        ApiParam apiParam = ServerWebExchangeUtil.getApiParam(exchange);
        String token = apiParam.fetchAccessToken();
        ServerWebExchange serverWebExchange = ServerWebExchangeUtil.addHeaders(exchange, httpHeaders -> {
            httpHeaders.add("token", token);
        });
        return chain.filter(serverWebExchange);
    }

    @Override
    public int getOrder() {
        // 自定义过滤器可以从0开始
        return 0;
    }
}

使用过滤器,在sop-gateway中找到MyConfig,添加:

@Configuration
public class MyConfig {

    ...
    @Bean
    CustomFilter customFilter() {
        return new CustomFilter();
    }
    ...
}