简体   繁体   English

cachedRequestBodyObject 在 Spring Cloud Gateway 自定义过滤器工厂中不起作用

[英]cachedRequestBodyObject is not working in spring cloud gateway custom filter factory

I am using spring cloud gateway for my project.我正在为我的项目使用 spring 云网关。 I have written one custom filter factory for generating HMAC Signature which requires request payload.我编写了一个自定义过滤器工厂,用于生成需要请求负载的 HMAC 签名。 I have enabled readBody(String.class, requestBody -> true) to true in route definition and from cachedRequestBody attribute, I was able to get the request payload in my custom filter with following spring boot version 2.1.3.RELEASE and spring cloud version 2.1.1.RELEASE .我已在路由定义和cachedRequestBody属性中将 readBody(String.class, requestBody -> true)启用为 true,我能够使用以下 Spring Boot版本 2.1.3.RELEASE 和 Spring Cloud 版本在我的自定义过滤器中获取请求有效负载2.1.1 .发布

Now after upgrading the spring boot version to 2.3.3.RELEASE and Spring cloud version 2.2.4.RELEASE same code is not working.现在将 spring boot 版本升级到 2.3.3.RELEASE 和 Spring cloud 版本 2.2.4.RELEASE 后,相同的代码不起作用。

does the readbody predicate not work with latest spring cloud version? readbody 谓词是否不适用于最新的 Spring Cloud 版本? any suggestion will be helpful.任何建议都会有所帮助。

Sample of custom filter factory:定制过滤器工厂示例:

@Override
public GatewayFilter apply(Config config) {
  
    return (exchange, chain) -> {
        HttpMethod method = exchange.getRequest().getMethod();
        String httpVerb = method.name();
        ServerHttpRequest httpRequest = exchange.getRequest();
        String requestPayload = exchange.getAttribute("cachedRequestBodyObject");
       signature = generateSignature(....);
       ServerHttpRequest.Builder builder = httpRequest.mutate().header(HttpHeaders.AUTHORIZATION, signature)
       ServerHttpRequest downStreamRequest= (config.getPath()!=null?builder.path(config.path):builder).build();
        return chain.filter(exchange.mutate().request(downStreamRequest).build());
    };
}

我尝试使用 spring cloud 2020.0.0 和 spring boot 2.4.1 ,我能够在我的自定义过滤器中获取请求负载

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM