考虑在配置中定义类型为“ org.springframework.security.authentication.AuthenticationManager”的bean

时间:2018-09-09 10:54:34

标签: spring spring-boot spring-security

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

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

2 个答案:

答案 0 :(得分:16)

似乎这是Spring Boot 2.0引入的“重大更改”之一。我相信您的案件已在Spring Boot 2.0 Migration Guide中进行了描述。

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

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

此外,在您的ResourceServerConfig中,无需向AuthenticationManager注入@Autowired实例,您可以使用authenticationManagerBean()方法,即:

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

答案 1 :(得分:0)

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;