【问题标题】:cachedRequestBodyObject is not working in spring cloud gateway custom filter factorycachedRequestBodyObject 在 Spring Cloud Gateway 自定义过滤器工厂中不起作用
【发布时间】:2021-02-13 02:49:17
【问题描述】:

我正在为我的项目使用 Spring Cloud Gateway。我编写了一个自定义过滤器工厂,用于生成需要请求有效负载的 HMAC 签名。我在路由定义中启用了 readBody(String.class, requestBody -> true) 为 true,并且从 cachedRequestBody 属性中,我能够在我的自定义过滤器中获取请求有效负载使用以下 spring boot 版本 2.1.3.RELEASE 和 spring cloud 版本 2.1.1.RELEASE

现在将 Spring Boot 版本升级到 2.3.3.RELEASE 和 Spring Cloud 版本 2.2.4.RELEASE 相同的代码无法正常工作。

readbody 谓词是否不适用于最新的 Spring Cloud 版本?任何建议都会有所帮助。

自定义过滤器工厂示例:

@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 gateway spring-cloud-gateway


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 2020-04-13
      • 2020-11-04
      • 2018-06-21
      • 2021-02-23
      • 2020-11-14
      • 2018-11-01
      • 1970-01-01
      • 2023-03-04
      相关资源
      最近更新 更多