好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

spring cloud gateway 全局过滤器的实现

全局过滤器作用于所有的路由,不需要单独配置,我们可以用它来实现很多统一化处理的业务需求,比如权限认证,ip访问限制等等。

接口定义类:org.springframework.cloud.gateway.filter.globalfilter

?

1

2

3

public interface globalfilter {

   mono< void > filter(serverwebexchange exchange, gatewayfilterchain chain);

}

gateway自带的globalfilter实现类有很多,如下图:

有转发,路由,负载等相关的globalfilter,感兴趣的可以自己去看下源码,了解下。

我们自己如何定义globalfilter来实现我们自己的业务逻辑?

给出一个官方文档上的案例:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

@configuration

public class exampleconfiguration {

   private logger log = loggerfactory.getlogger(exampleconfiguration. class );

 

   @bean

   @order (- 1 )

   public globalfilter a() {

     return (exchange, chain) -> {

       log.info( "first pre filter" );

       return chain.filter(exchange).then(mono.fromrunnable(() -> {

         log.info( "third post filter" );

       }));

     };

   }

 

   @bean

   @order ( 0 )

   public globalfilter b() {

     return (exchange, chain) -> {

       log.info( "second pre filter" );

       return chain.filter(exchange).then(mono.fromrunnable(() -> {

         log.info( "second post filter" );

       }));

     };

   }

 

   @bean

   @order ( 1 )

   public globalfilter c() {

     return (exchange, chain) -> {

       log.info( "third pre filter" );

       return chain.filter(exchange).then(mono.fromrunnable(() -> {

         log.info( "first post filter" );

       }));

     };

   }

}

上面定义了3个globalfilter,通过@order来指定执行的顺序,数字越小,优先级越高。下面就是输出的日志,从日志就可以看出执行的顺序:

2018-10-14 12:08:52.406 info 55062 --- [ioeventloop-4-1] c.c.gateway.config.exampleconfiguration : first pre filter
2018-10-14 12:08:52.406 info 55062 --- [ioeventloop-4-1] c.c.gateway.config.exampleconfiguration : second pre filter
2018-10-14 12:08:52.407 info 55062 --- [ioeventloop-4-1] c.c.gateway.config.exampleconfiguration : third pre filter
2018-10-14 12:08:52.437 info 55062 --- [ctor-http-nio-7] c.c.gateway.config.exampleconfiguration : first post filter
2018-10-14 12:08:52.438 info 55062 --- [ctor-http-nio-7] c.c.gateway.config.exampleconfiguration : second post filter
2018-10-14 12:08:52.438 info 55062 --- [ctor-http-nio-7] c.c.gateway.config.exampleconfiguration : third post filter

当globalfilter的逻辑比较多时,我还是推荐大家单独写一个globalfilter来处理,比如我们要实现对ip的访问限制,不在ip白名单中就不让调用的需求。

单独定义只需要实现globalfilter, ordered这两个接口就可以了。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

@component

public class ipcheckfilter implements globalfilter, ordered {

 

   @override

   public int getorder() {

     return 0 ;

   }

 

   @override

   public mono< void > filter(serverwebexchange exchange, gatewayfilterchain chain) {

     httpheaders headers = exchange.getrequest().getheaders();

     // 此处写死了,演示用,实际中需要采取配置的方式

     if (getip(headers).equals( "127.0.0.1" )) {

       serverhttpresponse response = exchange.getresponse();

       responsedata data = new responsedata();

       data.setcode( 401 );

       data.setmessage( "非法请求" );

       byte [] datas = jsonutils.tojson(data).getbytes(standardcharsets.utf_8);

       databuffer buffer = response.bufferfactory().wrap(datas);

       response.setstatuscode(httpstatus.unauthorized);

       response.getheaders().add( "content-type" , "application/json;charset=utf-8" );

       return response.writewith(mono.just(buffer));

     }

     return chain.filter(exchange);

   }

 

   // 这边从请求头中获取用户的实际ip,根据nginx转发的请求头获取

   private string getip(httpheaders headers) {

     return "127.0.0.1" ;

   }

 

}

过滤的使用没什么好讲的,都比较简单,作用却很大,可以处理很多需求,上面讲的ip认证拦截只是冰山一角,更多的功能需要我们自己基于过滤器去实现。

比如我想做a/b测试,那么就得在路由转发层面做文章,前面我们有贴一个图片,图片中有很多默认的全局过滤器,其中有一个loadbalancerclientfilter是负责选择路由服务的负载过滤器,里面会通过loadbalancer去选择转发的服务,然后传递到下面的路由nettyroutingfilter过滤器去执行,那么我们就可以基于这个机制来实现。

filter中往下一个filter中传递数据实用下面的方式:

?

1

exchange.getattributes().put(gateway_request_url_attr, requesturl);

获取方直接获取:

?

1

uri requesturl = exchange.getrequiredattribute(gateway_request_url_attr);

如果我想改变路由的话,就可以这样做:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

@component

public class debugfilter implements globalfilter, ordered {

 

   @override

   public int getorder() {

     return 10101 ;

   }

 

   @override

   public mono< void > filter(serverwebexchange exchange, gatewayfilterchain chain) {

     try {

       exchange.getattributes().put(gateway_request_url_attr, new uri( "http://192.168.31.245:8081/house/hello2" ));

     } catch (urisyntaxexception e) {

       e.printstacktrace();

     }

     return chain.filter(exchange);

   }

 

}

loadbalancerclientfilter的order是10100,我们这边比它大1,这样就能在它执行完之后来替换要路由的地址了。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

原文链接:https://segmentfault测试数据/a/1190000018402335

查看更多关于spring cloud gateway 全局过滤器的实现的详细内容...

  阅读:18次