Spring Cloud Gateway 修改响应数据

背景介绍

有个金融类项目,客户对系统安全性比较看重,要求接口请求和响应的数据,都要按特定要求进行加密,防止敏感业务数据被抓包截取。

现在设计流程已经拟定,客户端也解决了如何解密响应数据。服务端还没实现对响应数据进行加密。

抽象出来,本质上要解决的问题是,如何修改响应数据。

问题描述

项目已经使用了Spring Cloud Gateway技术,响应数据可以在网关拦截。

现在的问题是,如何修改响应数据。

关键词:spring cloud gateway modify response body

解决方案

spring cloud gateway 已经提供了修改响应体的示例ModifyResponseBodyGatewayFilterFactory

image.png

示例代码内容如下:

ModifyResponseBodyGatewayFilterFactory

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.springframework.cloud.gateway.filter.factory.rewrite;

import java.util.Map;
import org.reactivestreams.Publisher;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.support.BodyInserterContext;
import org.springframework.cloud.gateway.support.CachedBodyOutputMessage;
import org.springframework.cloud.gateway.support.DefaultClientResponse;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseCookie;
import org.springframework.http.client.reactive.ClientHttpResponse;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.server.reactive.ServerHttpResponseDecorator;
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.client.ExchangeStrategies;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public class ModifyResponseBodyGatewayFilterFactory extends AbstractGatewayFilterFactory<ModifyResponseBodyGatewayFilterFactory.Config> {
    private final ServerCodecConfigurer codecConfigurer;

    public ModifyResponseBodyGatewayFilterFactory(ServerCodecConfigurer codecConfigurer) {
        super(ModifyResponseBodyGatewayFilterFactory.Config.class);
        this.codecConfigurer = codecConfigurer;
    }

    public GatewayFilter apply(ModifyResponseBodyGatewayFilterFactory.Config config) {
        return new ModifyResponseBodyGatewayFilterFactory.ModifyResponseGatewayFilter(config);
    }

    public static class Config {
        private Class inClass;
        private Class outClass;
        private Map<String, Object> inHints;
        private Map<String, Object> outHints;
        private String newContentType;
        private RewriteFunction rewriteFunction;

        public Config() {
        }

        public Class getInClass() {
            return this.inClass;
        }

        public ModifyResponseBodyGatewayFilterFactory.Config setInClass(Class inClass) {
            this.inClass = inClass;
            return this;
        }

        public Class getOutClass() {
            return this.outClass;
        }

        public ModifyResponseBodyGatewayFilterFactory.Config setOutClass(Class outClass) {
            this.outClass = outClass;
            return this;
        }

        public Map<String, Object> getInHints() {
            return this.inHints;
        }

        public ModifyResponseBodyGatewayFilterFactory.Config setInHints(Map<String, Object> inHints) {
            this.inHints 
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Gateway提供了一种全局响应处理的机制,可以在所有路由处理完成后进行统一的响应处理。具体实现方式是通过实现`GlobalFilter`接口来实现全局过滤器,并在其中处理响应。以下是一个简单的示例代码: ```java @Component public class CustomGlobalFilter implements GlobalFilter { @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { return chain.filter(exchange).then(Mono.fromRunnable(() -> { ServerHttpResponse response = exchange.getResponse(); HttpStatus statusCode = response.getStatusCode(); if (statusCode == HttpStatus.UNAUTHORIZED) { response.getHeaders().setContentType(MediaType.APPLICATION_JSON); String body = "{\"message\": \"Unauthorized\"}"; DataBuffer buffer = response.bufferFactory().wrap(body.getBytes(StandardCharsets.UTF_8)); return response.writeWith(Flux.just(buffer)); } })); } } ``` 在上述代码中,我们通过实现`GlobalFilter`接口并重写`filter`方法,在方法中调用`chain.filter(exchange)`代表继续执行后续的过滤器和路由处理。在其后面通过`then`方法调用`Mono.fromRunnable`,并在其中处理响应。例如,如果响应状态码为`UNAUTHORIZED`,我们可以设置响应头和响应体,然后将其写入响应流中。 需要注意的是,在全局过滤器中处理响应时,需要调用`ServerHttpResponse.writeWith`或`ServerHttpResponse.writeAndFlushWith`方法来写入响应流,而不能使用`ServerHttpResponse.getBody`方法来直接写入响应体。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值