【问题标题】:Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration考虑在你的配置中定义一个 'org.springframework.security.authentication.AuthenticationManager' 类型的 bean
【发布时间】:2019-02-14 01:27:35
【问题描述】:

我遵循了这里提到的一些建议,但它对我不起作用。因此,把问题放在这里

  1. How To Inject AuthenticationManager using Java Configuration in a Custom Filter
  2. Spring required a bean of type 'AuthenticationManager'

谁能指导我这是什么问题以及如何解决?

错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field authenticationManager in com.techprimers.security.springsecurityauthserver.config.AuthorizationServerConfig required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.

AuthorizationServerConfig.java

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {

        security.tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()");
    }


    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                .inMemory()
                .withClient("ClientId")
                .secret("secret")
                .authorizedGrantTypes("authorization_code")
                .scopes("user_info")
                .autoApprove(true);
    }


    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

        endpoints.authenticationManager(authenticationManager);
    }
}

ResourceServerConfig.java

@EnableResourceServer
@Configuration
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;
    @Autowired
    private UserDetailsService customUserDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.requestMatchers()
                .antMatchers("/login", "/oauth/authorize")
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .permitAll();
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.parentAuthenticationManager(authenticationManager)
                .userDetailsService(customUserDetailsService);
    }
}

取自https://github.com/TechPrimers/spring-security-oauth-mysql-example的代码参考,仅将Spring Boot Parent版本更新为2.0.4.RELEASE,事情开始崩溃了。

【问题讨论】:

    标签: spring spring-boot spring-security


    【解决方案1】:

    这似乎是 Spring Boot 2.0 引入的“重大变化”之一。我相信Spring Boot 2.0 Migration Guide中描述了您的情况。

    在您的WebSecurityConfigurerAdapter 类中,您需要覆盖authenticationManagerBean 方法并使用@Bean 对其进行注释,即:

    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    

    此外,在您的WebSecurityConfigurerAdapter 中,您可以使用authenticationManagerBean() 方法,而不是使用@Autowired 注入AuthenticationManager 实例,即:

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception 
    {
        auth.parentAuthenticationManager(authenticationManagerBean())
            .userDetailsService(customUserDetailsService);
    }
    

    【讨论】:

    • 请从示例中删除 .parentAuthenticationManager(authenticationManagerBean()) 部分,因为它会导致错误! 我刚刚发现这是导致无限的原因递归最终导致StackOverflowException。每当使用不正确的密码调用 AuthenticationManager.authenticate() 时,就会发生无限递归。这会抛出BadCredentialsException,这会导致由于某种原因再次调用相同的方法。这很可能发生,因为 parentAuthenticationManagerAuthenticationManager 是同一个实例。
    【解决方案2】:
    just add this to the AuthenticationManagerBuilder
    
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
    
    and in your controller where you need to use it add this :
    
     @Autowired
        private AuthenticationManager authenticationManager;
    

    【讨论】:

      【解决方案3】:

      考虑在你的配置中定义一个“org.springframework.security.core.userdetails.UserDetails”类型的bean。

      【讨论】:

        猜你喜欢
        • 2018-06-22
        • 1970-01-01
        • 2020-08-01
        • 2018-12-21
        • 2019-05-10
        • 2019-09-04
        • 2020-05-23
        • 2019-02-05
        • 1970-01-01
        相关资源
        最近更新 更多