springboot项目通过自定义注解+aop实现对入参加解密

news/2025/2/19 8:51:05/

1.创建自定义注解:

@Target({ElementType.FIELD, ElementType.PARAMETER})
@Retention(RetentionPolicy.RUNTIME)
public @interface EncryptDecrypt {
}

2.创建加解密工具类:

public class EncryptionUtils {// 加密方法public static String encrypt(String data) {// 实现加密逻辑// ...return encryptedData;}// 解密方法public static String decrypt(String encryptedData) {// 实现解密逻辑// ...return decryptedData;}
}

3.创建切面类:

@Aspect
@Component
public class EncryptionAspect {@Around("execution(* com.example.*.*(..)) && (@annotation(com.example.EncryptDecrypt) || @args(com.example.EncryptDecrypt))")public Object encryptDecryptMethod(ProceedingJoinPoint joinPoint) throws Throwable {// 获取方法参数Object[] args = joinPoint.getArgs();// 遍历参数,对带有 EncryptDecrypt 注解的参数进行加解密操作for (int i = 0; i < args.length; i++) {Object arg = args[i];if (arg != null) {if (arg instanceof String) {if (arg.getClass().isAnnotationPresent(EncryptDecrypt.class)) {String encryptedData = (String) arg;String decryptedData = EncryptionUtils.decrypt(encryptedData);args[i] = decryptedData;}} else {Field[] fields = arg.getClass().getDeclaredFields();for (Field field : fields) {if (field.isAnnotationPresent(EncryptDecrypt.class)) {field.setAccessible(true);Object fieldValue = field.get(arg);if (fieldValue instanceof String) {String encryptedData = (String) fieldValue;String decryptedData = EncryptionUtils.decrypt(encryptedData);field.set(arg, decryptedData);}}}}}}// 执行原方法Object result = joinPoint.proceed(args);// 对返回值进行加密操作(可根据需求进行操作)return result;}
}

4.参数解释

@Target({ElementType.FIELD, ElementType.PARAMETER}) 是一个注解的元注解,用于指定注解可以应用的目标元素类型。在这个例子中,@Target 注解表明注解 EncryptDecrypt 可以应用于字段(Field)和参数(Parameter)上。ElementType.FIELD 表示注解可以应用于字段上,即类的成员变量。
ElementType.PARAMETER 表示注解可以应用于方法的参数上。
通过指定这些目标元素类型,你可以限制注解的使用范围,确保它只能应用在指定的地方。在这个场景中,EncryptDecrypt 注解旨在用于标记需要进行加解密操作的字段和方法参数。
1.execution(* com.example.*.*(..)):表示匹配 com.example 包及其子包下的所有类的所有方法。execution() 是切点函数,用于匹配方法执行的连接点。* 表示匹配任意返回类型的方法。com.example.* 表示匹配 com.example 包下的任意类。*.*(..) 表示匹配任意方法名和参数的方法。2.(@annotation(com.example.EncryptDecrypt) || @args(com.example.EncryptDecrypt)):表示匹配带有 EncryptDecrypt 注解的方法或参数。@annotation(com.example.EncryptDecrypt) 匹配带有 EncryptDecrypt 注解的方法。@args(com.example.EncryptDecrypt) 匹配带有 EncryptDecrypt 注解的参数。

-----------------------------------------分割线-------------------------------------------------------------------------------------------

对出参进行解密
注解类

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Decrypt {String value() default "";
}
mport org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;import java.lang.reflect.Field;
import java.util.Collection;@Aspect
@Component
public class DecryptionAspect {@Around("execution(* com.example.*.*(..))")public Object decryptMethod(ProceedingJoinPoint joinPoint) throws Throwable {// 执行原方法并获取返回值Object result = joinPoint.proceed();// 对带有 @Decrypt 注解的字段进行解密操作if (result != null) {if(result instanceof GatewayResp){//GatewayResp 为统一定义的出参 包括code,msg,dataGatewayResp r = (GatewayResp) result;Object data = r.getData();if(data instanceof Collection<?>) {//集合data = decryptCollectionFields((Collection<?>)data);}else{//单对象data = decryptObjectFields(data);}r.setData(data);}}return result;}private Collection<?> decryptCollectionFields(Collection<?> collection) throws IllegalAccessException {for (Object object : collection) {decryptObjectFields(object);}return collection;}private Object decryptObjectFields(Object object) throws IllegalAccessException {Field[] fields = object.getClass().getDeclaredFields();for (Field field : fields) {// 如果字段带有 @Decrypt 注解,则对其进行解密操作if (field.isAnnotationPresent(Decrypt.class)) {field.setAccessible(true);Object fieldValue = field.get(object);if (fieldValue instanceof String) {String decryptedValue = EncryptionUtils.decrypt((String) fieldValue);field.set(object, decryptedValue);}} else {// 如果字段不带有 @Decrypt 注解,则递归调用 decryptObjectFields() 方法field.setAccessible(true);Object fieldValue = field.get(object);if (fieldValue != null) {if (fieldValue instanceof Collection<?>) {decryptCollectionFields((Collection<?>) fieldValue);} else if(isCustomObject(fieldValue)){decryptObjectFields(fieldValue);}}}}return object;}/*** 判断一个对象不是八大基本数据类型和String类型*/private boolean isCustomObject(Object object) {Class<?> objectType = object.getClass();// 判断是否为八大基本数据类型if (objectType.isPrimitive()) {return false;}// 判断是否为String类型if (objectType.equals(String.class)) {return false;}return true;}
}

http://www.ppmy.cn/news/1185932.html

相关文章

【文件存储服务器】Minio使用

文章目录 2.2 Minio使用2.2.1 Minio介绍2.2.2 Minio安装Windows安装Linux安装 2.2.3 Minio入门 2.3 上传文件接口2.3.1 FileUploadController2.3.2 FileUploadService2.3.3 MinioProperties2.3.4 配置文件内容2.3.5 主启动类加上EnableConfigurationProperties 2.4 前端对接 2.…

基于Java8的CompletableFuture实现的异步执行工具类

异步执行工具类 前言 基于Java8的CompletableFuture实现的异步执行工具类 package com.jareny.jave.design.thread;import lombok.extern.slf4j.Slf4j; import java.util.ArrayList; import java.util.List; import java.util.Objects; import java.util.Optional; import j…

python引用计数

#python 垃圾回收算法 1、引用计数 2、标记-清除 3、分代收集import sysa[1,2,3] # print(dir(a)) ba ca da print(id(a),id(b)) print(id(c),id(d)) #sys.getrescount()可以获得对象的引用计数 print(sys.getrefcount(a)-1) print(sys.getrefcount(b)-1) a[1,2,3,4] print(sys…

蓝凌EIS智慧协同平台saveImg接口存在任意文件上传漏洞

蓝凌EIS智慧协同平台saveImg接口存在任意文件上传漏洞 一、蓝凌EIS简介二、漏洞描述三、影响版本四、fofa查询语句五、漏洞复现六、深度复现1、发送如花2、哥斯拉直连 免责声明&#xff1a;请勿利用文章内的相关技术从事非法测试&#xff0c;由于传播、利用此文所提供的信息或者…

memcmp内存比较函数

1.头文件&#xff1a;string.h 2.函数原型&#xff1a;int memcmp(const void *ptr1, const void *ptr2, size_t num) 3.函数功能&#xff1a;比较两块内存中数据的大小 4.参数解释&#xff1a; 比较 ptr1 和 ptr2 两个指针所指向的两块内存中的前 num 个字节 5.返回值&am…

轻量封装WebGPU渲染系统示例<5>-多重纹理(源码)

当前示例源码github地址: https://github.com/vilyLei/voxwebgpu/blob/version-1.01/src/voxgpu/sample/MultiTexturedCube.ts 此示例渲染系统实现的特性: 1. 用户态与系统态隔离。 2. 高频调用与低频调用隔离。 3. 面向用户的易用性封装。 4. 渲染数据和渲染机制分离。 …

基于单片机的太阳跟踪系统的设计

欢迎大家点赞、收藏、关注、评论啦 &#xff0c;由于篇幅有限&#xff0c;只展示了部分核心代码。 技术交流认准下方 CSDN 官方提供的联系方式 文章目录 概要 一、设计的主要内容二、硬件电路设计2.1跟踪控制方案的选择2.1.1跟踪系统坐标系的选择2.2系统总体设计及相关硬件介绍…

一文详解如何从 Oracle 迁移数据到 DolphinDB

Oracle 是一个广泛使用的关系型数据库管理系统&#xff0c;它支持 ACID 事务处理&#xff0c;具有强大的安全性和可靠性&#xff0c;因此被广泛应用于各种企业级应用程序。但是&#xff0c;随着数据规模的增加和业务需求的变化&#xff0c;Oracle 的一些限制和缺点也逐渐暴露出…