考虑在你的配置中定义一个 'org.springframework.security.authentication.AuthenticationManager' 类型的 bean

新手上路,请多包涵

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

  1. 如何在自定义过滤器中使用 Java 配置注入 AuthenticationManager
  2. Spring 需要一个 ‘AuthenticationManager’ 类型的 bean

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

错误:

 ***************************
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);
    }
}

资源服务器配置.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 父版本更新为 2.0.4.RELEASE ,事情开始破裂。

原文由 Jeff Cook 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.1k
1 个回答

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

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

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

原文由 Poger 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题