我正在为项目使用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());
};
}
答案 0 :(得分:1)
我尝试使用 spring cloud 2020.0.0 和 spring boot 2.4.1,我能够在我的自定义过滤器中获取请求负载