Spring Cloud Gateway 获取request body(基于源码改造,不走弯路)

在使用Spring Cloud Gateway的过程中,经常需要获取request body,比如用来做日志记录、签名验证、加密解密等等。

网上的资料,解决方案五花八门。所以就整理了经过验证且已经在线上使用的两种方法,都是基于官方源码进行扩展。

本文使用的Spring Cloud Gateway版本为2.1.1.RELEASE。

ModifyRequestBodyGatewayFilterFactory

ModifyRequestBodyGatewayFilterFactory在官方文档中的介绍如下:

This filter can be used to modify the request body before it is sent downstream by the Gateway.

也就是用来修改request body的,既然能修改,自然就能获取到。

java配置

一个简单的配置如下:

@Bean
public RouteLocator routes(RouteLocatorBuilder builder) {
   
  return builder.routes()
    .route("rewrite_request_body", r -> r.path("/post_json")
           .filters(f -> f.modifyRequestBody(String.class, String.class, MediaType.APPLICATION_JSON_VALUE, (exchange, s) -> Mono.just(s)))
           .uri("l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值