java 日誌脫敏框架 sensitive-新版本0.0.2-深度拷貝,屬性為對象和集合的支持
阿新 • • 發佈:2019-01-11
ive print name 自己 null iss array 使用註解 util 項目介紹
日誌脫敏是常見的安全需求。普通的基於工具類方法的方式,對代碼的***性太強。編寫起來又特別麻煩。
本項目提供基於註解的方式,並且內置了常見的脫敏方式,便於開發。
用戶也可以基於自己的實際需要,自定義註解。
特性
-
基於註解的日誌脫敏
-
可以自定義策略實現,策略生效條件
-
常見的脫敏內置方案
- java 深拷貝,且原始對象不用實現任何接口。
快速開始
maven 導入
<dependency> <groupId>com.github.houbb</groupId> <artifactId>sensitive</artifactId> <version>0.0.2</version> </dependency>
定義對象
- 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 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=‘[email protected]‘, phone=‘18888888888‘}, userList=[User{username=‘脫敏君‘, idCard=‘123456190001011234‘, password=‘1234567‘, email=‘[email protected]‘, phone=‘18888888888‘}], userArray=[User{username=‘脫敏君‘, idCard=‘123456190001011234‘, password=‘1234567‘, email=‘[email protected]‘, 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=‘[email protected]‘, phone=‘18888888888‘}, userList=[User{username=‘脫敏君‘, idCard=‘123456190001011234‘, password=‘1234567‘, email=‘[email protected]‘, phone=‘18888888888‘}], userArray=[User{username=‘脫敏君‘, idCard=‘123456190001011234‘, password=‘1234567‘, email=‘[email protected]‘, phone=‘18888888888‘}]}
需求 & BUGS
issues
歡迎加入開發
如果你對本項目有興趣,並且對代碼有一定追求,可以申請加入本項目開發。
如果你善於寫文檔,或者願意補全測試案例,也非常歡迎加入。
java 日誌脫敏框架 sensitive-新版本0.0.2-深度拷貝,屬性為對象和集合的支持