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ī)則,避免敏感信息寫(xiě)入日志。
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)文章!
相關(guān)文章
詳解Spring Boot中使用@Scheduled創(chuàng)建定時(shí)任務(wù)
本篇文章中主要介紹了Spring Boot中使用@Scheduled創(chuàng)建定時(shí)任務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-03-03IDEA連接遠(yuǎn)程服務(wù)器簡(jiǎn)化部署流程
筆者每次上線部署應(yīng)用,都要使用第三方的客戶端連接工具,比如?Xshell,FinalShell,Terminus?等,基本的流程步驟及其繁瑣,基于這個(gè)原因,筆者今天探索通過(guò)?IDEA?連接遠(yuǎn)程服務(wù)器并上傳文件,減少繁瑣的部署步驟,需要的朋友可以參考下2024-01-01解決Maven項(xiàng)目pom.xml文件Ignored的問(wèn)題
在Maven項(xiàng)目中,若不慎刪除了.iml文件,可能會(huì)導(dǎo)致pom.xml文件顯示為Ignored狀態(tài),影響項(xiàng)目構(gòu)建,解決方法是通過(guò)IDEA的設(shè)置取消Ignored Files中對(duì)應(yīng)文件的忽略,再刷新Maven項(xiàng)目即可恢復(fù),此操作可有效解決pom.xml文件被誤忽略的問(wèn)題,保證項(xiàng)目正常構(gòu)建和運(yùn)行2024-09-09springboot配置請(qǐng)求超時(shí)時(shí)間(Http會(huì)話和接口訪問(wèn))
本文主要介紹了springboot配置請(qǐng)求超時(shí)時(shí)間,包含Http會(huì)話和接口訪問(wèn)兩種,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07Java實(shí)戰(zhàn)項(xiàng)目之校園跑腿管理系統(tǒng)的實(shí)現(xiàn)
只有理論是不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+Springboot+vue+maven+elementui+mysql實(shí)現(xiàn)一個(gè)校園跑腿管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2022-01-01Java高效實(shí)現(xiàn)電商產(chǎn)品排序?qū)崙?zhàn)
這篇文章主要為大家介紹了Java高效實(shí)現(xiàn)電商產(chǎn)品排序?qū)崙?zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Java使用Hutool執(zhí)行日期的加法和減法操作方法
使用Hutool進(jìn)行日期的加法和減法操作,可以使用`DateUtil.offsetXXX()`方法來(lái)實(shí)現(xiàn),這些方法會(huì)返回一個(gè)新的日期,而不是在原日期上進(jìn)行修改,本文給大家介紹Java使用Hutool執(zhí)行日期的加法和減法操作方法,感興趣的朋友一起看看吧2023-11-11