SpringBoot敏感數(shù)據(jù)脫敏的處理方式
1. 使用注解 + Jackson序列化脫敏
通過(guò)自定義注解和Jackson的JsonSerializer實(shí)現(xiàn)數(shù)據(jù)脫敏,適合接口返回敏感數(shù)據(jù)時(shí)動(dòng)態(tài)處理。
實(shí)現(xiàn)步驟:
- 定義脫敏策略枚舉
public enum SensitiveStrategy {
// 不同脫敏策略
USERNAME(s -> s.replaceAll("(.).", "$1**")),
PHONE(s -> s.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2")),
ID_CARD(s -> s.replaceAll("(\\d{4})\\d{10}(\\d{4})", "$1****$2"));
private final Function<String, String> desensitizer;
SensitiveStrategy(Function<String, String> desensitizer) {
this.desensitizer = desensitizer;
}
public Function<String, String> getDesensitizer() {
return desensitizer;
}
}
- 自定義脫敏注解
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@JsonSerialize(using = SensitiveSerialize.class)
public @interface Sensitive {
SensitiveStrategy strategy();
}
- 自定義序列化器
public class SensitiveSerialize extends JsonSerializer<String> {
private SensitiveStrategy strategy;
public SensitiveSerialize(SensitiveStrategy strategy) {
this.strategy = strategy;
}
@Override
public void serialize(String value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeString(strategy.getDesensitizer().apply(value));
}
}
- 在DTO字段上使用注解
public class UserDTO {
@Sensitive(strategy = SensitiveStrategy.PHONE)
private String phone;
@Sensitive(strategy = SensitiveStrategy.ID_CARD)
private String idCard;
}
2. 日志脫敏處理
使用日志框架(如Logback或Log4j2)的替換規(guī)則,避免敏感信息寫入日志。
Logback配置示例(通過(guò)正則替換):
<configuration>
<conversionRule conversionWord="msg" converterClass="com.example.LogMaskConverter"/>
</configuration>
自定義轉(zhuǎn)換器:
public class LogMaskConverter extends ClassicConverter {
@Override
public String convert(ILoggingEvent event) {
return event.getMessage()
.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2") // 手機(jī)號(hào)
.replaceAll("(\\d{4})\\d{10}(\\d{4})", "$1****$2"); // 身份證
}
}
3. 數(shù)據(jù)庫(kù)加密存儲(chǔ)
使用加密工具(如Jasypt)對(duì)敏感字段進(jìn)行加密存儲(chǔ)。
實(shí)現(xiàn)步驟:
- 添加依賴
<dependency>
<groupId>com.github.ulisesbocchio</groupId>
<artifactId>jasypt-spring-boot-starter</artifactId>
<version>3.0.5</version>
</dependency>
- 配置加密密鑰
jasypt.encryptor.password=your_secret_key
- 在實(shí)體類中使用加密注解
public class User {
@Encrypted
private String phone;
@Encrypted
private String idCard;
}
4. 其他注意事項(xiàng)
- 全局性處理:結(jié)合AOP對(duì)所有Controller返回結(jié)果進(jìn)行統(tǒng)一脫敏。
- 深度脫敏:處理嵌套對(duì)象(如集合、Map中的敏感數(shù)據(jù))。
- 性能優(yōu)化:避免在高頻接口中使用復(fù)雜正則匹配。
- 測(cè)試驗(yàn)證:確保脫敏后的數(shù)據(jù)不可逆且符合業(yè)務(wù)需求。
總結(jié)
根據(jù)場(chǎng)景選擇合適方案:
- 接口脫敏:使用Jackson自定義序列化。
- 日志安全:配置日志替換規(guī)則。
- 存儲(chǔ)安全:數(shù)據(jù)庫(kù)字段加密。
- 傳輸安全:?jiǎn)⒂肏TTPS + 數(shù)據(jù)加密傳輸。
通過(guò)組合以上方法,可系統(tǒng)性地保護(hù)敏感數(shù)據(jù),滿足GDPR等數(shù)據(jù)安全法規(guī)要求。
以上就是SpringBoot敏感數(shù)據(jù)脫敏的處理方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot數(shù)據(jù)脫敏處理的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
- SpringBoot中實(shí)現(xiàn)數(shù)據(jù)脫敏的六種常用方案
- 使用SpringBoot整合Sharding Sphere實(shí)現(xiàn)數(shù)據(jù)脫敏的示例
- SpringBoot中實(shí)現(xiàn)數(shù)據(jù)脫敏處理的方法詳解
- SpringBoot中的數(shù)據(jù)脫敏處理詳解
- SpringBoot數(shù)據(jù)脫敏的實(shí)現(xiàn)示例
- 淺析如何在SpringBoot中實(shí)現(xiàn)數(shù)據(jù)脫敏
- SpringBoot動(dòng)態(tài)實(shí)現(xiàn)數(shù)據(jù)脫敏的實(shí)戰(zhàn)指南
相關(guān)文章
JAVASE系統(tǒng)實(shí)現(xiàn)抽卡功能
這篇文章主要為大家詳細(xì)介紹了JAVASE系統(tǒng)實(shí)現(xiàn)抽卡功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-11-11
springBoot server.port=-1的含義說(shuō)明
這篇文章主要介紹了springBoot server.port=-1的含義說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Java?synchornized與ReentrantLock處理并發(fā)出現(xiàn)的錯(cuò)誤
synchronized機(jī)制提供了對(duì)每個(gè)對(duì)象相關(guān)的隱式監(jiān)視器鎖,并強(qiáng)制所有鎖的獲取和釋放都必須在同一個(gè)塊結(jié)構(gòu)中。當(dāng)獲取了多個(gè)鎖時(shí),必須以相反的順序釋放。即synchronized對(duì)于鎖的釋放是隱式的2023-01-01
Spring MVC的國(guó)際化實(shí)現(xiàn)代碼
本篇文章主要介紹了Spring MVC的國(guó)際化實(shí)現(xiàn)代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08

