日誌脫敏是常見的安全需求。普通的基於工具類方法的方式,對代碼的入侵性太強。編寫起來又特別麻煩。java
本項目提供基於註解的方式,而且內置了常見的脫敏方式,便於開發。git
用戶也能夠基於本身的實際須要,自定義註解。github
<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
。安全
會遍歷每個屬性,執行上面的脫敏策略。maven
會處理對象中各個字段上的脫敏註解信息。工具
遍歷每個對象,處理對象中各個字段上的脫敏註解信息。測試
做爲演示,集合中爲普通的字符串。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'}]}
issues
若是你對本項目有興趣,而且對代碼有必定追求,能夠申請加入本項目開發。
若是你善於寫文檔,或者願意補全測試案例,也很是歡迎加入。