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 use of GatewayFilter Factory in Spring Cloud Gateway

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

Share

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

This article will explain in detail about the use of GatewayFilter Factory in Spring Cloud Gateway, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

GatewayFilter Factory is a filter factory provided in Spring Cloud Gateway. Spring Cloud Gateway's route filter allows you to modify incoming HTTP requests or output HTTP responses in some way, acting only on specific routes.

There are many filter factories built into Spring Cloud Gateway, which can be used directly in configuration, and also support custom GatewayFilter Factory to achieve more complex business requirements.

Spring: cloud:gateway: routes:- id: add_request_header_route uri: http://c.biancheng.net filters:- AddRequestHeader=X-Request-Foo, Bar

Next, I'd like to introduce several commonly used filter factory classes.

1. AddRequestHeader filter Factory

Through the name, we can quickly understand that the purpose of this filter factory is to add request headers.

If the request matches successfully according to the rules, the X-Request-Foo:bar request header will be added and passed to the back-end service, and the rear service can obtain the request header information directly. The code is as follows.

@ GetMapping ("/ hello") public String hello (HttpServletRequest request) throws Exception {System.err.println (request.getHeader ("X-Request-Foo")); return "success";} 2. RemoveRequestHeader filter Factory

RemoveRequestHeader is a filter factory for removing request headers, and you can remove Header before the request is forwarded to the back-end service.

Spring: cloud:gateway: routes:-id: removerequestheader_route uri: http://c.biancheng.net- RemoveRequestHeader=X-Request-Foo3. SetStatus filter Factory

The SetStatus filter factory receives a single state, which is used to set the response code for Http requests. It must be a valid Spring Httpstatus (org.springframework.http.HttpStatus). It can be an integer value of404 or an enumerated type NOT_FOUND.

Spring: cloud:gateway: routes:- id: setstatusint_route uri: http://c.biancheng.net filters:- SetStatus=4014. RedirectTo filter Factory

RedirectTo filter factory is used for redirect operations, for example, we need to redirect to Baidu.

Spring: cloud:gateway: routes:- id: prefixpath_route uri: http://c.biancheng.net filters:- RedirectTo=302, http://baidu.com

The above introduces the use of several filter factories, the tutorial will also introduce you to Retry retry, RequestRateLimiter current limit, Hystrix fuse filter factory and other content, others can refer to the official documentation to learn.

Customize the Spring Cloud Gateway filter factory

The custom Spring Cloud Gateway filter factory needs to inherit the AbstractGatewayFilterFactory class and override the logic of the apply method. Naming needs to end with GatewayFilterFactory, such as CheckAuthGatewayFilterFactory, so CheckAuth is the name of the filter factory when used.

The custom filter factory code is shown below.

@ Componentpublic class CheckAuth3GatewayFilterFactoryextends AbstractGatewayFilterFactory {public CheckAuth3GatewayFilterFactory () {super (Config.class);} @ Overridepublic GatewayFilter apply (Config config) {return (exchange, chain)-> {System.err.println ("entered CheckAuth3GatewayFilterFactory" + config.getName ()); ServerHttpRequest request = exchange.getRequest (). Mutate () .build (); return chain.filter (exchange.mutate (). Request (request). Build ());} public static class Config {private String name Public void setName (String name) {this.name = name;} public String getName () {return name;}

The use is as follows:

Filters:-name: CheckAuth3 args: name: Zhang San

If your configuration is in the form of Key or Value, you can inherit the AbstractNameValueGatewayFilterFactory class without defining your own configuration class.

The AbstractNameValueGatewayFilterFactory class inherits AbstractGatewayFilterFactory and defines a NameValueConfig configuration class with two fields, name and value, in NameValueConfig.

We can use it directly. AddRequestHeaderGatewayFilterFactory, AddRequestParameterGatewayFilterFactory and so on are all AbstractNameValueGatewayFilterFactory directly inherited.

Inherit the AbstractNameValueGatewayFilterFactory mode to define the filter factory, as shown below.

@ Componentpublic class CheckAuthGatewayFilterFactory extends AbstractNameValueGatewayFilter-actory {@ Overridepublic GatewayFilter apply (NameValueConfig config) {return (exchange, chain)-> {System.err.println ("entered CheckAuthGatewayFilterFactory" + config.getName () + "\ t" + config.getValue ()); ServerHttpRequest request = exchange.getRequest (). Mutate (). Build (); return chain.filter (exchange.mutate (). Request (request). Build ();};}}

The use is as follows:

Filters:-CheckAuth=zhangsan, men on how the use of GatewayFilter Factory in Spring Cloud Gateway is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

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