通过注解、切面、反射实现返回信息脱敏

简介: 通过注解、切面、反射实现返回信息脱敏
/*** projectName micro-util* package com.open.util.handler.aspect* className SensitiveMethod  * * description: 标注于需要处理的方法上* ** @author joshua_liu* @date 2021/12/20 10:11*/@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documentedpublic@interfaceSensitiveMethod {
/*** 是否启用*/booleanenabled() defaulttrue;
booleanencParamEnabled() defaultfalse;
booleanencResultEnabled() defaultfalse;
booleansensitiveResultEnabled() defaulttrue;
ClasssensitiveClass() defaultInteger.class;
SensitiveMapMeta[] sensitiveMapMeta() default {};
}
/*** projectName micro-util* package com.open.util.handler.aspect* className SensitiveMapMeta  * * description: 标注于需要处理的方法上,当返回类型是map时标注* ** @author joshua_liu* @date 2021/12/20 10:11*/@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documentedpublic@interfaceSensitiveMapMeta {
Stringkey() default"";
ClasssensitiveClass() defaultInteger.class;
booleanencResultEnabled() defaultfalse;
booleansensitiveResultEnabled() defaulttrue;
}
/*** projectName micro-util* package com.open.util.handler.aspect* className EncMeta  * * description: 标注于需要加密的字段上* ** @author joshua_liu* @date 2021/12/20 10:11*/@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documentedpublic@interfaceEncMeta {
}
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documentedpublic@interfaceEncSubMeta {
}
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documentedpublic@interfaceSensitiveMeta {
/*** 开始替换位置*/intrpStart() default0;
/*** 结束替换位置*/intrpEnd() default1;
/*** 替换成的符号*/StringrpSymbol() default"*";
}
@Target({ElementType.FIELD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documentedpublic@interfaceSensitiveSubMeta {
}
@Slf4j@Aspect@Order(Ordered.LOWEST_PRECEDENCE-100)
@Component@ConditionalOnProperty(prefix="open.advice.sensitive", name="enabled", havingValue="true")
publicclassSensitiveInfoAspect {
@AutowiredprivateListsensitiveManageChain=newCopyOnWriteArrayList<>();
@AutowiredprivateTransSensitiveFieldProvidertransSensitiveField;
@Pointcut("@annotation(com.open.util.entity.annotation.SensitiveMethod)")
privatevoidallAnnotationMethod() {
    }
/*** description: PARAMETER 不支持,所有加密的数据都放到body中* //        Object[] args = point.getArgs();* //        Annotation[][] paramAnnotations = method.getParameterAnnotations();* //        for (int i = 0; i < paramAnnotations.length; i++) {* //            for (int j = 0; j < paramAnnotations[i].length; j++) {* //                Annotation currentAnnotation = paramAnnotations[i][j];* //                Object arg = args[i];* //                if (currentAnnotation instanceof EncMeta && Objects.nonNull(arg) && arg instanceof String) {* //                    log.debug("Source args[{}] value {}", i, arg);* //                    args[i] = encManager.decField(String.valueOf(arg));* //                }* //            }* //        }** @param point* @return {@link Object}* @throws* @author joshua_liu* @date 2021/12/21 17:53*/@Around("allAnnotationMethod()")
publicObjectdoAround(ProceedingJoinPointpoint) throwsThrowable {
MethodSignaturesignature= (MethodSignature) point.getSignature();
Methodmethod=signature.getMethod();
SensitiveMethodsensitiveMethodMeta=method.getAnnotation(SensitiveMethod.class);
if (sensitiveMethodMeta.encParamEnabled()) {
ArrayListdecFields=newArrayList<>();
Object[] args=point.getArgs();
Annotation[][] paramAnnotations=method.getParameterAnnotations();
for (inti=0; i<paramAnnotations.length; i++) {
for (intj=0; j<paramAnnotations[i].length; j++) {
AnnotationcurrentAnnotation=paramAnnotations[i][j];
Objectarg=args[i];
if (currentAnnotationinstanceofRequestBody&&Objects.nonNull(arg)) {
Field[] fields=arg.getClass().getDeclaredFields();
transSensitiveField.classifyFields(arg.getClass(), fields, decFields, null, null, null);
log.debug("Source args[{}] value {}", i, arg);
decFields.forEach(e->transSensitiveField.decField(e, arg));
                    }
                }
            }
        }
ObjecttempObj=point.proceed();
if (!sensitiveMethodMeta.enabled() ||Objects.isNull(tempObj)) {
returntempObj;
        }
ListsortedChain=sensitiveManageChain.stream()
                .sorted(Comparator.comparing(SensitiveManageChain::getOrder)).collect(Collectors.toList());
for (inti=0; i<sortedChain.size() -1; i++) {
sortedChain.get(i).setNextChain(sortedChain.get(i+1));
        }
returnsortedChain.get(0).sensitiveHand(tempObj, method, sensitiveMethodMeta, null, newArrayList<>(), newArrayList<>(),
newArrayList<>(), newArrayList<>(), sortedChain);
    }
}



相关文章
|
前端开发 搜索推荐 JavaScript
使用uniapp实现时钟功能
使用uniapp实现时钟功能
532 1
|
传感器 安全
光学雨量计红外雨量传感器的工作原理是基于光学传感技术
光学雨量计红外雨量传感器的工作原理是基于光学传感技术
光学雨量计红外雨量传感器的工作原理是基于光学传感技术
|
机器学习/深度学习 前端开发 PyTorch
【轻量化:蒸馏】都2023年了,你还不会蒸馏操作,难怪你面试不通过!
【轻量化:蒸馏】都2023年了,你还不会蒸馏操作,难怪你面试不通过!
319 0
【轻量化:蒸馏】都2023年了,你还不会蒸馏操作,难怪你面试不通过!
|
IDE Java 开发工具
解决IntelliJ IDEA报错Error:Cannot determine path to ‘tools.jar‘ library for 17 (D:/JAVA)
解决IntelliJ IDEA报错Error:Cannot determine path to ‘tools.jar‘ library for 17 (D:/JAVA)
1338 0
|
11月前
|
存储 传感器 人工智能
「AI实践派」产品生态伙伴Zilliz联合活动
阿里云与向量搜索领域明星企业Zilliz将在杭州阿里巴巴西溪园区共同举办“中外AI产品应用实践和出海实战”分享沙龙。
|
10月前
|
SQL 分布式计算 DataWorks
如何让DataWorks调度依赖一个非DataWorks的任务结点,如数据上传任务?
如何让DataWorks调度依赖一个非DataWorks的任务结点,如数据上传任务?创建一个表的空分区,然后通过DataWorks去检查这个分区。
199 7
|
缓存 网络协议 算法
计算机网络常用知识总结!(二)
计算机网络常用知识总结!
587 4
|
关系型数据库 MySQL Linux
mysql超出最大连接数解决方法
以上就是解决MySQL超出最大连接数的方法,希望对你有所帮助。
275 1
|
传感器 人工智能 网络协议
:嵌入式 Linux 及其用途
【8月更文挑战第24天】
617 0