项目介绍
日志脱敏是常见的安全需求。普通的基于工具类方法的方式,对代码的入侵性太强。编写起来又特别麻烦。
本项目提供基于注解的方式,并且内置了常见的脱敏方式,便于开发。
用户也可以基于自己的实际需要,自定义注解。
特性
- 基于注解的日志脱敏
- 可以自定义策略实现,策略生效条件
- 常见的脱敏内置方案
- java 深拷贝,且原始对象不用实现任何接口。
快速开始
maven 导入
com.github.houbb sensitive-core 0.0.2
定义对象
- User.java
我们对 password 使用脱敏,指定脱敏策略为 StrategyPassword。(直接返回 null)
public class User { @Sensitive(strategy = StrategyChineseName.class) private String username; @Sensitive(strategy = StrategyCardId.class) private String idCard; @Sensitive(strategy = StrategyPassword.class) private String password; @Sensitive(strategy = StrategyEmail.class) private String email; @Sensitive(strategy = StrategyPhone.class) private String phone; //Getter & Setter //toString()}
属性为集合或者对象
如果某个属性是单个集合或者对象,则需要使用注解 @SensitiveEntry
。
- 放在集合属性上,且属性为普通对象
会遍历每一个属性,执行上面的脱敏策略。
- 放在对象属性上
会处理对象中各个字段上的脱敏注解信息。
- 放在集合属性上,且属性为对象
遍历每一个对象,处理对象中各个字段上的脱敏注解信息。
放在集合属性上,且属性为普通对象
- UserEntryBaseType.java
作为演示,集合中为普通的字符串。
public class UserEntryBaseType { @SensitiveEntry @Sensitive(strategy = StrategyChineseName.class) private ListchineseNameList; @SensitiveEntry @Sensitive(strategy = StrategyChineseName.class) private String[] chineseNameArray; //Getter & Setter & toString()}
- 构建对象
/** * 构建用户-属性为列表,列表中为基础属性 * @return 构建嵌套信息 * @since 0.0.2 */public static UserEntryBaseType buildUserEntryBaseType() { UserEntryBaseType userEntryBaseType = new UserEntryBaseType(); userEntryBaseType.setChineseNameList(Arrays.asList("盘古", "女娲", "伏羲")); userEntryBaseType.setChineseNameArray(new String[]{"盘古", "女娲", "伏羲"}); return userEntryBaseType;}
- 测试演示
/** * 用户属性中有集合或者map,集合中属性是基础类型-脱敏测试 * @since 0.0.2 */@Testpublic void sensitiveEntryBaseTypeTest() { UserEntryBaseType userEntryBaseType = DataPrepareTest.buildUserEntryBaseType(); System.out.println("脱敏前原始: " + userEntryBaseType); UserEntryBaseType sensitive = SensitiveUtil.desCopy(userEntryBaseType); System.out.println("脱敏对象: " + sensitive); System.out.println("脱敏后原始: " + userEntryBaseType);}
- 日志信息
脱敏前原始: UserEntryBaseType{chineseNameList=[盘古, 女娲, 伏羲], chineseNameArray=[盘古, 女娲, 伏羲]}脱敏对象: UserEntryBaseType{chineseNameList=[*古, *娲, *羲], chineseNameArray=[*古, *娲, *羲]}脱敏后原始: UserEntryBaseType{chineseNameList=[盘古, 女娲, 伏羲], chineseNameArray=[盘古, 女娲, 伏羲]}
放在对象属性上
- 演示对象
这里的 User 和上面的 User 对象一致。
public class UserEntryObject { @SensitiveEntry private User user; @SensitiveEntry private ListuserList; @SensitiveEntry private User[] userArray; //...}
- 对象构建
/** * 构建用户-属性为列表,数组。列表中为对象。 * @return 构建嵌套信息 * @since 0.0.2 */public static UserEntryObject buildUserEntryObject() { UserEntryObject userEntryObject = new UserEntryObject(); User user = buildUser(); User user2 = buildUser(); User user3 = buildUser(); userEntryObject.setUser(user); userEntryObject.setUserList(Arrays.asList(user2)); userEntryObject.setUserArray(new User[]{user3}); return userEntryObject;}
- 测试演示
/** * 用户属性中有集合或者对象,集合中属性是对象-脱敏测试 * @since 0.0.2 */@Testpublic void sensitiveEntryObjectTest() { UserEntryObject userEntryObject = DataPrepareTest.buildUserEntryObject(); System.out.println("脱敏前原始: " + userEntryObject); UserEntryObject sensitiveUserEntryObject = SensitiveUtil.desCopy(userEntryObject); System.out.println("脱敏对象: " + sensitiveUserEntryObject); System.out.println("脱敏后原始: " + userEntryObject);}
- 测试结果
脱敏前原始: UserEntryObject{user=User{username='脱敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}, userList=[User{username='脱敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}], userArray=[User{username='脱敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}]}脱敏对象: UserEntryObject{user=User{username='脱*君', idCard='123456**********34', password='null', email='123**@qq.com', phone='188****8888'}, userList=[User{username='脱*君', idCard='123456**********34', password='null', email='123**@qq.com', phone='188****8888'}], userArray=[User{username='脱*君', idCard='123456**********34', password='null', email='123**@qq.com', phone='188****8888'}]}脱敏后原始: UserEntryObject{user=User{username='脱敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}, userList=[User{username='脱敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}], userArray=[User{username='脱敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}]}
需求 & BUGS
欢迎加入开发
如果你对本项目有兴趣,并且对代码有一定追求,可以申请加入本项目开发。
如果你善于写文档,或者愿意补全测试案例,也非常欢迎加入。