spring 考虑在您的配置中定义一个类型为 'org.springframework.security.authentication.AuthenticationManager' 的 bean
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52243774/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration
提问by Jeff Cook
I followed few suggestions mentioned here, but it didn't work for me. Hence, putting the question here
我遵循了这里提到的一些建议,但它对我不起作用。因此,把问题放在这里
- How To Inject AuthenticationManager using Java Configuration in a Custom Filter
- Spring required a bean of type 'AuthenticationManager'
Could anyone please guide me what's the issue and how to fixed that ?
任何人都可以请指导我是什么问题以及如何解决这个问题?
Error:
错误:
***************************
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);
}
}
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.
来自https://github.com/TechPrimers/spring-security-oauth-mysql-example的代码参考,仅将 Spring Boot 父版本更新为2.0.4.RELEASE,事情开始破裂。
回答by Poger
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.
这似乎是 Spring Boot 2.0 引入的“重大变化”之一。我相信您的情况在Spring Boot 2.0 Migration Guide 中有所描述。
In your WebSecurityConfigurerAdapterclass you need to override authenticationManagerBeanmethod and annotate it with @Bean, i.e.:
在您的WebSecurityConfigurerAdapter课程中,您需要覆盖authenticationManagerBean方法并用 注释它@Bean,即:
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
Moreover, in your WebSecurityConfigurerAdapterinstead of injecting the AuthenticationManagerinstance with @Autowiredyou can just use the authenticationManagerBean()method, i.e.:
此外,在您WebSecurityConfigurerAdapter而不是注入AuthenticationManager实例时,@Autowired您可以使用该authenticationManagerBean()方法,即:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception
{
auth.parentAuthenticationManager(authenticationManagerBean());
.userDetailsService(customUserDetailsService);
}
回答by Hilal Aissani
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;

