SpringBoot2.x漏洞將logback1.2.x 升級(jí)至1.3.x
場(chǎng)景
安全部門針對(duì)代碼進(jìn)行漏洞掃描時(shí),發(fā)現(xiàn) logback-core 和 logback-classic 都屬于 1.2.x 版本,這個(gè)版本存在 CVE 漏洞,并且建議升級(jí)到 1.3.x 版本。
問(wèn)題
將兩個(gè)包直接升級(jí)到 1.3.x 版本時(shí),Spring Boot Web 服務(wù)啟動(dòng)直接出現(xiàn)錯(cuò)誤
Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/impl/StaticLoggerBinder at org.springframework.boot.logging.logback.LogbackLoggingSystem.getLoggerContext(LogbackLoggingSystem.java:293) at org.springframework.boot.logging.logback.LogbackLoggingSystem.beforeInitialize(LogbackLoggingSystem.java:118) at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationStartingEvent(LoggingApplicationListener.java:238) at org.springframework.boot.context.logging.LoggingApplicationListener.onApplicationEvent(LoggingApplicationListener.java:220) at org.springframework.context.event.SimpleApplicationEventMulticaster.doInvokeListener(SimpleApplicationEventMulticaster.java:176)
原因
org.slf4j.impl.StaticLoggerBinder 這個(gè)類在 1.2.x 中由 logback 自行實(shí)現(xiàn),在1.3.x 版本中,這個(gè)類已經(jīng)消失,
所以直接升級(jí)logback的包看起來(lái)并不可行,因?yàn)閟pring boot的代碼中對(duì)實(shí)用到了1.2.x中的類。

解決
按照上面所說(shuō)的原因,直接升級(jí)spring boot到新版本就可以支持 logback 1.3.x ,
但是遺憾的是 spring boot 需要版本在 3.x.x 之上才支持 logback 1.3.x ,并且使用 spring boot 3 需要 jdk 17的支持,
這就麻煩了,如此大版本的升級(jí),對(duì)于系統(tǒng)的影響,可能無(wú)法預(yù)知,是否存在不升級(jí)版本還能兼容使用 logback 1.3.x 的方法。
最終方案
經(jīng)過(guò)嘗試,我使用了下面的步驟,在不升級(jí) spring boot 版本的情況下,成功使用 logback 1.3.x。
1. 單獨(dú)升級(jí)logback版本, pom.xml
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>jul-to-slf4j</artifactId>
<version>2.0.4</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.3.0</version>
</dependency>
2. 禁用spring boot自身對(duì)日志的適配
@SpringBootApplication
public class Application {
public static void main(String[] args) {
System.setProperty("org.springframework.boot.logging.LoggingSystem", "none");
SpringApplication.run(Application.class, args);
}
}
3. 在resources根目錄下增加logback.xml文件
這一步根據(jù)自己的需求配置饑渴。
<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
<property name="app" value="xxx"/>
<property name="filename" value="xxx"/>
<import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/>
<import class="ch.qos.logback.core.rolling.RollingFileAppender"/>
<import class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"/>
<import class="ch.qos.logback.core.ConsoleAppender"/>
<import class="ch.qos.logback.classic.AsyncAppender"/>
<import class="ch.qos.logback.classic.filter.ThresholdFilter"/>
<appender name="INFO" class="RollingFileAppender">
....
</appender>
<appender name="ERROR" class="RollingFileAppender">
.....
</appender>
<appender name="STDOUT" class="ConsoleAppender">
...
</appender>
....
<appender name="ASYNC_INFO" class="AsyncAppender">
....
</appender>
....
<appender name="ASYNC_ERROR" class="AsyncAppender">
....
</appender>
<logger name="sun.rmi" level="error"/>
<logger name="sun.net" level="error"/>
<logger name="javax.management" level="error"/>
<logger name="org.redisson" level="warn"/>
<logger name="com.zaxxer" level="warn"/>
<root level="INFO" additivity="false">
<appender-ref ref="STDOUT"/>
<appender-ref ref="ASYNC_INFO"/>
<appender-ref ref="ASYNC_ERROR"/>
</root>
</configuration>
4. 完畢
這時(shí)候啟動(dòng)項(xiàng)目即可正常運(yùn)行。
原因說(shuō)明:
slf4j-api 1.8之后的版本不再使用靜態(tài)綁定,意味著不再需要StaticLoggerBinder,使用的是ServiceLoader機(jī)制,
新版本的 logback 自然為了適配,刪除了 StaticLoggerBinder,使用 ServiceLoader 機(jī)制,
因此禁用了 spring boot 自身的日志適配,引入高版本logback后,logback自身通過(guò) ServiceLoader 即引入項(xiàng)目中正常使用
PS
本文參考了:
- springboot 版本1.5.4 版本低 logback 升級(jí)1.3以上項(xiàng)目啟動(dòng)沖突
- logback更新到1.3版本tomcat無(wú)法啟動(dòng)問(wèn)題解決
到此這篇關(guān)于SpringBoot2.x漏洞將logback1.2.x 升級(jí)至1.3.x的文章就介紹到這了,更多相關(guān)logback1.2.x 升級(jí)至1.3.x內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot動(dòng)態(tài)定時(shí)功能實(shí)現(xiàn)方案詳解
在SpringBoot項(xiàng)目中簡(jiǎn)單使用定時(shí)任務(wù),不過(guò)由于要借助cron表達(dá)式且都提前定義好放在配置文件里,不能在項(xiàng)目運(yùn)行中動(dòng)態(tài)修改任務(wù)執(zhí)行時(shí)間,實(shí)在不太靈活。現(xiàn)在我們就來(lái)實(shí)現(xiàn)可以動(dòng)態(tài)修改cron表達(dá)式的定時(shí)任務(wù),感興趣的可以了解一下2022-11-11
Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)詳解
這篇文章主要給大家介紹了關(guān)于Mybatis-plus自定義SQL注入器查詢@TableLogic邏輯刪除后的數(shù)據(jù)的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-03-03
詳解servlet調(diào)用的幾種簡(jiǎn)單方式總結(jié)
這篇文章主要介紹了詳解servlet調(diào)用的幾種簡(jiǎn)單方式總結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01
SpringSecurity request過(guò)濾問(wèn)題示例小結(jié)
這篇文章主要介紹了SpringSecurity request過(guò)濾問(wèn)題示例小結(jié),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-02-02

