我正在使用Spring Boot版本-2.0.6.RELEASE和Spring Cloud版本-Finchley.SR2
并且我创建了自定义网关过滤器来修改请求正文。
但是在使用Flux将请求主体转换为字符串时,我得到了一个空字符串。我需要一种方法来获取与我的请求正文相对应的字符串。
@Override
public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = (ServerHttpRequest) exchange.getRequest();
String s = resolveBodyFromRequest(request);
/* s comes out to be "" */
return chain.filter(newExchange);
}
private String resolveBodyFromRequest(ServerHttpRequest serverHttpRequest){
//Get the request body
Flux<DataBuffer> body = serverHttpRequest.getBody();
StringBuilder sb = new StringBuilder();
body.subscribe(buffer -> {
byte[] bytes = new byte[buffer.readableByteCount()];
buffer.read(bytes);
DataBufferUtils.release(buffer);
String bodyString = new String(bytes, StandardCharsets.UTF_8);
sb.append(bodyString);
});
return sb.toString();
}
答案 0 :(得分:4)
您可以使用ModifyRequestBodyGatewayFilterFactory,我相信它是Finchley一部分的Spring Cloud Gateway 2.0.2中包含的。
例如:
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
ModifyRequestBodyGatewayFilterFactory.Config modifyRequestConfig = new ModifyRequestBodyGatewayFilterFactory.Config()
.setContentType(ContentType.APPLICATION_JSON.getMimeType())
.setRewriteFunction(String.class, String.class, (exchange1, originalRequestBody) -> {
String modifiedRequestBody = yourMethodToModifyRequestBody(originalRequestBody);
return Mono.just(modifiedRequestBody);
});
return new ModifyRequestBodyGatewayFilterFactory().apply(modifyRequestConfig).filter(exchange, chain);
};
}
答案 1 :(得分:1)
这是Spring Cloud Gateway 2.2.5中的另一种方法,我们将使用ReadBodyPredicateFactory,因为这会将具有属性键cachedRequestBodyObject的requestBody缓存到ServerWebExchange
创建始终为真的谓词
@Component
public class TestRequestBody implements Predicate
{
@Override
public boolean test(Object o)
{
return true;
}
}
在application.yml中,添加谓词
spring:
cloud:
gateway:
routes:
....
predicates:
.....
- name: ReadBodyPredicateFactory
args:
inClass: "#{T(String)}"
predicate: "#{@testRequestBody}"
在您自己的过滤器中,如下所示获取requestBody:
@Override
public GatewayFilter apply(Object config)
{
return (exchange, chain) -> {
String requestBody = exchange.getAttribute("cachedRequestBodyObject");
};
}
答案 2 :(得分:0)
一旦您读取(通过读取日志)请求主体,请求本身就会下降。 Spring Cloud网关需要记录请求正文的内容,但请求正文只能读取一次。如果在读取请求主体后未对其进行封装,则后者服务将无法读取主体数据。 follow this
答案 3 :(得分:0)
使用spring cloud网关重写主体请求对@ tony.hokan答案https://stackoverflow.com/a/64080867/1484823进行精炼,以将请求主体(以及可能的响应主体)保存为org.springframework.web.server.ServerWebExchange
的属性
@Bean
public RouteLocator myRouteSavingRequestBody(RouteLocatorBuilder builder) {
return builder.routes()
.route("my-route-id",
p -> p
.path("/v2/**") //your own path filter
.filters(f -> f
.modifyResponseBody(String.class, String.class,
(webExchange, originalBody) -> {
if (originalBody != null) {
webExchange.getAttributes().put("cachedResponseBodyObject", originalBody);
return Mono.just(originalBody);
} else {
return Mono.empty();
}
})
.modifyRequestBody(String.class, String.class,
(webExchange, originalBody) -> {
if (originalBody != null) {
webExchange.getAttributes().put("cachedRequestBodyObject", originalBody);
return Mono.just(originalBody);
} else {
return Mono.empty();
}
})
)
.uri("https://myuri.org")
)
.build();
}
在您自己的过滤器中,如下所示获取requestBody:
@Override
public GatewayFilter apply(Object config)
{
return (exchange, chain) -> {
String requestBody = exchange.getAttribute("cachedRequestBodyObject");
};
}