SpringCloud Gateway获取并传递请求体body

本文介绍了如何在SpringMVC和SpringCloudGateway中处理请求体,特别是在使用Webflux时,如何自定义全局过滤器读取并存储请求体,以便后续过滤器访问。重点讲解了如何在SpringCloudGateway中避免因多次读取导致Controller无法获取请求体的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在SpringMVC编程中要想在过滤器或者拦截器获取到请求体body,进行一些操作(比如进行验签),需要自定义RequestWrapper extends HttpServletRequestWrapper对Request进行包装,否则你在过滤器里面读取了请求body后,controller就获取不到body了,因为body是输入流,流只能读取一次。(我的例子程序

在SpringCloud Gateway中,有同样的需求。由于Gateway底层用的Webflux响应式编程,所以获取请求体的写法和SpringMVC是不一样的。

提供一个全局过滤器

package com.edu.gateway.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBufferUtils;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;

/**
 * 在Spring Cloud Gateway中,由于请求的 body 只能被读取一次,因此通常的做法是在第一个过滤器中将请求的 body 读取出来,
 * 然后将 body 数据存储在 ServerWebExchange 的 attributes 中,这样其他的过滤器就可以从 attributes 中获取到 body 数据。
 * <p>
 * 首先,我们需要创建一个全局过滤器来读取 body 数据,并将其存储在 ServerWebExchange 的 attributes ,
 * 然后,其他的过滤器就可以从 ServerWebExchange 的 attributes 中获取到 body 数据:
 * String body = exchange.getAttribute("cachedRequestBody");
 *
 * @author 
 * @date 2023年12月07日 13:19
 */
@Slf4j
@Component
public class CacheBodyGlobalFilter implements GlobalFilter, Ordered {


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

        HttpMethod method = exchange.getRequest().getMethod();
        log.info("============CacheBodyGlobalFilter==========method:{}",method.name());
        if (method == HttpMethod.GET || method == HttpMethod.DELETE) {
            return chain.filter(exchange);
        }
        return DataBufferUtils.join(exchange.getRequest().getBody())
                .flatMap(dataBuffer -> {
                    byte[] bytes = new byte[dataBuffer.readableByteCount()];
                    dataBuffer.read(bytes);
                    String bodyString = new String(bytes, StandardCharsets.UTF_8);
                    log.info("[CacheBodyGlobalFilter]获取到请求体---------{}", bodyString);
                    exchange.getAttributes().put("cachedRequestBody", bodyString);
                    DataBufferUtils.release(dataBuffer);
                    return chain.filter(exchange);
                });
    }

    @Override
    public int getOrder() {
        return -9999;
    }
}

后面过滤器使用

  String bodyJson = exchange.getAttribute("cachedRequestBody");
要在Spring Cloud Gateway中添加POST请求参数,可以使用Spring Cloud Gateway的过滤器来修改请求体。下面是一个示例过滤器,它将“name”参数添加到POST请求请求体中: ```java @Component public class AddNameToRequestBodyFilter implements GlobalFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { if (exchange.getRequest().getMethod() == HttpMethod.POST) { return DataBufferUtils.join(exchange.getRequest().getBody()) .flatMap(dataBuffer -> { byte[] bytes = new byte[dataBuffer.readableByteCount()]; dataBuffer.read(bytes); DataBufferUtils.release(dataBuffer); String body = new String(bytes, StandardCharsets.UTF_8); body += "&name=yourName"; //添加name参数 byte[] newBytes = body.getBytes(StandardCharsets.UTF_8); Flux<DataBuffer> newBody = Flux.just(exchange.getResponse().bufferFactory().wrap(newBytes)); ServerHttpRequest request = exchange.getRequest().mutate().body(newBody).build(); return chain.filter(exchange.mutate().request(request).build()); }); } else { return chain.filter(exchange); } } } ``` 在上面的示例中,我们检查请求的HTTP方法是否为POST。如果是,则通过`DataBufferUtils.join`将请求体中的所有数据缓冲区连接起来,将其转换为字符串。然后,我们添加“name”参数,将请求体转换回字节数组,创建一个新的Flux DataBuffer对象来包含新的请求体。最后,我们使用`ServerHttpRequest.mutate`方法创建一个新的请求对象,其中包含新的请求体将其传递给下一个过滤器。如果请求的HTTP方法不是POST,则直接调用`chain.filter(exchange)`传递请求。 需要注意的是,这只是一个简单的示例,你需要根据自己的业务逻辑来修改和完善这个过滤器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值