NyanMps / SecurityExample

SpringSecurity练习工程
1 stars 0 forks source link

AuthenticationManager无法注入 #3

Open bfchengnuo opened 4 years ago

bfchengnuo commented 4 years ago

在 SpringSecurity4.x 还是可以直接注入的,但是到了 SpringSecurity5.x 后,如果再直接使用 @Autowired 注入,就会报错:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field authenticationManager in cn.springcloud.book.OAuthConfiguration 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.

也没有其他什么好方法,就是手动再添加到 IOC 呗,找一个配置类(嫌麻烦直接在启动类上搞)继承 WebSecurityConfigurerAdapter 然后定义:

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

参考: