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

Spring Security报错:找不到AuthenticationManager类型Bean 求解决

Fix: Missing AuthenticationManager Bean in Spring Authorization Server

Hey there, let's get that missing AuthenticationManager bean sorted out for your authorization server! The root issue here is that Spring Security doesn't expose the AuthenticationManager as an injectable bean by default—you need to explicitly configure it in your security setup.

Solution 1: Using WebSecurityConfigurerAdapter (Legacy Approach)

If you're using an older version of Spring Security (pre-5.7) or still relying on WebSecurityConfigurerAdapter, create a security configuration class that exposes the AuthenticationManager as a bean:

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    // Expose AuthenticationManager as a bean for dependency injection
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    // Configure your user authentication (example with in-memory users)
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
            .withUser("your-user")
            .password("{noop}your-password") // {noop} disables password encoding (only for testing!)
            .roles("USER");
    }
}

Once you add this class, your AuthServerConfig will be able to autowire the AuthenticationManager successfully.

If you're using Spring Security 5.7 or later (where WebSecurityConfigurerAdapter is deprecated), use the modern component-based configuration:

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Bean
    public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
        http
            .authorizeHttpRequests(auth -> auth
                .anyRequest().authenticated()
            )
            .formLogin(form -> form.permitAll()); // Adjust login settings as needed
        return http.build();
    }

    // Expose AuthenticationManager bean
    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
        return authConfig.getAuthenticationManager();
    }

    // Example: In-memory user details service
    @Bean
    public UserDetailsService userDetailsService() {
        UserDetails testUser = User.withUsername("test-user")
            .password("{noop}test-pass")
            .roles("USER")
            .build();
        return new InMemoryUserDetailsManager(testUser);
    }
}

Quick Checks to Ensure It Works

  • Make sure both your SecurityConfig and AuthServerConfig are in a package that's scanned by Spring (i.e., under your main application class's package or explicitly included in @ComponentScan).
  • Verify you have the necessary dependencies: If you're using Spring Boot, include spring-boot-starter-security in your pom.xml or build.gradle.

That should resolve the "could not be found" error for the AuthenticationManager bean!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 09:51:36