Maven 换源 & Mybatis 开启 Log4j 日志框架

2022-10-05 16:46:29 浏览数 (1)

本文主要讲解如何在 idea 开发环境下更换 maven 国内镜像源与 Mybatis 开启 Log4j 日志框架相关操作

Maven 换源

在 maven 中找到 conf 然后配置 settings.xml 即可

不要在 idea 默认的 maven 插件目录下修改配置,否则下次更新会重新覆盖,失效!!!

其他不再赘述,请读者自行查询

这里主要叙述下 idea 开发环境中 针对 maven 的配置项,开始前首先区分两个设置

  • 项目设置,只针对当前项目生效;
  • 开发环境默认设置,每次建立项目都从这里读取相关配置;

由于我们在本地配置好了 maven 仓储库,并不想使用默认指定的位置(一般都在 c 盘符下),所以需要在 idea 中更新下

1.项目设置

进入设置,找到 maven

2.默认设置

如果你不想每次新建项目都需要重新配置,最好执行以下操作

Mybatis 开启 Log4j 日志框架

ration. Cause: org.apache.ibatis.logging.LogException: Error setting Log implementation. Cause: java.lang.NoClassDefFoundError: org/apache/log4j/Priority

网络现在大多的参考教程配置项仍为 log4j 1.2.17 版本,单此版本的安全漏洞在 idea 中禁用,若读者不能自行找到更高或最新版本,请参考以下教程

maven 中央仓库搜索 log4j 结果

发现已经迁移位置,来到新位置

可以看到已经针对漏洞进行了修复

点击合适版本并选择对应引入方式,(这里以 maven 为例)加入到 pom 文件下即可

pom.xml

代码语言:javascript复制
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.19.0</version>
</dependency>

log4j.properties

代码语言:javascript复制
#声明日志的输出级别及输出方式
log4j.rootLogger=DEBUG,stdout
# MyBatis logging configuration...
# MyBatis 日志配置 (记得修改为你的项目对应文件)
log4j.logger.com.jason.jasonMapper=TRACE
# Console output...
#声明日志的输出位置在控制台输出
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#定义日志的打印格式  %t 表示线程名称  %5p表示输出日志级别   %n表示换行
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

mybatis-config.xml

代码语言:javascript复制
<settings>
    <!-- 开启日志 -->
    <setting name="logImpl" value="LOG4J"/>
</settings>

重新启动测试,成功

代码语言:javascript复制
"C:Program FilesJavajdk-17.0.2binjava.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:IDEIntelliJ IDEA 2021.2libidea_rt.jar=1831:C:IDEIntelliJ IDEA 2021.2bin" -Dfile.encoding=UTF-8 -classpath "C:IDEIntelliJ IDEA 2021.2libidea_rt.jar;C:IDEIntelliJ IDEA 2021.2pluginsjunitlibjunit5-rt.jar;C:IDEIntelliJ IDEA 2021.2pluginsjunitlibjunit-rt.jar;D:ProgrammeDiskjava-workspaceMybatistargetclasses;D:SoftwareDownloadingMavenmvn_repoorgmybatismybatis3.5.10mybatis-3.5.10.jar;D:SoftwareDownloadingMavenmvn_repomysqlmysql-connector-java8.0.30mysql-connector-java-8.0.30.jar;D:SoftwareDownloadingMavenmvn_repocomgoogleprotobufprotobuf-java3.19.4protobuf-java-3.19.4.jar;D:SoftwareDownloadingMavenmvn_repojunitjunit4.13.2junit-4.13.2.jar;D:SoftwareDownloadingMavenmvn_repoorghamcresthamcrest-core1.3hamcrest-core-1.3.jar;D:SoftwareDownloadingMavenmvn_repoorgapachelogginglog4jlog4j-core2.19.0log4j-core-2.19.0.jar;D:SoftwareDownloadingMavenmvn_repoorgapachelogginglog4jlog4j-api2.19.0log4j-api-2.19.0.jar;D:SoftwareDownloadingMavenmvn_repolog4jlog4j1.2.17log4j-1.2.17.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.jason.test.UserTest
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - Class not found: org.jboss.vfs.VFS
DEBUG [main] - JBoss 6 VFS API is not available in this environment.
DEBUG [main] - Class not found: org.jboss.vfs.VirtualFile
DEBUG [main] - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.
DEBUG [main] - Using VFS adapter org.apache.ibatis.io.DefaultVFS
DEBUG [main] - Find JAR URL: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo
DEBUG [main] - Not a JAR: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo
DEBUG [main] - Reader entry: User.class
DEBUG [main] - Listing file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo
DEBUG [main] - Find JAR URL: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo/User.class
DEBUG [main] - Not a JAR: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo/User.class
DEBUG [main] - Reader entry: x   = H
DEBUG [main] - Checking to see if class com.jason.pojo.User matches criteria [is assignable to Object]
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 1198817209.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@47747fb9]
DEBUG [main] - ==>  Preparing: insert into tb_user(name,sex,age,phone) values (?,?,?,?,?)
DEBUG [main] - ==> Parameters: ooo(String), ooo(String), ooo(String), ooo(String), ooo(String)
DEBUG [main] - <==    Updates: 1
成功插入 1 条数据
DEBUG [main] - Committing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@47747fb9]
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@47747fb9]
DEBUG [main] - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@47747fb9]
DEBUG [main] - Returned connection 1198817209 to pool.
DEBUG [Finalizer] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Logging initialized using 'class org.apache.ibatis.logging.log4j.Log4jImpl' adapter.
DEBUG [main] - Find JAR URL: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo
DEBUG [main] - Not a JAR: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo
DEBUG [main] - Reader entry: User.class
DEBUG [main] - Listing file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo
DEBUG [main] - Find JAR URL: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo/User.class
DEBUG [main] - Not a JAR: file:/D:/ProgrammeDisk/java-workspace/Mybatis/target/classes/com/jason/pojo/User.class
DEBUG [main] - Reader entry: x   = H
DEBUG [main] - Checking to see if class com.jason.pojo.User matches criteria [is assignable to Object]
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - PooledDataSource forcefully closed/removed all connections.
DEBUG [main] - Opening JDBC Connection
DEBUG [main] - Created connection 470132045.
DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1c05a54d]
DEBUG [main] - ==>  Preparing:  select * from tb_user where id=?
DEBUG [main] - ==> Parameters: 41(Integer)
DEBUG [main] - <==      Total: 1
User{id=41, name='jason', sex='男', age='00', phone='12345678901'}
DEBUG [main] - Resetting autocommit to true on JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1c05a54d]
DEBUG [main] - Closing JDBC Connection [com.mysql.cj.jdbc.ConnectionImpl@1c05a54d]
DEBUG [main] - Returned connection 470132045 to pool.

Process finished with exit code 0

参考资料:

  • Log4j vulnerability - Is Log4j 1.2.17 vulnerable (was unable to find any JNDI code in source)?
  • How to mitigate Apache Log4j Deserialization RCE (CVE-2019-17571)

0 人点赞