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

基于注解的Spring Security实现求助:WebSecurityConfigurerAdapter类找不到

解决Spring Security中WebSecurityConfigurerAdapter类找不到的问题

你遇到的核心问题是缺少必要的Spring Security依赖模块,同时依赖范围配置也存在不合理之处,导致运行时无法找到WebSecurityConfigurerAdapter类。下面是一步步的解决方案:

1. 修复Maven依赖配置

WebSecurityConfigurerAdapter类并不在spring-security-web模块中,它属于spring-security-config模块。另外你设置的<scope>provided</scope>也不合适——这个范围意味着依赖仅在编译/测试阶段可用,运行时由容器提供,但普通Web容器(比如Tomcat)不会自带Spring Security的jar包,所以需要调整依赖:

替换你当前的Spring Security依赖为以下内容:

<spring.version>4.2.2.RELEASE</spring.version>
<spring.security.version>4.0.1.RELEASE</spring.version>

<!-- Spring Security Web模块 -->
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-web</artifactId>
    <version>${spring.security.version}</version>
</dependency>

<!-- Spring Security Config模块(必须添加,包含WebSecurityConfigurerAdapter) -->
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-config</artifactId>
    <version>${spring.security.version}</version>
</dependency>

2. 确保配置类正确继承WebSecurityConfigurerAdapter

你的configureGlobal方法需要放在继承了WebSecurityConfigurerAdapter的配置类中,并且要加上@Configuration和@EnableWebSecurity注解开启Spring Security:

@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Autowired
    private CustomUserDetailsService customUserDetailsService;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth.parentAuthenticationManager(authenticationManager)
            .userDetailsService(customUserDetailsService);
    }

    // 可选:配置HTTP请求的权限规则
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .anyRequest().authenticated() // 所有请求都需要认证
                .and()
            .formLogin(); // 启用默认表单登录
    }
}

3. 额外排查步骤

  • 执行mvn clean install -U命令强制更新Maven依赖,确保spring-security-config的jar包已经正确下载到本地仓库。
  • 如果使用IDE(比如IntelliJ IDEA或Eclipse),刷新Maven项目(IntelliJ:右键项目 → Maven → Reload Project;Eclipse:右键项目 → Maven → Update Project),让IDE识别新增的依赖。
  • 检查本地Maven仓库路径下是否存在org/springframework/security/spring-security-config/4.0.1.RELEASE/spring-security-config-4.0.1.RELEASE.jar,如果不存在,手动下载或排查Maven镜像配置问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 08:16:07