利用SpringAOP的返回通知处理数据加密返回

项目安全要求把所有返回值做加密处理,利用SpringAOP的返回切面可以简单方便的做到该需求。

java 复制代码
@Aspect
public class ResponseDataEncryptAspect {

    private ObjectMapper objectMapper;

    public ResponseDataEncryptAspect () {
        this.objectMapper = new ObjectMapper();
        // 保持空值也被序列化
        objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS);
    }
	
	// 拦截Controller内带@RequestMapper的方法及衍生注解即可
    @AfterReturning(value = PointCut.CONTROLLER_POINT_CUT, returning = "res")
    public void jsonDateEncrypt(JoinPoint joinPoint, Object res){
        // 只处理JsonResult返回值
        if (res instanceof JsonResult) {
            JsonResult jsonResult = (JsonResult) res;
            Object data = jsonResult.getData();
            if (null != data) {
                // 把data转成json串,再把json串加密,再替换原来的data,vue拦截到data作全局解密,不影响已绑定的功能。
                String json = objectMapper.writeValueAsString(data);
                String cryptResult = ZYCryptUtils.encryptAES(json);
                // 在全局响应结果里添加一个布尔值,方便前端判断到底是密文还是明文
                jsonResult.setEncrypt(true);
                jsonResult.setData(cryptResult);
            }
        }
    }
}

实际效果:

相关推荐
城管不管几秒前
重生——第五次面试2026.8.1一面
java·数据库·后端·ai·面试·职场和发展·agent
DarLing丶张皇2 分钟前
【源码】JeecgBoot导出Excel模板
java·spring boot
223糖12 分钟前
Idea,pycharm2026激活保姆级教程
java·ide·intellij-idea
geovindu28 分钟前
java: Gale-Shapley Algorithm
java·开发语言·后端·算法
LUCKY-LIVING41 分钟前
ELF File in linux
android·java·linux
星核0penstarry1 小时前
DeepSeek-V4-Flash 正式公测:大模型行业进入「极速平价普惠时代」
java·开发语言·人工智能
xieliyu.1 小时前
Java 线程池入门详解:线程池基础定义 + ThreadPoolExecutor 全参构造方法逐参数解析
java·开发语言·idea·javaee
lemon_sjdk2 小时前
Reactor几个重要的接口(1)
java·spring·webflux
老白干4 小时前
jjwt 0.9.1 在 JDK 11+ 上的两个“坑”与完整解决方案
java·python·log4j
圆山猫10 小时前
[Virtualization](四):Linux KVM/RISC-V 的 vCPU 运行路径
java·linux·risc-v