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 are the common route assertion factories of SpringCloud-Gateway

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

Share

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

This article mainly introduces "what is the common route assertion factory of SpringCloud-Gateway". In the daily operation, I believe that many people have doubts about the common route assertion factory of SpringCloud-Gateway. The editor consulted all kinds of information and sorted out the simple and easy-to-use operation method. I hope it will be helpful to answer the doubt of "what is the common route assertion factory of SpringCloud-Gateway?" Next, please follow the editor to study!

1) Path routing assertion factory

The Path routing assertion factory receives a parameter that determines whether the accessed URI matches according to the rules defined by the Path.

Spring: cloud: gateway: routes:-id: host_route uri: http://minglisoft.cn predicates:-Path=/blog/detail/ {segment}

If the request path is / blog/detail/xxx, this route will match. You can also use regularities, such as / blog/detail/**, to match the multilevel URI at the beginning of / blog/detail/.

When we visit the local gateway: http://localhost 2001 more blogmax detailprime 36185, we can see that the content corresponding to blog is displayed.

2) Query routing assertion factory

The Query routing assertion factory receives two parameters, a required parameter and an optional regular expression.

Spring: cloud: gateway: routes:-id: query_route uri: http://minglisoft.cn predicates:-Query=foo, ba.

If the request contains a foo query parameter whose value matches ba, this route will match. Bar and baz also match because the second parameter is a regular expression.

Test link: http://localhost VOLAGUR 2001 Universe foodies Baz.

3) Method routing assertion factory

The Method route assertion factory receives one parameter, the HTTP method to match.

Spring: cloud: gateway: routes:-id: method_route uri: http://baidu.com predicates:-Method=GET4) Header routing assertion factory

The Header routing assertion factory receives two parameters, the request header name and the regular expression.

Spring: cloud: gateway: routes:-id: header_route uri: http://example.org predicates:-Header=X-Request-Id,\ d+

If the request has a request header named x-request-id whose value matches the\ d + regular expression (the value is one or more digits), this route matches.

If you want to learn more about the use of the routing assertion factory, you can refer to the official documentation.

Custom routing assertion factory

The custom route assertion factory needs to inherit the AbstractRoutePredicateFactory class and override the logic of the apply method.

In the apply method, you can get the ServerHttpRequest object through exchange.getRequest (), so you can get the request parameters, request method, request header and other information.

The parameters of the apply method are custom configuration classes, which are configured when used, and are directly obtained and used in the apply method.

Naming needs to end with RoutePredicateFactory, such as CheckAuthRoutePredicateFactory, so CheckAuth is the name of the routing assertion factory when used. The code is as follows.

@ Componentpublic class CheckAuthRoutePredicateFactory extends AbstractRoutePredicateFactory {public CheckAuthRoutePredicateFactory () {super (Config.class);} @ Override public Predicate apply (Config config) {return exchange-> {System.err.println ("entered CheckAuthRoutePredicateFactory\ t" + config.getName ()); if (config.getName (). Equals ("zhangsan")) {return true } return false;};} public static class Config {private String name; public void setName (String name) {this.name = name;} public String getName () {return name;}}

Examples of usage are as follows:

Spring: cloud: gateway: routes:-id: customer_route uri: http://minglisoft.cn predicates:-name: CheckAuth args: name: zhangsan this is the end of the study on "what is the common route of SpringCloud-Gateway to assert what the factory has", hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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