解决Spring Security升级到5.5.7、5.6.4及以上启动报错出现版本不兼容的问题
1.背景
版本比对检测原理:检查当前系统中spring-security-web版本是否在漏洞版本范围内|版本比对检测结果:- spring-security-web
当前安装版本:5.2.1.RELEASE
需要升级到 5.5.7、5.6.4 及以上版本,因为pom中找不到直接引用的位置,所以加入以下依赖将spring-security-web版本强制升级到5.5.7
<!-- 修复spring-security-web版本漏洞 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.5.7</version>
</dependency>启动时报错,报错内容如下:
***************************
APPLICATION FAILED TO START
***************************Description:
An attempt was made to call a method that does not exist. The attempt was made from the following location:
org.springframework.security.web.util.matcher.OrRequestMatcher.<init>(OrRequestMatcher.java:43)
The following method did not exist:
org.springframework.util.Assert.noNullElements(Ljava/util/Collection;Ljava/lang/String;)V
The method's class, org.springframework.util.Assert, is available from the following locations:
jar:file:/C:/Users/sutpc/.m2/repository/org/springframework/spring-core/5.1.18.RELEASE/spring-core-5.1.18.RELEASE.jar!/org/springframework/util/Assert.class
It was loaded from the following location:
file:/C:/Users/sutpc/.m2/repository/org/springframework/spring-core/5.1.18.RELEASE/spring-core-5.1.18.RELEASE.jar
2.原因分析
可以发现spring包版本不兼容导致该问题
理论上是spring-security-web是在某一个jar引入
单独改了spring-security-web的版本
导致这个jar中的配套代码不兼容导致的问题
3.解决方式
将spring-boot-dependencies的2.1.17.RELEASE升级到2.2.2.RELEASE
<!-- SpringBoot的依赖配置-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<!-- <version>2.1.17.RELEASE</version>-->
<version>2.2.2.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
在pom的最后直接使用spring-security-web的5.5.7强制覆盖版本即可
<!-- 修复spring-security-web版本漏洞 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>5.5.7</version>
</dependency>4.解决思路
因为单独改了spring-security-web的版本
导致这个jar中的配套代码不兼容导致的问题
所以首要问题需要找到spring-security-web由哪个jar引入的
4.1查询Maven 项目查找 jar 包是由哪个依赖引入的
直接使用mvn dependency:tree可以查看项目完整的依赖树。
1.命令格式
mvn dependency:tree -Dverbose -Dincludes=要查询的内容(groupId:artifactId)
-dependency:tree:表示树状显示。-Dverbose:表示可以显示所有的引用,包括因为多次引用重复而忽略的。-Dincludes:可以制定查询条件
spring-security-web的groupId和artifactId为:
groupId: org.springframework.security artifactId: spring-security-web 所以命令为 mvn dependency:tree -Dverbose -Dincludes=org.springframework.security:spring-security-web
4.2在idea的Teminal中执行之后依赖层级如下图

4.3spring-security-web是由spring-boot-starter-security引入的
spring-security-web是由spring-boot-starter-security引入的
版本是2.1.17.RELEASE
搜spring-boot-starter-security发现又是使用的spring-boot-dependencies-2.1.17.RELEASE.pom的版本

4.4spring-boot-starter-security版本是继承spring-boot-dependencies的版本
在全局搜spring-boot-dependencies的版本,
发现果然是2.1.17.RELEASE,到此,所有的依赖层级都找到了,
那开始猜,是不是spring-boot-dependencies版本太低了,
spring-security-web的版本太高了导致的不兼容,
spring-security-web版本不能调低,只能升级spring-boot-dependencies的版本,
在maven仓库查找spring-boot-dependencies版本,
逐级测试,发现2.2.2.RELEASE可以支持,所以问题到此解决.

总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
IDEA运行Java项目报错java: 错误: 不支持发行版本 xx的解决方法
这篇文章主要介绍了IDEA运行Java项目报错java: 错误: 不支持发行版本 xx的解决方法,文中有详细的解决方案供大家参考,对大家解决问题有一定的帮助,需要的朋友可以参考下2025-04-04
springboot3 使用 jasypt 加密配置文件的使用步骤
在SpringBoot项目中,使用Jasypt加密配置文件可以有效保护敏感信息,首先,需添加Jasypt依赖并配置加密密码,可在application.properties或通过启动参数、环境变量设置,本文介绍了Jasypt的配置步骤及使用方法,帮助开发者保护应用配置信息2024-11-11
java.lang.UnsatisfiedLinkError: %1 不是有效的Win32应用程序错误解决
这篇文章主要给大家介绍了关于java.lang.UnsatisfiedLinkError: %1 不是有效的Win32应用程序错误的解决方法,文中介绍的非常详细,需要的朋友们可以参考学习,下面来一起看看吧。2017-03-03
面试官:java ThreadLocal真的会造成内存泄露吗
ThreadLocal,java面试过程中的“钉子户”,在网上也充斥着各种有关ThreadLocal内存泄露的问题,本文换个角度,先思考ThreadLocal体系中的ThreadLocalMap为什么要设计成弱引用2021-08-08


最新评论