1. 程式人生 > >SpringCloud Gateway獲取post請求體(request body)

SpringCloud Gateway獲取post請求體(request body)

獲取spring cloud gateway POST請求體的時候,會有很多坑,網上大多數解決方案是

/**
這種方法在spring-boot-starter-parent 2.0.6.RELEASE + Spring Cloud Finchley.SR2 body 中生效, 
但是在spring-boot-starter-parent 2.1.0.RELEASE + Spring Cloud Greenwich.M3 body 中不生效,總是為空
*/
private String resolveBodyFromRequest(ServerHttpRequest serverHttpRequest) {
        Flux<DataBuffer> body = serverHttpRequest.getBody();
        AtomicReference<String> bodyRef = new AtomicReference<>();
        body.subscribe(buffer -> {
            CharBuffer charBuffer = StandardCharsets.UTF_8.decode(buffer.asByteBuffer());
            DataBufferUtils.release(buffer);
            bodyRef.set(charBuffer.toString());
        });
        return bodyRef.get();
    }

但是實際這種解決方案(例如 這篇文章)會帶來很多問題,比如request不能在其他filter中獲取,會報錯:

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

針對這種不能重複獲取的問題,網上通用解決是把request重新包裝,繼續傳遞,比如

這篇文章的解決方案。
但是這種方案還會帶來request body獲取不完整,只能獲取1024B的資料,這個問題暫時沒有很好的解法,很頭痛,在給官方提issues的時候,issues709issues707 的時候,對方讓我參看一個類ModifyRequestBodyGatewayFilterFactory.java,說真的並沒有看懂,最後翻原始碼的時候,發現了一個預言類,ReadBodyPredicateFactory ,發現裡面快取了request body的資訊,於是在自定義router中配置了ReadBodyPredicateFactory,然後在filter中通過cachedRequestBodyObject快取欄位獲取request body資訊,這種解決,一不會帶來重複讀取問題,二不會帶來requestbody取不全問題。三在低版本的Spring Cloud Finchley.SR2也可以執行。

step 1:現在自動以router裡面配置ReadBodyPredicate預言類:
RouteLocatorBuilder.Builder serviceProvider = builder.
                routes().route("gateway-sample",
                    r -> r.readBody(Object.class, requestBody -> {
                        log.info("requestBody is {}", requestBody);
                        // 這裡不對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:在自定義filter中獲取快取了的request body:
      Object requestBody = exchange.getAttribute("cachedRequestBodyObject");

至此問題解決,完整程式碼在我的github上面。參考這裡

參考:
https://www.cnblogs.com/cafebabe-yun/p/9328554.html
https://blog.csdn.net/tianyaleixiaowu/article/details/83375246