在学习spring Cloud时,gateway作为服务统一入口,经常需要对来历不明的请求做一些筛选。

官方提供了一些常用的过滤器

filters:
            - AddRequestHeader=name,zs          #请求头添加name:zs
            - AddRequestParameter=color,blue    #请求参数添加color:blue
            - AddResponseHeader=phone,973345344 #响应头添加phone:973345344
            - PrefixPath=/mypath			    #添加路径前缀/mypath
            - StripPrefix=n					    #删除路径前缀n个

但我们也可以diy专属自己的过滤器

操作方法

1.在网关中定义自己的DemoGatewayFilterFactory,并且继承AbstractGatewayFilterFactory<DemoGatewayFilterFactory.Config>

spring clound Gateway header添加数据 spring cloud gateway 自定义filter_List

 要注意,DemoGatewayFilterFactory的命名方式:由Demo + GatewayFilterFactory组成,

其中Demo是自己起名字,GatewayFilterFactory是固定的。

范型中的Config是我们待会要用到的静态内部类,用于声明过滤器中传递的参数。

2.接着我们直接定义一个静态内部类,名字就叫Config

spring clound Gateway header添加数据 spring cloud gateway 自定义filter_List_02

 Config中声明了一些属性。例如: - StripPrefix=1  //删除路径前缀1个。这个官方的过滤器中需要我们传递一个参数n,代表删除路径前n个. 我们传递的1就是由Config中的属性来接收的

3.重写shortcutFieldOrder方法。

spring clound Gateway header添加数据 spring cloud gateway 自定义filter_gateway_03

 该方法返回值是一个List集合,集合中arg1,arg2,arg3...... 这些参数的顺序将影响Config中变量的赋值结果。

4.给定一个构造方法。

spring clound Gateway header添加数据 spring cloud gateway 自定义filter_内部类_04

5.最后一步:重写最重要的apply方法,此方法将会执行网关过滤器的流程。

spring clound Gateway header添加数据 spring cloud gateway 自定义filter_内部类_05

这里我的流程只做日志打印。

在yml中配置自己的过滤器

server:
  port: 8888
spring:
  application:
    name: GATEWAY
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848 #Nacos
    gateway:
      routes: #路由
        #admin
        - id: API-ADMIN #起个名字
          uri: lb://ADMINS #负载均衡 去注册中心拉取服务
          predicates: #断言
            - Path=/admin
          filters:
            - Demo=a,b,c,d,e

  1.由于我们的过滤器类名叫:DemoGatewayFilterFactory,由Demo+ GatewayFilterFactory组成,所以在使用自定义过滤器时,只需要配置 - Demo=a,b,c,d,e即可。


2.其中a,b,c,d,e是我们传入的参数,将由Config静态内部类中的arg1,arg2,arg3,arg4,arg5接收。

3.具体参数传递的顺序由shortcutFieldOrder方法中返回值List集合的顺序一一对应。

演示部分:

启动网关服务,并访问自定义过滤器。 

spring clound Gateway header添加数据 spring cloud gateway 自定义filter_gateway_06

代码

@Slf4j
@Component
public class DemoGatewayFilterFactory extends AbstractGatewayFilterFactory<DemoGatewayFilterFactory.Config>
{


    //构造方法
    public DemoGatewayFilterFactory()
    {
        super(Config.class);
    }

    //返回一个List   ->  负责指定接收网关参数的顺序
    @Override
    public List<String> shortcutFieldOrder()
    {
        return Arrays.asList("arg1", "arg2", "arg3", "arg4", "arg5");
    }

    //主要方  => 负责 执行网关流程
    @Override
    public GatewayFilter apply(Config config)
    {
        return new GatewayFilter()
        {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
            {

                log.info("进入Demo网关");
                log.info("打印网关参数:" + config.arg1);
                log.info("打印网关参数:" + config.arg2);
                log.info("打印网关参数:" + config.arg3);
                log.info("打印网关参数:" + config.arg4);
                log.info("打印网关参数:" + config.arg5);
                log.info("退出Demo网关");
                return chain.filter(exchange);
            }
        };
    }

    //Config静态内部类 ->  负责指定网关的参数
    @Data
    static class Config
    {
        private String arg1;
        private String arg2;
        private String arg3;
        private String arg4;
        private String arg5;
    }


}