完美解决spring cloud gateway 获取body内容并修改

3 篇文章 0 订阅
2 篇文章 0 订阅

之前写过一篇文章,如何获取body的内容。
Spring Cloud Gateway获取body内容,不影响GET请求
确实能够获取所有body的内容了,不过今天终端同学调试接口的时候和我说,遇到了400的问题,报错是这样的HTTP method names must be tokens,搜了一下,都是说https引起的。可我的项目还没用https,排除了。
想到是不是因为修改了body内容导致的问题,试着不修改body的内容,直接传给微服务,果然没有报错了。
问题找到,那就好办了,肯定是我新构建的REQUEST对象缺胳膊少腿了,搜索一通之后发现一篇大牛写的文章:
Spring Cloud Gateway(读取、修改 Request Body)
这里要再次表扬一下古哥,同样是中文文章,度娘却搜不到
不过文章中的spring cloud版本是
Spring Cloud: Greenwich.RC2
我本地是最新的Release版本RS3,并不能完全照搬过来,不过算是给了很大的启发(如何获取body以及重构)
下面给出我的代码
网关中对body内容进行解密然后验签

/**
 * @author tengdj
 * @date 2019/8/13 11:08
 * 设备接口验签,解密
 **/
@Slf4j
public class TerminalSignFilter implements GatewayFilter, Ordered {

    private static final String AES_SECURTY = "XXX";
    private static final String MD5_SALT = "XXX";

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        exchange.getAttributes().put("startTime", System.currentTimeMillis());
        if (exchange.getRequest().getMethod().equals(HttpMethod.POST)) {
        	//重新构造request,参考ModifyRequestBodyGatewayFilterFactory
            ServerRequest serverRequest = ServerRequest.create(exchange, HandlerStrategies.withDefaults().messageReaders());
            MediaType mediaType = exchange.getRequest().getHeaders().getContentType();
            //重点
            Mono<String> modifiedBody = serverRequest.bodyToMono(String.class).flatMap(body -> {
            	//因为约定了终端传参的格式,所以只考虑json的情况,如果是表单传参,请自行发挥
                if (MediaType.APPLICATION_JSON.isCompatibleWith(mediaType) || MediaType.APPLICATION_JSON_UTF8.isCompatibleWith(mediaType)) {
                    JSONObject jsonObject = JSONUtil.toJO(body);
                    String paramStr = jsonObject.getString("param");
                    String newBody;
                    try{
                        newBody = verifySignature(paramStr);
                    }catch (Exception e){
                        return processError(e.getMessage());
                    }
                    return Mono.just(newBody);
                }
                return Mono.empty();
            });
            BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, String.class);
            HttpHeaders headers = new HttpHeaders();
            headers.putAll(exchange.getRequest().getHeaders());
            //猜测这个就是之前报400错误的元凶,之前修改了body但是没有重新写content length
            headers.remove("Content-Length");
            //MyCachedBodyOutputMessage 这个类完全就是CachedBodyOutputMessage,只不过CachedBodyOutputMessage不是公共的
            MyCachedBodyOutputMessage outputMessage = new MyCachedBodyOutputMessage(exchange, headers);
            return bodyInserter.insert(outputMessage, new BodyInserterContext()).then(Mono.defer(() -> {
                ServerHttpRequest decorator = this.decorate(exchange, headers, outputMessage);
                return returnMono(chain, exchange.mutate().request(decorator).build());
            }));
        } else {
            //GET 验签 
            MultiValueMap<String, String> map = exchange.getRequest().getQueryParams();
            if (!CollectionUtils.isEmpty(map)) {
                String paramStr = map.getFirst("param");
                try{
                    verifySignature(paramStr);
                }catch (Exception e){
                    return processError(e.getMessage());
                }
            }
            return returnMono(chain, exchange);
        }
    }

    @Override
    public int getOrder() {
        return 1;
    }


    private Mono<Void> returnMono(GatewayFilterChain chain,ServerWebExchange exchange){
        return chain.filter(exchange).then(Mono.fromRunnable(()->{
            Long startTime = exchange.getAttribute("startTime");
            if (startTime != null){
                long executeTime = (System.currentTimeMillis() - startTime);
                log.info("耗时:{}ms" , executeTime);
                log.info("状态码:{}" , Objects.requireNonNull(exchange.getResponse().getStatusCode()).value());
            }
        }));
    }

    private String verifySignature(String paramStr) throws Exception{
        log.info("密文{}", paramStr);
        String dParamStr;
        try{
            dParamStr = AESUtil.decrypt(paramStr, AES_SECURTY);
        }catch (Exception e){
            throw new Exception("解密失败!");
        }
        log.info("解密得到字符串{}", dParamStr);
        String signature = SignUtil.sign(dParamStr, MD5_SALT);
        log.info("重新加密得到签名{}", signature);
        JSONObject jsonObject1 = JSONUtil.toJO(dParamStr);
        if (!jsonObject1.getString("signature").equals(signature)) {
            throw new Exception("签名不匹配!");
        }
        return jsonObject1.toJSONString();
    }


    private Mono processError(String message) {
            /*exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();*/
        log.error(message);
        return Mono.error(new Exception(message));
    }

    ServerHttpRequestDecorator decorate(ServerWebExchange exchange, HttpHeaders headers, MyCachedBodyOutputMessage outputMessage) {
        return new ServerHttpRequestDecorator(exchange.getRequest()) {
            public HttpHeaders getHeaders() {
                long contentLength = headers.getContentLength();
                HttpHeaders httpHeaders = new HttpHeaders();
                httpHeaders.putAll(super.getHeaders());
                if (contentLength > 0L) {
                    httpHeaders.setContentLength(contentLength);
                } else {
                    httpHeaders.set("Transfer-Encoding", "chunked");
                }
                return httpHeaders;
            }
            public Flux<DataBuffer> getBody() {
                return outputMessage.getBody();
            }
        };
    }

}

代码到这里就结束了,希望看到的朋友可以少走点弯路,少踩点坑。

### 回答1: Spring Cloud Gateway 可以通过自定义过滤器来获取请求体(body)。具体步骤如下: 1. 创建一个自定义过滤器类,实现 GatewayFilter 接口。 2. 在过滤器类中重写 filter 方法,在该方法中获取请求体。 3. 在 Spring Cloud Gateway 配置文件中配置该过滤器。 示例代码如下: ```java @Component public class MyFilter implements GatewayFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { // 获取请求体 ServerHttpRequest request = exchange.getRequest(); Flux<DataBuffer> body = request.getBody(); // 处理请求体 // ... // 调用下一个过滤器 return chain.filter(exchange); } } ``` 在 Spring Cloud Gateway 配置文件中配置该过滤器: ```yaml spring: cloud: gateway: routes: - id: my_route uri: http://localhost:8080 predicates: - Path=/my_path/** filters: - MyFilter ``` 其中,MyFilter 是自定义过滤器类的名称。在 filters 配置中指定该过滤器即可。 ### 回答2: Spring Cloud Gateway是一个基于Spring Boot的API网关,它允许开发者以统一的方式管理和路由HTTP请求到多个微服务。在实际开发中,有时需要获取HTTP请求的body,在Spring Cloud Gateway获取HTTP请求的body需要注意以下几点: 1. 所有的Route Predicate都需要配置读取HTTP请求体,否则在路由到下游服务时,请求体会丢失。 2. 如果请求体是将JSON字符串作为参数传递,则需要使用JSON库将字符串转成JSON对象。Spring Cloud Gateway中推荐使用与Spring Framework组件集成的Jackson JSON库。 3. HTTP请求的body只能读取一次,所以需要配置路由过滤器来实现将读取过的请求体保存在请求上下文中,以便后续的路由过滤器和路由处理器获取请求体。 在Spring Cloud Gateway获取HTTP请求的body,可以通过自定义GatewayFilter来实现。下面给出获取HTTP请求体的代码示例: ```java public class BodyGatewayFilterFactory extends AbstractGatewayFilterFactory<BodyGatewayFilterFactory.Config> { public BodyGatewayFilterFactory() { super(Config.class); } @Override public GatewayFilter apply(Config config) { return (exchange, chain) -> { return DataBufferUtils.join(exchange.getRequest().getBody()) .flatMap(dataBuffer -> { byte[] bytes = new byte[dataBuffer.readableByteCount()]; dataBuffer.read(bytes); DataBufferUtils.release(dataBuffer); String requestBody = new String(bytes, Charset.forName("UTF-8")); exchange.getAttributes().put("requestBody", requestBody); return chain.filter(exchange); }); }; } public static class Config { } } ``` 在上面的代码中,使用DataBufferUtils.join()函数将请求体存储在字节数组中,并通过exchange的setAttribute()方法存储到请求上下文中。这样,在后续的路由过滤器和路由处理器中就可以通过读取exchange.getAttributes().get("requestBody")来获取HTTP请求的body,而无需重新读取请求体。 ### 回答3: Spring Cloud Gateway是一个基于Spring Boot的网关。它可以在微服务架构中起到路由、负载均衡、API管理等多种作用。 在Spring Cloud Gateway中,获取请求体有两种方式:获取单个请求体和获取多个请求体。 获取单个请求体: 在Spring Cloud Gateway中,获取单个请求体可以使用Exchange对象的getBody()方法。这个方法会返回一个Mono对象,需要使用subscribe()方法来订阅结果。 例如: ```java public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { Mono<String> requestBody = exchange.getRequest().getBodyToMono(String.class); requestBody.subscribe(content -> { // 对请求体进行操作 }); return chain.filter(exchange); } ``` 上面代码中,我们使用getBodyToMono()获取请求体,然后使用subscribe()方法来订阅请求体的内容。订阅成功后,我们可以对请求体进行操作。 获取多个请求体: 在Spring Cloud Gateway中,获取多个请求体可以使用GlobalFilter。GlobalFilter是一种全局过滤器,可以对所有的请求进行处理。 我们可以创建一个自定义的GlobalFilter,然后在filter()方法中获取请求体。 例如: ```java @Component public class MyGlobalFilter implements GlobalFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { MediaType mediaType = exchange.getRequest().getHeaders().getContentType(); Flux<DataBuffer> body = exchange.getRequest().getBody(); return chain.filter(exchange.mutate().request( exchange.getRequest().mutate().body(Flux.just(body)).build()) .build()); } } ``` 上面代码中,我们创建了一个MyGlobalFilter类,并实现了GlobalFilter接口。在filter()方法中,我们使用getBody()获取请求体。获取请求体后,我们更改了请求体的数据,然后使用build()方法创建了一个新的Exchange对象,并返回chain.filter()。 总结: Spring Cloud Gateway可以通过Exchange对象来获取请求体。可以使用getBody()方法获取单个请求体,也可以使用GlobalFilter获取多个请求体。 注意:在Spring Cloud Gateway中,请求体是一个Flux对象。如果需要将请求体转换成其他类型,请使用getBodyToMono()方法。由于Flux对象可能包含多个元素,因此在订阅操作时需要注意。
评论 44
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值