请求体修改
使用 GlobalFilter 过滤所有请求,用 ModifyRequestBodyGatewayFilterFactory 生成的 filter 通过 RewriteFunction 修改请求体。
源码
@Component
@Order(10)
public class RequestBodyModifyFilter implements GlobalFilter{
@Resource
private ModifyRequestBodyGatewayFilterFactory modifyRequestBodyGatewayFilterFactory;
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
return reqBodyRewriter().filter(exchange, chain);
}
/**
* 修改请求体的filter
*/
@Bean
private GatewayFilter reqBodyRewriter(){
return modifyRequestBodyGatewayFilterFactory.apply(new ModifyRequestBodyGatewayFilterFactory.Config().setRewriteFunction(
String.class, String.class, rewriteFunction()
));
}
/**
* 此处是对原请求体的修改方法
*/
private RewriteFunction<String, String> rewriteFunction(){
return (serverWebExchange, s) -> {
//对请求体进行操作、s为原请求体
return Mono.just("修改后的结果");
};
}
}
大约 1 分钟