考虑在配置中定义类型为"org.springframework.security.authentication.AuthenticationManager"的bean [英] Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration

查看:2070
本文介绍了考虑在配置中定义类型为"org.springframework.security.authentication.AuthenticationManager"的bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了这里提到的一些建议,但是对我来说不起作用.因此,将问题放在这里

I followed few suggestions mentioned here, but it didn't work for me. Hence, putting the question here

  1. 如何注入在自定义过滤器中使用Java配置的AuthenticationManager
  2. Spring需要类型为"AuthenticationManager"的bean
  1. How To Inject AuthenticationManager using Java Configuration in a Custom Filter
  2. Spring required a bean of type 'AuthenticationManager'

任何人都可以指导我解决什么问题以及如何解决该问题吗?

Could anyone please guide me what's the issue and how to fixed that ?

错误:

***************************
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父版本更新为2.0.4.RELEASE,事情开始崩溃.

The code reference taken from https://github.com/TechPrimers/spring-security-oauth-mysql-example, only updated Spring Boot Parent Version to 2.0.4.RELEASE, things started breaking.

推荐答案

似乎是Spring Boot 2.0引入的重大更改"之一.我相信您的案件在 Spring Boot 2.0迁移指南.

It seems like it's one of the "breaking changes" Spring Boot 2.0 introduced. I believe that your case is described in Spring Boot 2.0 Migration Guide.

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

In your WebSecurityConfigurerAdapter class you need to override authenticationManagerBean method and annotate it with @Bean, i.e.:

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

此外,在您的WebSecurityConfigurerAdapter中,您无需使用@Autowired注入AuthenticationManager实例,而只需使用authenticationManagerBean()方法,即:

Moreover, in your WebSecurityConfigurerAdapter instead of injecting the AuthenticationManager instance with @Autowired you can just use the authenticationManagerBean() method, i.e.:

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

这篇关于考虑在配置中定义类型为"org.springframework.security.authentication.AuthenticationManager"的bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆