SpringBoot敏感數(shù)據(jù)脫敏的處理方式
1. 使用注解 + Jackson序列化脫敏
通過自定義注解和Jackson的JsonSerializer實(shí)現(xiàn)數(shù)據(jù)脫敏,適合接口返回敏感數(shù)據(jù)時動態(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配置示例(通過正則替換):
<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ī)號
.replaceAll("(\\d{4})\\d{10}(\\d{4})", "$1****$2"); // 身份證
}
}
3. 數(shù)據(jù)庫加密存儲
使用加密工具(如Jasypt)對敏感字段進(jìn)行加密存儲。
實(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對所有Controller返回結(jié)果進(jìn)行統(tǒng)一脫敏。
- 深度脫敏:處理嵌套對象(如集合、Map中的敏感數(shù)據(jù))。
- 性能優(yōu)化:避免在高頻接口中使用復(fù)雜正則匹配。
- 測試驗(yàn)證:確保脫敏后的數(shù)據(jù)不可逆且符合業(yè)務(wù)需求。
總結(jié)
根據(jù)場景選擇合適方案:
- 接口脫敏:使用Jackson自定義序列化。
- 日志安全:配置日志替換規(guī)則。
- 存儲安全:數(shù)據(jù)庫字段加密。
- 傳輸安全:啟用HTTPS + 數(shù)據(jù)加密傳輸。
通過組合以上方法,可系統(tǒng)性地保護(hù)敏感數(shù)據(jù),滿足GDPR等數(shù)據(jù)安全法規(guī)要求。
以上就是SpringBoot敏感數(shù)據(jù)脫敏的處理方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot數(shù)據(jù)脫敏處理的資料請關(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動態(tài)實(shí)現(xiàn)數(shù)據(jù)脫敏的實(shí)戰(zhàn)指南
相關(guān)文章
springboot?整合表達(dá)式計(jì)算引擎?Aviator?使用示例詳解
本文詳細(xì)介紹了Google?Aviator?這款高性能、輕量級的?Java?表達(dá)式求值引擎,并通過詳細(xì)的代碼操作演示了相關(guān)API的使用以及如何在springboot項(xiàng)目中進(jìn)行集成,感興趣的朋友一起看看吧2024-08-08
springboot中ApplicationRunner執(zhí)行順序問題小結(jié)
SpringBoot中ApplicationRunner用于應(yīng)用啟動后執(zhí)行初始化任務(wù),通過@Order注解可控制多個Runner的執(zhí)行順序,數(shù)值越小越優(yōu)先,下面就一起來了解一下2025-06-06
Eclipse+Java+Swing實(shí)現(xiàn)學(xué)生成績管理系統(tǒng)的實(shí)例代碼
這篇文章主要介紹了Eclipse+Java+Swing實(shí)現(xiàn)學(xué)生成績管理系統(tǒng),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Java 中如何使用 JavaFx 庫標(biāo)注文本顏色
這篇文章主要介紹了在 Java 中用 JavaFx 庫標(biāo)注文本顏色,在本文中,我們將了解如何更改標(biāo)簽的文本顏色,并且我們還將看到一個必要的示例和適當(dāng)?shù)慕忉專员愀菀桌斫庠撝黝},需要的朋友可以參考下2023-05-05

