/*** projectName micro-util* package com.open.util.handler.provider* className TransSensitiveFieldProvider* <p>* description: 敏感字段转换* </p>** @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/12/21 9:31*/prefix="open.advice.sensitive", name="enabled", havingValue="true") (publicclassTransSensitiveFieldProvider { privateSensitiveEncProvidersensitiveEncProvider; "unchecked") (publicvoidcollectionObjSensitive(ArrayListcollectionObj, List<Field>senFields) { collectionObj.forEach(eleObj->senFields.forEach(senField->sensitiveField(senField, eleObj))); } "unchecked") (publicvoidcollectionObjEnc(ArrayListcollectionObj, List<Field>encFields) { collectionObj.forEach(eleObj->encFields.forEach(encField->encField(encField, eleObj))); } publicvoidsensitiveField(FieldsenField, Objecttarget) { try { senField.setAccessible(true); ObjecttempFieldValue=senField.get(target); StringstringTempFieldValue=String.valueOf(tempFieldValue); StringnullStr="null"; if (Objects.nonNull(stringTempFieldValue) &&!Objects.equals(nullStr, stringTempFieldValue)) { SensitiveMetasensitiveMeta=senField.getAnnotation(SensitiveMeta.class); intend=Math.min(stringTempFieldValue.length(), sensitiveMeta.rpEnd()); StringsubStr=stringTempFieldValue.substring(sensitiveMeta.rpStart(), end); senField.set(target, stringTempFieldValue.replace(subStr, sensitiveMeta.rpSymbol())); } } catch (IllegalAccessExceptionex) { log.error("Sensitive catch exception:", ex); } } publicvoidencField(FieldencField, Objecttarget) { try { encField.setAccessible(true); ObjecttempFieldValue=encField.get(target); StringstringTempFieldValue=String.valueOf(tempFieldValue); StringnullStr="null"; if (Objects.nonNull(stringTempFieldValue) &&!Objects.equals(nullStr, stringTempFieldValue)) { StringaftEncValue=sensitiveEncProvider.encField(stringTempFieldValue); encField.set(target, aftEncValue); } } catch (Exceptionex) { log.error("Enc field catch exception:", ex); } } publicvoiddecField(FielddecField, Objecttarget) { try { decField.setAccessible(true); ObjecttempFieldValue=decField.get(target); StringstringTempFieldValue=String.valueOf(tempFieldValue); StringnullStr="null"; if (Objects.nonNull(stringTempFieldValue) &&!Objects.equals(nullStr, stringTempFieldValue)) { StringaftEncValue=sensitiveEncProvider.decField(stringTempFieldValue); decField.set(target, aftEncValue); } } catch (Exceptionex) { log.error("Dec field catch exception:", ex); } } publicvoidclassifyFields(Class<?>sourceClass, Field[] fields, ArrayList<Field>encFields, ArrayList<Field>senFields, ArrayList<Field>subEncFields, ArrayList<Field>subSenFields) { List<Field>fieldList=Stream.of(fields).collect(Collectors.toList()); if (sourceClass.isAnnotationPresent(SensitiveMeta.class)) { if (Objects.nonNull(senFields)) { senFields.addAll(fieldList); } fieldList.forEach(e-> { if (e.isAnnotationPresent(SensitiveSubMeta.class) &&Objects.nonNull(senFields)) { senFields.remove(e); } if (e.isAnnotationPresent(SensitiveSubMeta.class) &&Objects.nonNull(subSenFields)) { subSenFields.add(e); } }); } if (sourceClass.isAnnotationPresent(EncMeta.class)) { if (!CollectionUtils.isEmpty(encFields)) { encFields.addAll(fieldList); } fieldList.forEach(e-> { if (e.isAnnotationPresent(EncSubMeta.class) &&Objects.nonNull(encFields)) { encFields.remove(e); } if (e.isAnnotationPresent(EncSubMeta.class) &&Objects.nonNull(subEncFields)) { subEncFields.add(e); } }); } fieldList.forEach(e-> { booleanannotationSenPresent=e.isAnnotationPresent(SensitiveMeta.class) &&!sourceClass.isAnnotationPresent(SensitiveMeta.class); booleanannotationEncPresent=e.isAnnotationPresent(EncMeta.class) &&!sourceClass.isAnnotationPresent(EncMeta.class); if (annotationEncPresent&&annotationSenPresent) { encFields.add(e); senFields.add(e); } elseif (annotationEncPresent) { encFields.add(e); } elseif (annotationSenPresent) { senFields.add(e); } }); } }
/*** projectName micro-util* package com.open.util.handler.aspect.sensitive4chain* className SensitiveManageChain* <p>* description:* </p>** @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/12/21 9:17*/publicinterfaceSensitiveManageChainextendsOrdered { /*** description:** @param sourceResult 原返回结果* @param method 当前处理方法* @param sensitiveMeta 敏感信息方法元数据* @param sourceMapMeta* @param senFields 敏感字段集合* @param encFields 加密字段集合* @param subEncFields* @param subSenFields* @param sortedChain 有序链路* @return {@link Object} 处理后的返回结果* @throws* @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/12/21 9:19*/ObjectsensitiveHand(ObjectsourceResult, Methodmethod, SensitiveMethodsensitiveMeta, SensitiveMapMetasourceMapMeta, ArrayList<Field>senFields, ArrayList<Field>encFields, ArrayList<Field>subEncFields, ArrayList<Field>subSenFields, List<SensitiveManageChain>sortedChain); /*** description: 设置下一个链路** @param chain 下一个链路* @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/12/21 9:42*/voidsetNextChain(SensitiveManageChainchain); }
/*** projectName micro-util* package com.open.util.handler.aspect.sensitive4chain* className CollectionResultHandler* <p>* description:* </p>** @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/12/21 10:05*/prefix="open.advice.sensitive", name="enabled", havingValue="true") (publicclassCollectionManageChainimplementsSensitiveManageChain { privateintorder; privateSensitiveManageChainnextChain; privateTransSensitiveFieldProvidertransSensitiveField; /*** description:** @param sourceResult 原返回结果* @param method 当前处理方法* @param sensitiveMeta 敏感信息方法元数据* @param sourceMapMeta* @param senFields 敏感字段集合* @param encFields 加密字段集合* @param subEncFields* @param subSenFields* @param sortedChain 有序链路* @return {@link Object} 处理后的返回结果* @throws* @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/12/21 9:19*/"unchecked") (publicObjectsensitiveHand(ObjectsourceResult, Methodmethod, SensitiveMethodsensitiveMeta, SensitiveMapMetasourceMapMeta, ArrayList<Field>senFields, ArrayList<Field>encFields, ArrayList<Field>subEncFields, ArrayList<Field>subSenFields, List<SensitiveManageChain>sortedChain) { if (Objects.isNull(sourceResult)) { returnnull; } if (sourceResultinstanceofCollection) { ArrayListcollectionObj=newArrayList(); ClasssensitiveClass=Objects.nonNull(sourceMapMeta) ?sourceMapMeta.sensitiveClass() : sensitiveMeta.sensitiveClass(); if (Objects.equals(sensitiveClass, Integer.class)) { log.warn("Return collection need to config sensitiveClass() ,but not found method {},please check you config.", method); returnsourceResult; } Field[] fields=sensitiveClass.getDeclaredFields(); if (fields.length<=0) { returnsourceResult; } Collectioncollection= (Collection) sourceResult; if (!CollectionUtils.isEmpty(collection)) { collectionObj.addAll(collection); } transSensitiveField.classifyFields(sensitiveClass, fields, encFields, senFields, subEncFields, subSenFields); booleansenResultEnabled=Objects.nonNull(sourceMapMeta) ?sourceMapMeta.sensitiveResultEnabled() : sensitiveMeta.sensitiveResultEnabled(); if (sensitiveMeta.sensitiveResultEnabled() &&!CollectionUtils.isEmpty(senFields)) { transSensitiveField.collectionObjSensitive(collectionObj, senFields); } if (sensitiveMeta.encResultEnabled() &&!CollectionUtils.isEmpty(encFields)) { transSensitiveField.collectionObjEnc(collectionObj, encFields); } returncollectionObj; } if (Objects.isNull(nextChain)) { returnsourceResult; } returnnextChain.sensitiveHand(sourceResult, method, sensitiveMeta, sourceMapMeta, senFields, encFields, subEncFields, subSenFields, sortedChain); } }
/*** projectName micro-util* package com.open.util.handler.aspect.sensitive4chain* classname PageManageChain* <p>* description* </p>** @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/12/22 23:09*/prefix="open.advice.sensitive", name="enabled", havingValue="true") (publicclassPageManageChainimplementsSensitiveManageChain { privateintorder; privateSensitiveManageChainnextChain; privateTransSensitiveFieldProvidertransSensitiveField; /*** description:** @param sourceResult 原返回结果* @param method 当前处理方法* @param sensitiveMeta 敏感信息方法元数据* @param sourceMapMeta* @param senFields 敏感字段集合* @param encFields 加密字段集合* @param sortedChain 有序链路* @return {@link Object} 处理后的返回结果* @throws* @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/12/21 9:19*/"unchecked") (publicObjectsensitiveHand(ObjectsourceResult, Methodmethod, SensitiveMethodsensitiveMeta, SensitiveMapMetasourceMapMeta, ArrayList<Field>senFields, ArrayList<Field>encFields, ArrayList<Field>subEncFields, ArrayList<Field>subSenFields, List<SensitiveManageChain>sortedChain) { if (Objects.isNull(sourceResult)) { returnnull; } if (sourceResultinstanceofPage) { ArrayListcollectionObj=newArrayList(); Pageablepageable; longtotal; ClasssensitiveClass=sensitiveMeta.sensitiveClass(); if (Objects.equals(sensitiveClass, Integer.class)) { log.warn("Return page need to config SensitiveMethod.sensitiveClass() ,but not found method {},please check you config.", method); returnsourceResult; } Field[] fields=sensitiveMeta.sensitiveClass().getDeclaredFields(); if (fields.length<=0) { returnsourceResult; } PageModelpageResult= (PageModel) sourceResult; total=pageResult.getTotal(); pageable=pageResult.getPageable(); Collectioncollection=pageResult.getContent(); if (!CollectionUtils.isEmpty(collection)) { collectionObj.addAll(collection); } transSensitiveField.classifyFields(sourceResult.getClass(), fields, encFields, senFields, subEncFields, subSenFields); if (sensitiveMeta.sensitiveResultEnabled() &&!CollectionUtils.isEmpty(senFields)) { transSensitiveField.collectionObjSensitive(collectionObj, senFields); returnPageModel.pageSuccess(collectionObj, pageable, total); } if (sensitiveMeta.encResultEnabled() &&!CollectionUtils.isEmpty(encFields)) { transSensitiveField.collectionObjEnc(collectionObj, encFields); } } if (Objects.isNull(nextChain)) { returnsourceResult; } returnnextChain.sensitiveHand(sourceResult, method, sensitiveMeta, sourceMapMeta, senFields, encFields, subEncFields, subSenFields, sortedChain); } }
/*** projectName micro-util* package com.open.util.handler.aspect.sensitive4chain* className GenericManageChain* <p>* description:* </p>** @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/12/21 10:12*/prefix="open.advice.sensitive", name="enabled", havingValue="true") (publicclassGenericManageChainimplementsSensitiveManageChain { privateintorder=Ordered.LOWEST_PRECEDENCE-1000; privateSensitiveManageChainnextChain=null; privateTransSensitiveFieldProvidertransSensitiveField; /*** description:** @param sourceResult 原返回结果* @param method 当前处理方法* @param sensitiveMeta 敏感信息方法元数据* @param sourceMapMeta* @param senFields 敏感字段集合* @param encFields 加密字段集合* @param subEncFields* @param subSenFields* @param sortedChain 有序链路* @return {@link Object} 处理后的返回结果* @throws* @author <a href="mailto:joshualwork@163.com">joshua_liu</a>* @date 2021/12/21 9:19*/publicObjectsensitiveHand(ObjectsourceResult, Methodmethod, SensitiveMethodsensitiveMeta, SensitiveMapMetasourceMapMeta, ArrayList<Field>senFields, ArrayList<Field>encFields, ArrayList<Field>subEncFields, ArrayList<Field>subSenFields, List<SensitiveManageChain>sortedChain) { if (Objects.isNull(sourceResult)) { returnnull; } Class<?>sourceClass=sourceResult.getClass(); Field[] fields=sourceResult.getClass().getDeclaredFields(); if (fields.length<=0) { returnsourceResult; } transSensitiveField.classifyFields(sourceClass, fields, encFields, senFields, subEncFields, subSenFields); if (sensitiveMeta.sensitiveResultEnabled()) { senFields.forEach(field->transSensitiveField.sensitiveField(field, sourceResult)); } if (sensitiveMeta.encResultEnabled()) { encFields.forEach(field->transSensitiveField.encField(field, sourceResult)); } returnsourceResult; } }