WEB应用Log4j1.x升级到log4j2.17.1
WEB应用log4j1.x升级到log4j2.17.1
升级背景:引用apache log4j2官网描述,有严重的漏洞;加上公司维护的WEB应用使用了非常落后的版本为log4j-1.2.16和log4j1.x生命周期已经于2015年结束。注意log4j1.x与log4j2.x是两个不兼容的版本。
Apache Log4j2 versions 2.0-beta7 through 2.17.0 (excluding security fix releases 2.3.2 and 2.12.4) are vulnerable to a remote code execution (RCE) attack where an attacker with permission to modify the logging configuration file can construct a malicious configuration using a JDBC Appender with a data source referencing a JNDI URI which can execute remote code. This issue is fixed by limiting JNDI data source names to the java protocol in Log4j2 versions 2.17.1, 2.12.4, and 2.3.2.
Mitigation
Upgrade to Log4j 2.3.2 (for Java 6), 2.12.4 (for Java 7), or 2.17.1 (for Java 8 and later)
环境条件
log4j2.17.1基于JDK1.8编译,所以必须将log4j的应用程序的运行JDK升级到1.8或更高版本
删除旧版本JAR包
删除WEB根目录的WEB-INF/lib旧版本log4j1.x相关的jar包:
- commons-logging-1.1.jar
- log4j-1.2.16.jar
- slf4j-api-*.jar
- slf4j-log4j12-1.5.0.jar
- slf4j-simple-1.5.2.jar
删除log4j1.x的日志配置文件
删除WEB根目录的WEB-INF/log4j.xml
删除log4j1.x的启动配置或代码
公司WEB项目利用Spring的Log4jConfigListener启动配置,所以编辑web.xml并删除以下启动配置:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
新增log4j2的jar包依赖
WEB-INF/lib新增以下jar包:
- commons-logging-1.2.jar
- log4j-1.2-api-2.17.1.jar
- log4j-api-2.17.1.jar
- log4j-core-2.17.1.jar
- log4j-jcl-2.17.1.jar
- log4j-jul-2.17.1.jar
- log4j-slf4j-impl-2.17.1.jar
- log4j-web-2.17.1.jar
- slf4j-api-1.7.29.jar
新增log4j2的日志配置文件
WEB-INF下新增llog4j2.xml日志配置文件
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger:%L - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="STDOUT"/>
</Root>
<Logger name="freemarker.core" level="debug" additivity="false">
<AppenderRef ref="STDOUT"/>
</Logger>
</Loggers>
</Configuration>
新增log4j2的web启动配置
考虑公司的WEB项目比较旧,甚至存在Servlet2.5的应用,所以关闭log4j2-web.jar的自动初始化Log4jServletContextListener与Log4jServletFilter(需要Servelt3.0+)功能,统一改为人工配置。所以编辑web.xml配置文件,并新增以下启动配置:
<context-param>
<param-name>isLog4jAutoInitializationDisabled</param-name>
<param-value>true</param-value>
</context-param>
<listener>
<listener-class>org.apache.logging.log4j.web.Log4jServletContextListener</listener-class>
</listener>
<filter>
<filter-name>log4jServletFilter</filter-name>
<filter-class>org.apache.logging.log4j.web.Log4jServletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>log4jServletFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>FORWARD</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>ERROR</dispatcher>
</filter-mapping>
如果需要重定位日志配置文件位置,请设置log4j2的日志文件路径参数,否认为在默认路径WEB-INF下。
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>file:///D:/conf/myLogging.xml</param-value>
</context-param>
《一线大厂前端面试题+开发学习笔记+最新架构讲解视频+事站项目讲义》
【https://docs.qq.com/doc/DQVJzbnRIWWNtcVR4】完整内容开源分享