@Documented @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Order(Ordered.HIGHEST_PRECEDENCE) public @interface PrivateData { }
@Documented @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Order(Ordered.HIGHEST_PRECEDENCE) public @interface PrivateDataMethod { }
首先定义两个自定义注解,privateData
和privateDataMethod
,分别将@Target属性定义为FIELD和METHOD。
@Pointcut("@annotation(com.max.base.services.annotation.PrivateDataMethod)") public void annotationPointCut() { }
对所有添加@privateDataMethod
注解的方法进行切入。
@Around("annotationPointCut()") public Object around(ProceedingJoinPoint joinPoint) { Object responseObj = null; try { Object[] request = joinPoint.getArgs(); for (Object object : request) { if (object instanceof Collection) { Collection collection = (Collection) object; collection.forEach(var -> { try { handleEncrypt(var); } catch (IllegalAccessException e) { e.printStackTrace(); } }); } else { handleEncrypt(object); } } responseObj = joinPoint.proceed(); if (responseObj instanceof Collection) { Collection collection = (Collection) responseObj; collection.forEach(var -> { try { handleDecrypt(var); } catch (IllegalAccessException e) { e.printStackTrace(); } }); } else { handleDecrypt(responseObj); } } catch (Throwable throwable) { throwable.printStackTrace(); log.error("SecureFieldAop 异常{}", throwable); } return responseObj; }
申明Aroud通知,对于方法输入输出的对象进行判断,如果是非集合对象则直接进行加解密操作,否则则拆分集合,逐一操作
/** * 处理加密 * @param requestObj */ private void handleEncrypt(Object requestObj) throws IllegalAccessException { if (Objects.isNull(requestObj)) { return; } Field[] fields = requestObj.getClass().getDeclaredFields(); for (Field field : fields) { boolean hasSecureField = field.isAnnotationPresent(PrivateData.class); if (hasSecureField) { Boolean accessible = field.isAccessible(); if (!accessible) { field.setAccessible(true); } String plaintextValue = (String) field.get(requestObj); String encryptValue = AseUtil.encrypt(plaintextValue, secretKey); field.set(requestObj, encryptValue); if (!accessible) { field.setAccessible(false); } } } }
通过反射获取对象的Field列表,对于拥有@PrivateData
注解的字段执行**encryptValue()**方法并用加密后的字符串覆盖原字段。
解密逻辑与加密类似,不做赘述。
insert()
方法为需要加密的方法public interface CmTenantMapper { int deleteByPrimaryKey(Long id); @PrivateDataMethod int insert(CmTenant record); int insertSelective(CmTenant record); CmTenant selectByPrimaryKey(Long id); int updateByPrimaryKeySelective(CmTenant record); int updateByPrimaryKey(CmTenant record); }
public class CmTenant { private Long id; private String tenantId; @PrivateData private String tenantName; private String createBy; private Date createDate; private String updateBy; private Date updateDate; private String remarks; private Byte delFlag; //set get...
{ "createBy": "可乐可不乐", "delFlag": "NOTDELETE", "remarks": "测试加密", "tenantId": "996", "tenantName": "椅子团队出品", "updateBy": "可乐可不乐" }
数据库保存对象
脱敏逻辑与加解密基本一致,需要注意的一点是脱敏的注解需要添加type
类型
@Documented @Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Order(Ordered.HIGHEST_PRECEDENCE) public @interface MaskingField { MaskingTypeEnum type(); }
在MaskingTypeEnum
中定义脱敏的分类
public enum MaskingTypeEnum { /*身份证号码*/ ID_CARD, /*手机号码*/ PHONE, /*地址*/ ADDRESS, /*姓名*/ NAME }
在使用是MaskingTypeEnum
时标识字段的类型
@MaskingField(type = MaskingTypeEnum.NAME) private String cpName;
后续~~bug~~
功能大家自行研究, peace~
本文由博客一文多发平台 OpenWrite 发布!