Spring项目集成Spring Security时conversionServicePostProcessor创建失败
问题分析与解决方案
问题根源
报错java.lang.NoSuchMethodError: org.springframework.core.convert.converter.Converter.andThen(Lorg/springframework/core/convert/converter/Converter;)Lorg/springframework/core/convert/converter/Converter;的本质是Spring版本不兼容:
- 当前项目使用的
spring-core版本是5.2.20.RELEASE,而Converter.andThen()方法是Spring Framework 5.3版本才新增的API - 引入的
spring-boot-starter-security:2.7.7依赖的Spring Framework版本为5.3.27,它会调用这个5.3专属方法,但项目运行时加载的是5.2版本的spring-core,导致方法找不到 - 你之前尝试的
spring.main.allow-bean-definition-overriding=true是解决Bean定义重复覆盖的配置,与本次错误无关,因此无法解决问题
解决方案
以下两种方式任选其一即可:
方案1:统一升级Spring基础包版本
Spring Boot 2.7.7对应的Spring Framework版本是5.3.27.RELEASE,将所有org.springframework开头的依赖(如spring-core、spring-jdbc、spring-webmvc等)的版本全部修改为5.3.27.RELEASE,示例:
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.3.27.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.3.27.RELEASE</version> </dependency> <!-- 其余spring-*依赖按此方式修改版本 -->
方案2:降级Spring Boot Security Starter版本
Spring Boot 2.3.x系列与Spring Framework 5.2.x完全兼容,将spring-boot-starter-security的版本改为2.3.12.RELEASE:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> <version>2.3.12.RELEASE</version> </dependency>
额外优化建议
为避免后续版本冲突,建议使用Spring Boot父POM统一管理依赖版本,只需在POM中添加父节点,大部分Spring相关依赖可省略版本号:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.7</version> <relativePath/> </parent>
内容的提问来源于stack exchange,提问作者Tobia
相关产品推荐
相关产品推荐

