問題
為了保證使用者的資訊安全,敏感資訊需要脫敏。
項目開發過程中,每次處理敏感資訊的日志問題感覺很麻煩,大部分都是用工具類單獨處理,不利于以後統一管理,很不優雅。
于是,就寫了一個基于 java 注解的日志脫敏工具。
github sensitive
項目介紹
日志脫敏是常見的安全需求。普通的基于工具類方法的方式,對代碼的入侵性太強。編寫起來又特别麻煩。
本項目提供基于注解的方式,并且内置了常見的脫敏方式,便于開發。
使用者也可以基于自己的實際需要,自定義注解。
變更日志
日志脫敏
為了金融交易的安全性,國家強制規定對于以下資訊是要日志脫敏的:
- 使用者名
- 手機号
- 郵箱
- 銀行卡号
- 密碼
持久化加密
存儲的時候上面的資訊都需要加密,密碼為不可逆加密,其他為可逆加密。
類似的功能有很多。不在本系統的解決範圍内。
特性
- 基于注解的日志脫敏
- 可以自定義政策實作,政策生效條件
- 常見的脫敏内置方案
- 支援 jdk1.7+
快速開始
maven 導入
```<dependency>
<groupId>com.github.houbb</groupId>
<artifactId>sensitive</artifactId>
<version>0.0.1</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()
}
- 測試
@Test
public void UserSensitiveTest() {
User user = buildUser();
System.out.println("脫敏前原始: " + user);
User sensitiveUser = SensitiveUtil.desCopy(user);
System.out.println("脫敏對象: " + sensitiveUser);
System.out.println("脫敏後原始: " + user);
}
private User buildUser() {
User user = new User();
user.setUsername("脫敏君");
user.setPassword("123456");
user.setEmail("[email protected]");
user.setIdCard("123456190001011234");
user.setPhone("18888888888");
return user;
}
- 輸出資訊如下
脫敏前原始: User{username=\'脫敏君\', idCard=\'123456190001011234\', password=\'1234567\', email=\'[email protected]\', phone=\'18888888888\'}
脫敏對象: User{username=\'脫*君\', idCard=\'123456**********34\', password=\'null\', email=\'123**@qq.com\', phone=\'188****8888\'}
脫敏後原始: User{username=\'脫敏君\', idCard=\'123456190001011234\', password=\'1234567\', email=\'[email protected]\', phone=\'18888888888\'}
我們可以直接利用
sensitiveUser
去列印日志資訊,而這個對象對于代碼其他流程不影響,我們依然可以使用原來的
user
對象。
自定義脫敏政策生效的場景
預設情況下,我們指定的場景都是生效的。
但是你可能需要有些情況下不進行脫敏,比如有些使用者密碼為 123456,你覺得這種使用者不脫敏也罷。
- UserPasswordCondition.java
@Sensitive(condition = ConditionFooPassword.class, strategy = StrategyPassword.class)
private String password;
其他保持不變,我們指定了一個 condition,實作如下:
- ConditionFooPassword.java
public class ConditionFooPassword implements ICondition {
@Override
public boolean valid(IContext context) {
try {
Field field = context.getCurrentField();
final Object currentObj = context.getCurrentObject();
final String password = (String) field.get(currentObj);
return !password.equals("123456");
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
也就是隻有當密碼不是 123456 時密碼脫敏政策才會生效。
針對單個字段
上面的例子是基于注解式的程式設計,如果你隻是單個字段。比如
- singleSensitiveTest
@Test
public void singleSensitiveTest() {
final String email = "[email protected]";
IStrategy strategy = new StrategyEmail();
final String emailSensitive = (String) strategy.des(email, null);
System.out.println("脫敏後的郵箱:" + emailSensitive);
}
- 日志資訊
脫敏後的郵箱:123***@qq.com
待優化的地方
全新對象建立
這種方式為了避免修改原始對象,建立了一個全新的對象,有點點浪費,可以優化。
其他方法
可以基于 log4j2/logback 等轉換器進行敏感資訊的脫敏,但是不具有不同的 log 架構的可移植性。
來源:https://segmentfault.com/a/1190000017742745