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

Spring Boot 1.5.8迁移至2.7.17遇类缺失等问题求助

Spring Boot 1.5.8 迁移到2.7.17类缺失问题解决方案

核心问题分析

你遇到的两个类缺失问题,都是Spring Boot/Spring Security版本升级后的API变更导致的,回退autoconfigure版本会引发更严重的依赖冲突,必须完全统一到2.7.17版本依赖。


1. 解决org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration找不到的问题

在Spring Boot 2.x版本中,该类的包路径已变更:

  • 旧路径:org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration
  • 新路径:org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration

修复步骤:

  • 检查代码中是否有@Import(SecurityAutoConfiguration.class)注解,替换为新路径
  • 检查application.properties/yml中的spring.autoconfigure.exclude配置,将旧类路径替换为新路径

2. 解决org.springframework.security.config.annotation.authentication.configurers.GlobalAuthenticationConfigurerAdapter找不到的问题

该类在Spring Security 5.x(对应Spring Boot 2.x)中已被移除,替代方案如下:

方案1:使用AuthenticationManagerBuilder直接配置

在你的认证配置类中,通过@Autowired注入AuthenticationManagerBuilder并配置:

@Configuration
public class CustomAuthConfig {
    @Autowired
    public void configureAuthentication(AuthenticationManagerBuilder auth) throws Exception {
        // 原GlobalAuthenticationConfigurerAdapter中的逻辑迁移到这里
        // 示例:auth.userDetailsService(customUserDetailsService).passwordEncoder(passwordEncoder());
    }
}

方案2:实现AuthenticationConfigurer接口

如果需要更灵活的配置,可以直接实现接口:

@Configuration
public class CustomAuthConfig implements AuthenticationConfigurer<AuthenticationManagerBuilder, Object> {
    @Override
    public void init(AuthenticationManagerBuilder builder) throws Exception {
        // 初始化逻辑
    }

    @Override
    public void configure(AuthenticationManagerBuilder builder) throws Exception {
        // 认证配置逻辑
    }
}

3. 强制统一所有Spring Boot依赖版本

确保pom.xml中使用Spring Boot的父依赖来管理所有相关组件版本,避免手动指定版本导致的冲突:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.7.17</version>
    <relativePath/>
</parent>

所有spring-boot-starter-*依赖无需指定版本,由父依赖统一管控。

4. 清理依赖冲突

运行以下命令查看依赖树,找出并排除旧版本的冲突依赖:

mvn dependency:tree

如果发现有第三方依赖引入了1.5.x版本的spring-boot-autoconfigure或Spring Security组件,在对应依赖中添加排除规则:

<dependency>
    <groupId>第三方依赖groupId</groupId>
    <artifactId>第三方依赖artifactId</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </exclusion>
        <exclusion>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </exclusion>
    </exclusions>
</dependency>

内容的提问来源于stack exchange,提问作者John Elesho

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 02:38:25