介紹
我們可以通過實現(xiàn)自定義日志附加程序并使用正則表達式來識別和屏蔽敏感信息,從而屏蔽 Spring Boot 應(yīng)用程序的 log4j 日志中的敏感數(shù)據(jù)。以下是具體方法實戰(zhàn):
基于 Spring Boot + MyBatis Plus + Vue & Element 實現(xiàn)的后臺管理系統(tǒng) + 用戶小程序,支持 RBAC 動態(tài)權(quán)限、多租戶、數(shù)據(jù)權(quán)限、工作流、三方登錄、支付、短信、商城等功能
- 項目地址:https://github.com/YunaiV/ruoyi-vue-pro
- 視頻教程:https://doc.iocoder.cn/video/
第1步:創(chuàng)建自定義日志 Appender
創(chuàng)建一個擴展 log4j 提供的 AppenderSkeleton
的類。這個自定義appender將在日志消息寫入日志之前攔截它們,并應(yīng)用必要的屏蔽。
importorg.apache.log4j.AppenderSkeleton;
importorg.apache.log4j.spi.LoggingEvent;
publicclassMaskingAppenderextendsAppenderSkeleton{
@Override
protectedvoidappend(LoggingEventloggingEvent){
Stringmessage=loggingEvent.getMessage().toString();
StringmaskedMessage=maskSensitiveData(message);
loggingEvent.setMessage(maskedMessage);
super.append(loggingEvent);
}
@Override
publicvoidclose(){
//Cleanupresources,ifany
}
@Override
publicbooleanrequiresLayout(){
returnfalse;
}
privateStringmaskSensitiveData(Stringmessage){
//Implementyourlogictomasksensitivedatausingregularexpressions
//Fordemonstrationpurposes,let'sassumewewanttomaskcreditcardnumbers
returnmessage.replaceAll("\d{4}-\d{4}-\d{4}-\d{4}","****-****-****-****");
}
}
log4j中的一個appender負責將日志消息寫入各種輸出中。通過擴展AppenderSkeleton
類,我們創(chuàng)建了一個自定義appender,它可以在將日志消息寫入日志之前對其進行修改。
正則表達式(regex)是用于模式匹配和操作字符串的強大模式。在maskSensitiveData()
方法中,我們使用regex來識別和替換敏感數(shù)據(jù)。在示例中,我們使用模式\d{4}-\d{4}-\d{4}-\d{4}
匹配格式為“xxxx-xxxx
”的信用卡號,并將其替換為“-
”。
append()方法
log4j在準備添加日志消息時調(diào)用此方法。在MaskingAppender
類中,我們覆蓋這個方法來攔截日志消息,使用maskSensitiveData()
方法對敏感數(shù)據(jù)應(yīng)用masking,然后將修改后的消息傳遞給超類的append()
方法。
基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 實現(xiàn)的后臺管理系統(tǒng) + 用戶小程序,支持 RBAC 動態(tài)權(quán)限、多租戶、數(shù)據(jù)權(quán)限、工作流、三方登錄、支付、短信、商城等功能
第2步:配置Log4j
在 Spring Boot 應(yīng)用程序的配置文件中,我們需要配置 log4j 以使用自定義 Appender。我們還需要根據(jù)您的要求指定日志級別和其他設(shè)置。這是使用 application.properties 配置 log4j 的示例:
#Log4jconfiguration
log4j.rootLogger=INFO,maskedAppender
log4j.appender.maskedAppender=com.example.MaskingAppender
log4j.appender.maskedAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.maskedAppender.layout.ConversionPattern=%d[%t]%-5p%c-%m%n
第3步:包含 Log4j
確保在Spring Boot應(yīng)用的構(gòu)建文件中有必要的log4j依賴項(例如,Maven的pom.xml):
log4j
log4j
1.2.17
第4步::測試日志屏蔽
現(xiàn)在,當我們在 Spring Boot 應(yīng)用程序中使用 log4j 記錄消息時,敏感數(shù)據(jù)將被自動屏蔽。例如:
importorg.apache.log4j.Logger;
publicclassSomeService{
privatestaticfinalLoggerlogger=Logger.getLogger(SomeService.class);
publicvoidprocessSensitiveData(Stringdata){
logger.info("Processingsensitivedata:"+data);//Sensitivedatawillbemaskedinthelogs
}
}
在應(yīng)用程序代碼中,可以使用log4j提供的Logger來記錄消息。在本例中,我們使用logger.info()
記錄一條消息。包含敏感數(shù)據(jù)的日志消息作為字符串連接傳遞。MaskingAppender攔截此消息并在將其寫入日志之前應(yīng)用掩碼。
在上面的例子中,如果數(shù)據(jù)參數(shù)中包含類似“1234—5678—9012—3456
”的信用卡號,那么它將在日志輸出中被屏蔽為“正在處理敏感數(shù)據(jù):——
”
結(jié)論
通過遵循這些步驟和概念,可以有效地屏蔽Spring Boot應(yīng)用程序log4j日志中的敏感數(shù)據(jù)。請記住根據(jù)我們的特定需求和敏感數(shù)據(jù)模式調(diào)整maskSensitiveData()
方法中的掩碼邏輯。
-
字符串
+關(guān)注
關(guān)注
1文章
589瀏覽量
20951 -
應(yīng)用程序
+關(guān)注
關(guān)注
38文章
3316瀏覽量
58516 -
掩碼
+關(guān)注
關(guān)注
0文章
3瀏覽量
1273
原文標題:保護 Log4j 日志中的敏感數(shù)據(jù),兩步搞定!
文章出處:【微信號:芋道源碼,微信公眾號:芋道源碼】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
相關(guān)推薦
slf4j打印日志必須的三個依賴包相關(guān)資料推薦
STM32Cube工具的log4j漏洞CVE-2021-44228和CVE-2021-45046有何影響?
java 日志框架Spring Boot分析
logback異常輸出詳細信息(調(diào)用堆棧)分析
使用IBM Cloud超級保護加密服務(wù)保護敏感數(shù)據(jù)
Java日志框架中的王者是誰
使用Keysight免費評估Log4j/Log4Shell零日漏洞
如何復現(xiàn)Log4j2漏洞
Spring Boot的日志框架使用

基于Rust的Log日志庫介紹
Log4cpp優(yōu)勢及優(yōu)點

評論