当前位置: 首页 > 知识库问答 >
问题:

考虑在您的配置中定义一个类型为“org.springframework.security.authentication.身份验证管理器”的bean

吴欣然
2023-03-14

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

  1. 如何在自定义筛选器中使用Java配置注入身份验证管理器
  2. Spring需要一个类型为“身份验证管理器”的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);
    }
}

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。发布,事情开始崩溃。

共有3个答案

桓修能
2023-03-14

考虑定义一个类型为“org”的bean。springframework。安全果心用户详细信息。配置中的“用户详细信息”。

卫沈义
2023-03-14
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;
张丰
2023-03-14

这似乎是Spring Boot 2.0推出的“突破性变化”之一。我相信Spring Boot 2.0迁移指南中描述了您的情况。

websecurityConfigureAdapter类中,需要重写authenticationManagerBean方法,并用@Bean对其进行注释,即:

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

此外,在您的WebSecurityConfigrerAdapter中,您可以只使用验证ManagerBean()方法,即:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception 
{
    auth.parentAuthenticationManager(authenticationManagerBean())
        .userDetailsService(customUserDetailsService);
}
 类似资料: