【发布时间】:2017-07-08 02:01:27
【问题描述】:
我正在配置 Spring Security。为了验证和授权用户,我覆盖了WebSecurityConfigurerAdapter 的configure(AuthenticationManagerBuilder auth)。这工作正常。以下是我的代码:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(customUserDetailsService)
.passwordEncoder(getPasswordEncoder());
}
但是当我尝试启用方法级别的安全性时,每个操作,使用 @EnableGlobalMethodSecurity(securedEnabled = true) 它会引发异常:
未找到 AuthenticationManager
据我了解,AuthenticationManager 用于对用户进行身份验证和授权,我已经在使用 configure(AuthenticationManagerBuilder auth) 并且 Spring 正在注入 auth 对象本身。
为什么我需要手动注册AuthenticationManager?
@Bean @Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
configure(AuthenticationManagerBuilder auth) 和 authenticationManagerBean() 有什么不同的用途?
我正在扩展WebSecurityConfigurerAdapter。为什么我需要通过覆盖authenticationManagerBean() 来提供自定义AuthenticationManager。
【问题讨论】:
标签: spring spring-mvc authentication spring-security