In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how springcloud integrates gateway to achieve gateway global filter function". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how springcloud integrates gateway to achieve gateway global filter function".
1. Code implementation:
Add a custom global filter
/ * customize a global filter * to implement globalfilter, ordered API * / @ Componentpublic class LoginFilter implements GlobalFilter, Ordered {/ * execute the business logic in the filter * to judge the access-token in the request parameters * if this parameter exists: it indicates that the authentication has been successful * if this parameter does not exist: authentication failed. * ServerWebExchange: equivalent to the context of request and response (RequestContext in zuul) * / @ Override public Mono filter (ServerWebExchange exchange, GatewayFilterChain chain) {System.out.println ("custom global filter"); / / 1. Get the request parameter access-token String token = exchange.getRequest (). GetQueryParams (). GetFirst ("access-token"); / / 2. Determine whether there is an if (token = = null) {/ / 3. If it does not exist: authentication failed System.out.println ("no login"); exchange.getResponse () .setStatusCode (HttpStatus.UNAUTHORIZED); return exchange.getResponse () .setComplete (); / / request end} / / 4. If it exists, continue to execute return chain.filter (exchange); / / continue to execute} / * * to specify the order in which the filter is executed. The smaller the return value, the higher the priority of execution * / @ Override public int getOrder () {return 0;}} 2. Achieve results:
Start nacos, and then start project gateway-service, gateway-client. Open a browser to access
Http://localhost:8001/client/index
Visit http://localhost:8001/client/index?access-token=token
After you add token, you can access it.
Thank you for your reading. the above is the content of "how springcloud integrates gateway to achieve gateway global filter function". After the study of this article, I believe you have a deeper understanding of how springcloud integrates gateway to achieve gateway global filter function, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.