問題
為了保證用戶的信息安全,敏感信息需要脫敏。
項目開發過程中,每次處理敏感信息的日志問題感覺很麻煩,大部分都是用工具類單獨處理,不利於以后統一管理,很不優雅。
於是,就寫了一個基於 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("12345@qq.com");
user.setIdCard("123456190001011234");
user.setPhone("18888888888");
return user;
}
- 輸出信息如下
脫敏前原始: User{username='脫敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', phone='18888888888'}
脫敏對象: User{username='脫*君', idCard='123456**********34', password='null', email='123**@qq.com', phone='188****8888'}
脫敏后原始: User{username='脫敏君', idCard='123456190001011234', password='1234567', email='12345@qq.com', 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 = "123456@qq.com";
IStrategy strategy = new StrategyEmail();
final String emailSensitive = (String) strategy.des(email, null);
System.out.println("脫敏后的郵箱:" + emailSensitive);
}
- 日志信息
脫敏后的郵箱:123***@qq.com
待優化的地方
全新對象創建
這種方式為了避免修改原始對象,創建了一個全新的對象,有點點浪費,可以優化。
其他方法
可以基於 log4j2/logback 等轉換器進行敏感信息的脫敏,但是不具有不同的 log 框架的可移植性。
