You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级spring-security-config后@Bean出现NoSuchMethodError求助

问题分析与解决方案

错误原因

你遇到的NoSuchMethodError是Spring Security模块版本不一致导致的:

  • 仅升级了spring-security-config到5.7.10,但Spring Boot 2.7.15默认依赖的其他Spring Security模块(比如spring-security-webserver)仍停留在5.7.5版本。
  • 5.7.10的spring-security-config中调用了ServerWebExchangeMatchers.pathMatchers(PathPattern[])方法,但该方法是在更高版本的spring-security-webserver中新增的,旧版本5.7.5没有这个方法,因此运行时抛出找不到方法的错误。

解决方案

方案1:统一升级所有Spring Security模块版本

在Maven的dependencyManagement中引入Spring Security的BOM,统一指定所有Spring Security模块的版本为5.7.10,避免单个模块版本不一致:

<dependencyManagement>
    <dependencies>
        <!-- 统一Spring Security版本 -->
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-bom</artifactId>
            <version>5.7.10</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
        <!-- 保留原Spring Boot BOM -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.7.15</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

方案2:升级Spring Boot到对应兼容的版本

Spring Boot的spring-boot-starter-security会管理Spring Security的版本,直接升级Spring Boot到2.7.x系列的最新版本(比如2.7.18),该版本默认依赖的Spring Security版本已经修复了CVE漏洞,且所有模块版本保持一致:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.18</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

验证步骤

  1. 执行mvn dependency:tree命令,检查所有Spring Security相关模块的版本是否统一为5.7.10(方案1)或Spring Boot对应版本的Security版本(方案2)。
  2. 清理Maven缓存(mvn clean install -U),重新构建项目后启动测试。

内容的提问来源于stack exchange,提问作者Jason Chan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 00:33:15