数据落库自动加解密实现

数据落库自动加解密

注解

/**
 * @author: guanjian
 * @date: 2020/07/18 8:09
 * @description: 数据保护注解(方法注解)
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DataProtect {
}
/**
 * @author: guanjian
 * @date: 2020/07/18 8:09
 * @description: 对称解密
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Decrypt {
}
/**
 * @author: guanjian
 * @date: 2020/07/18 8:09
 * @description: 对称加密
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Encrpty {
}

逻辑切面

/**
 * @author: guanjian
 * @date: 2020/07/18 8:12
 * @description: 数据保护处理
 */
@Aspect
@Component("dataProtectAspect")
public class DataProtectAspect {

    @Resource
    private AksRpc aksRpc;

    @Around("@annotation(xxx.service.aspect.annotation.DataProtect)")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        Assert.notEmpty(pjp.getArgs(), "args can not be null.");

        Stream.of(pjp.getArgs()).forEach(arg -> {
            if (arg instanceof Collection) {
                Collection collection = (Collection) arg;
                collection.forEach(x -> handleObject(x));
            } else {
                handleObject(arg);
            }
        });

        Object result = pjp.proceed();


        if (result instanceof Collection) {
            Collection collection = (Collection) result;
            collection.forEach(x -> {
                handleObject(x);
            });
        } else {
            handleObject(result);
        }

        return result;
    }

    /**
     * 处理属性对象
     *
     * @param arg 对象
     * @return
     */
    private void handleObject(Object arg) {
        Optional.ofNullable(arg)
                .map(Object::getClass)
                .map(Class::getDeclaredFields)
                .ifPresent(fields ->
                        Stream.of(fields)
                                .forEach(field -> {
                                    try {
                                        //放行未注解加解密字段
                                        if (!field.isAnnotationPresent(Encrpty.class) && !field.isAnnotationPresent(Decrypt.class)) return;

                                        //过滤属性为空
                                        field.setAccessible(true);
                                        Object value = field.get(arg);
                                        if (Objects.isNull(value)) return;

                                        //属性为对象,进行递归
                                        if (isParseByObject(value.getClass())) {
                                            handleObject(value);
                                        }
                                        //属性为字段
                                        else {
                                            handleField(field, arg);
                                        }
                                    } catch (IllegalAccessException e) {
                                        e.printStackTrace();
                                    }
                                }));
    }

    /**
     * 解析字段属性
     *
     * @param field 字段
     * @param arg   对象
     * @return
     */
    private void handleField(Field field, Object arg) {
        //仅处理字符串类型字段
        if (field.getType() != String.class) return;

        if (field.isAnnotationPresent(Encrpty.class)) {
            try {
                field.setAccessible(true);
                Object value = field.get(arg);
                if (StringUtils.isBlank((CharSequence) value)) return;
                String ciphertext = aksRpc.encryptString(String.valueOf(value));
                field.set(arg, ciphertext);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        if (field.isAnnotationPresent(Decrypt.class)) {
            try {
                field.setAccessible(true);
                Object value = field.get(arg);
                if (StringUtils.isBlank((CharSequence) value)) return;
                String ciphertext = aksRpc.decryptString(String.valueOf(value));
                field.set(arg, ciphertext);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    }

    private boolean isParseByObject(Class clz) {
        try {
            return !clz.isPrimitive()
                    && clz != Date.class
                    && clz != BigDecimal.class
                    && clz != BigInteger.class
                    && !isWrapClass(clz)
                    && clz != String.class;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    private static boolean isWrapClass(Class clz) {
        try {
            return ((Class) clz.getField("TYPE").get(null)).isPrimitive();
        } catch (Exception e) {
            return false;
        }
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大摩羯先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值