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

SpringCloud Gateway gets the post request body (request body)

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

When you get the spring cloud gateway POST request body, there will be a lot of holes, and most of the solutions on the Internet are

/ * * this method works in spring-boot-starter-parent 2.0.6.RELEASE + Spring Cloud Finchley.SR2 body, but not in spring-boot-starter-parent 2.1.0.RELEASE + Spring Cloud Greenwich.M3 body. It is always empty * / private String resolveBodyFromRequest (ServerHttpRequest serverHttpRequest) {Flux body = serverHttpRequest.getBody (); AtomicReference bodyRef = new AtomicReference () Body.subscribe (buffer-> {CharBuffer charBuffer = StandardCharsets.UTF_8.decode (buffer.asByteBuffer ()); DataBufferUtils.release (buffer); bodyRef.set (charBuffer.toString ());}); return bodyRef.get ();}

However, the actual solution (such as this article) will cause a lot of problems. For example, if request is not available in other filter, it will report an error:

Reactor.core.Exceptions$ErrorCallbackNotImplemented: java.lang.IllegalStateException: Only one connection receive subscriber allowed.Caused by: java.lang.IllegalStateException: Only one connection receive subscriber allowed.

To solve this problem that can not be obtained repeatedly, the general solution on the Internet is to repackage the request and continue to deliver it, such as the solution of this article.

However, this scheme will also lead to incomplete access to request body, only 1024B of data. There is no good solution to this problem for the time being, and it is a headache. When I mentioned issues, issues709 and issues707, they asked me to refer to a class ModifyRequestBodyGatewayFilterFactory.java, which I really didn't understand. Finally, when I went through the source code, I found a prophecy class, ReadBodyPredicateFactory, and found that the information of request body was cached in it, so I configured ReadBodyPredicateFactory in the custom router. Then obtain the requestbody information through the cachedRequestBodyObject cache field in filter, this solution will not bring the problem of repeated reading and the problem of incomplete requestbody fetching. Third, it can also be run in lower versions of Spring Cloud Finchley.SR2.

Step 1: now automatically configure ReadBodyPredicate prophecy class in router: RouteLocatorBuilder.Builder serviceProvider = builder. Routes (). Route ("gateway-sample", r-> r.readBody (Object.class, requestBody-> {log.info ("requestBody is {}", requestBody); / / there is no judgment processing for body return true;}). And (). Path ("/ service"). Filters (f-> {f.filter (requestFilter); return f;}) .uri ("http://127.0.0.1:8009")); RouteLocator routeLocator = serviceProvider.build ()) Step2: get the cached request body: Object requestBody = exchange.getAttribute ("cachedRequestBodyObject") in the custom filter

Now that the problem is solved, the complete code is on my github. Refer to here.

Reference:

Https://www.cnblogs.com/cafebabe-yun/p/9328554.html

Https://blog.csdn.net/tianyaleixiaowu/article/details/83375246

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

Servers

Wechat

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

12
Report