在配置spring cloud security的过程中出现如下异常信息导致无法启动项目
Field authenticationManager in com.clark.online.edu.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.
看错误信息就很明白了,就是需要authenticationManager,但是没有在config里面注入,但是实际上在我的config配置里面已经注入了,代码如下:
/**
* 认证管理
*/
@Autowired
private AuthenticationManager authenticationManager;
/**
* redis连接工具
*/
@Autowired
private RedisConnectionFactory redisConnectionFactory;
/**
* redis存储token
* @return
*/
@Bean
RedisTokenStore redisTokenStore() {
return new RedisTokenStore(redisConnectionFactory);
}
/**
* 配置
*/
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.tokenStore(redisTokenStore()).authenticationManager(authenticationManager);
}
其实原因我不知道,解决方法很简单就是重新实例化bean就OK
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
/**
* 重新实例化bean
*/
@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
}