日志脱敏是常见的安全需求。普通的基于工具类方法的方式,对代码的入侵性太强。编写起来又特别麻烦。java
本项目提供基于注解的方式,而且内置了常见的脱敏方式,便于开发。git
用户也能够基于本身的实际须要,自定义注解。github
基于注解的日志脱敏数组
能够自定义策略实现,策略生效条件安全
常见的脱敏内置方案maven
java 深拷贝,且原始对象不用实现任何接口。工具
<dependency> <groupId>com.github.houbb</groupId> <artifactId>sensitive-core</artifactId> <version>0.0.2</version> </dependency>
咱们对 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
。ui
会遍历每个属性,执行上面的脱敏策略。日志
会处理对象中各个字段上的脱敏注解信息。
遍历每个对象,处理对象中各个字段上的脱敏注解信息。
做为演示,集合中为普通的字符串。
public class UserEntryBaseType { @SensitiveEntry @Sensitive(strategy = StrategyChineseName.class) private List<String> chineseNameList; @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 */ @Test public 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 List<User> userList; @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 */ @Test public 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'}]}
若是你对本项目有兴趣,而且对代码有必定追求,能够申请加入本项目开发。
若是你善于写文档,或者愿意补全测试案例,也很是欢迎加入。