大家好,我是一安,今天聊一下springboot利用aop+jasypt实现对传入敏感数据的脱敏。

前言

不管什么项目,总会有一些配置信息或用户名、手机号等敏感信息暴露出来,而jasypt可以采用简单的方式来解决java开发中的数据脱敏问题 ,不管是项目中配置文件还是用户的敏感信息,jasypt都能够轻松的嵌入其中。

引入jasypt依赖

@Slf4j
@Aspect
@Component
public class DecryptAspect {
    @Autowired
    private StringEncryptor stringEncryptor;

    @Pointcut("@annotation(com.capitek.dataEncryption.annotation.NeedDecrypt)")
    public void pointCut() {
    }

    @Around("pointCut()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        //解密
        Object result = decrypt(joinPoint);
        return result;
    }

    public Object decrypt(ProceedingJoinPoint joinPoint) {
        Object result = null;
        try {
            Object obj = joinPoint.proceed();
            if (obj != null) {         
                if (obj instanceof String) {
                    decryptValue(obj);
                } else {
                    result = decryptData(obj);
                }
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return result;
    }

    private Object decryptData(Object obj) throws IllegalAccessException {

        if (Objects.isNull(obj)) {
            return null;
        }
        if(obj instanceof ResultDTO){
            decryptResult(obj);
        }else if (obj instanceof ArrayList) {
            decryptList(obj);
        } else {
            decryptObj(obj);
        }
        return obj;
    }

    /**
     * 针对单个实体类进行解密
     * @param obj
     * @throws IllegalAccessException
     */
    private void decryptObj(Object obj) throws IllegalAccessException {
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            boolean hasSecureField = field.isAnnotationPresent(EncryptFields.class);
            if (hasSecureField) {
                field.setAccessible(true);
                String realValue = (String) field.get(obj);             
                String value = stringEncryptor.decrypt(realValue);
                
                 //自定义加密解密
                //String value = MyEncryptUtil.decrypt(realValue,"551b789c25cfbe731f723eab5906de5c");
                field.set(obj, value);
            }
        }
    }

    /**
     * 针对list<实体来> 进行反射、解密
     * @param obj
     * @throws IllegalAccessException
     */
    private void decryptList(Object obj) throws IllegalAccessException {
        if (obj instanceof ArrayList) {
            for (Object o : (List<?>) obj) {
                decryptObj(o);
            }
        }
    }
    /**
     * 针对自定义result进行反射、解密
     * @param obj
     * @throws IllegalAccessException
     */
    private void decryptResult(Object obj) throws IllegalAccessException {
        if (obj instanceof ResultDTO) {
            if(((ResultDTO) obj).getData() instanceof ArrayList){
                decryptList(((ResultDTO) obj).getData());
            }else{
                decryptObj(((ResultDTO) obj).getData());
            }
        }
    }

    public String decryptValue(Object realValue) {
        try {
            //也可自定义加密解密,这里用自带的
            realValue = stringEncryptor.encrypt(String.valueOf(realValue));
            
            //自定义加密解密
           //realValue = MyEncryptUtil.decrypt(String.valueOf(realValue),"551b789c25cfbe731f723eab5906de5c");
        } catch (Exception e) {
            log.info("解密异常={}", e.getMessage());
        }
        return String.valueOf(realValue);
    }
}


本篇文章来源于微信公众号: 一安未来



微信扫描下方的二维码阅读本文

此作者没有提供个人介绍
最后更新于 2023-06-27