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

Spring Boot中WebMvcConfigurerAdapter类不存在错误求助

问题根源

Spring Boot 3.2.0-M1 依赖的 Spring Security 6.x 已经彻底移除了WebSecurityConfigurerAdapter类(该类在Spring Security 5.7版本就已标记废弃);同时Spring MVC 6.x也移除了早已废弃的WebMvcConfigurerAdapter类,这就是你报错的核心原因——旧的配置方式依赖了已被完全移除的API,和依赖包无关。

解决方案

1. 替换Spring Security配置方式

放弃继承WebSecurityConfigurerAdapter,改用Spring Security 6.x推荐的基于Bean的配置模式:

@Configuration
@EnableWebSecurity
public class AppConfig {

    // 配置HTTP安全规则
    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests(auth -> auth
                // 按需配置接口权限,示例:所有请求需认证
                .anyRequest().authenticated()
            )
            // 按需配置登录方式,示例:开启表单登录并允许所有人访问登录页
            .formLogin(form -> form.permitAll());
        
        return http.build();
    }

    // 配置用户信息服务(示例为内存用户,可替换为数据库实现)
    @Bean
    public UserDetailsService userDetailsService() {
        UserDetails demoUser = User.withDefaultPasswordEncoder()
            .username("demo")
            .password("123456")
            .roles("USER")
            .build();
        
        return new InMemoryUserDetailsManager(demoUser);
    }
}

2. 修正Spring MVC配置(如果有)

如果你的项目中存在继承WebMvcConfigurerAdapter的配置类,直接改为实现WebMvcConfigurer接口即可(接口方法均为默认方法,只需重写你需要的逻辑):

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    // 示例:自定义视图跳转
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }
}

3. 清理pom.xml依赖

确保只保留Spring Boot官方starter依赖,无需额外添加旧版Spring Security/MVC依赖:

<dependencies>
    <!-- Spring Security starter,自动引入所有必要依赖 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <!-- Spring Web starter(如果需要Web功能) -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

验证

删除旧的继承WebSecurityConfigurerAdapter和WebMvcConfigurerAdapter的代码,替换为上述配置后,重新启动项目即可解决报错。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 11:32:35