Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the global filter GlobalFilter that comes with SpringCloud Gateway

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

What is the global filter GlobalFilter of Gateway? I believe many inexperienced people are at a loss about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Global filter works on all routes and does not need to be configured separately. We can use it to achieve many unified business requirements, such as permission authentication, IP access restrictions and so on.

The API defines the class org.springframework.cloud.gateway.filter.GlobalFilter, as shown below.

Public interface GlobalFilter {Mono filter (ServerWebExchange exchange, GatewayFilterChain chain);}

There are many GlobalFilter implementation classes that come with SpringCloud Gateway, as shown in figure 1.

There are forwarding, routing, load and other related GlobalFilter, interested friends can take a look at the source code to understand. How do we implement our business logic by defining GlobalFilter?

Here is a case in the official documentation, the code is as follows.

@ Configurationpublic 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");}));};}}

Three GlobalFilter are defined above, and the order of execution is specified by @ Order. The smaller the number, the higher the priority. Here is the log of the output, from which you can see the order of execution, as shown below.

2019-8-26 16 INFO 08INFO 55062-[ioEventLoop-4-1] c.c.gateway.config.ExampleConfiguration: first pre filter2019-8-26 16V 08V 52.406 INFO 55062-[ioEventLoop-4-1] c.c.gateway.config.ExampleConfiguration: second pre filter2019-8-26 168RV 08RV 52.407 INFO 55062-- [ioEventLoop-4-1] c.c.gateway.config.ExampleConfiguration: third pre filter2019-8-26 16 : 08first post filter2019 52.437 INFO 55062-[ctor-http-nio-7] c.c.gateway.config.ExampleConfiguration: first post filter2019-8-26 16V 08Vera 52.438 INFO 55062-[ctor-http-nio-7] c.c.gateway.config.ExampleConfiguration: second post filter2019-8-2616Vera 08Vera 52.438 INFO 55062-[ctor-http-nio-7] c.c.gateway.config.ExampleConfiguration: third post filter

When there is a lot of logic in GlobalFilter, I still recommend that you write a separate GlobalFilter to deal with it. For example, we need to implement the access restriction on IP, that is, the requirement that cannot be called if it is not in the IP whitelist.

To define it separately, you only need to implement GlobalFilter and Ordered interfaces, as shown in the following code.

@ Componentpublic class IPCheckFilter implements GlobalFilter, Ordered {@ Overridepublic int getOrder () {return 0;} @ Overridepublic Mono filter (ServerWebExchange exchange, GatewayFilterChain chain) {HttpHeaders headers = exchange.getRequest (). GetHeaders (); / / it is very absolute here, only for demonstration purposes. In practice, it needs to be configured if (getIp (headers) .equals ("127.0.0.1")) {ServerHttpResponse response = exchange.getResponse () ResponseData data = new ResponseData (); data.setCode (401); data.setMessage ("illegal request"); 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);} / / get the actual IP of the user from the request header, and get the private String getIp (HttpHeaders headers) {return "127.0.0.1";}} according to the request header forwarded by Nginx.

Although the use of filtering is relatively simple, it is very useful and can handle a lot of requirements. The IP authentication interception mentioned above is only the tip of the iceberg, and more functions need to be realized based on the filter.

After reading the above, have you mastered the method of Gateway's global filter GlobalFilter? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report