SpringBoot2.x漏洞將logback1.2.x 升級(jí)至1.3.x
場景
安全部門針對(duì)代碼進(jìn)行漏洞掃描時(shí),發(fā)現(xiàn) logback-core 和 logback-classic 都屬于 1.2.x 版本,這個(gè)版本存在 CVE 漏洞,并且建議升級(jí)到 1.3.x 版本。
問題
將兩個(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的包看起來并不可行,因?yàn)閟pring boot的代碼中對(duì)實(shí)用到了1.2.x中的類。

解決
按照上面所說的原因,直接升級(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)的影響,可能無法預(yù)知,是否存在不升級(jí)版本還能兼容使用 logback 1.3.x 的方法。
最終方案
經(jīng)過嘗試,我使用了下面的步驟,在不升級(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)行。
原因說明:
slf4j-api 1.8之后的版本不再使用靜態(tài)綁定,意味著不再需要StaticLoggerBinder,使用的是ServiceLoader機(jī)制,
新版本的 logback 自然為了適配,刪除了 StaticLoggerBinder,使用 ServiceLoader 機(jī)制,
因此禁用了 spring boot 自身的日志適配,引入高版本logback后,logback自身通過 ServiceLoader 即引入項(xiàng)目中正常使用
PS
本文參考了:
到此這篇關(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)文章
SpringCloud學(xué)習(xí)筆記之OpenFeign進(jìn)行服務(wù)調(diào)用
OpenFeign對(duì)feign進(jìn)行進(jìn)一步的封裝,添加了springmvc的一些功能,更加強(qiáng)大,下面這篇文章主要給大家介紹了關(guān)于SpringCloud學(xué)習(xí)筆記之OpenFeign進(jìn)行服務(wù)調(diào)用的相關(guān)資料,需要的朋友可以參考下2022-01-01
spring boot 不連接數(shù)據(jù)庫啟動(dòng)的解決
這篇文章主要介紹了spring boot 不連接數(shù)據(jù)庫啟動(dòng)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
Spring Boot 項(xiàng)目創(chuàng)建的詳細(xì)步驟(圖文)
這篇文章主要介紹了Spring Boot 項(xiàng)目創(chuàng)建的詳細(xì)步驟(圖文),這里我們有兩種創(chuàng)建Spring Boot項(xiàng)目的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
SpringBoot ApplicationContextAware拓展接口使用詳解
當(dāng)一個(gè)類實(shí)現(xiàn)了這個(gè)接口(ApplicationContextAware)之后,這個(gè)類就可以方便獲得ApplicationContext中的所有bean。換句話說,就是這個(gè)類可以直接獲取spring配置文件中,所有有引用到的bean對(duì)象2023-04-04
SpringBoot實(shí)現(xiàn)API接口多版本支持的示例代碼
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)API接口多版本支持的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10

