5 authentication spring spring-mvc spring-security
我正在配置Spring Security。为了验证和授权用户,我重写configure(AuthenticationManagerBuilder auth)
的WebSecurityConfigurerAdapter
。这很好。下面是我的代码:
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(customUserDetailsService)
.passwordEncoder(getPasswordEncoder());
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试启用方法级安全性时,对于每个操作,使用 @EnableGlobalMethodSecurity(securedEnabled = true)
它都会引发异常:
找不到AuthenticationManager
根据我的理解AuthenticationManager
,我已经使用过身份验证和授权用户,configure(AuthenticationManagerBuilder auth)
并且Spring正在注入auth
对象本身。
为什么我需要AuthenticationManager
手动注册?
@Bean @Override
public AuthenticationManager authenticationManagerBean() throws Exception {
return super.authenticationManagerBean();
}
Run Code Online (Sandbox Code Playgroud)
有什么不同的目的 configure(AuthenticationManagerBuilder auth)
和authenticationManagerBean()
服务?
我在延伸WebSecurityConfigurerAdapter
。为什么我需要AuthenticationManager
通过覆盖来提供风俗习惯authenticationManagerBean()
。
您的配置类extend WebSecurityConfigurerAdapter
,仅配置Web安全性(而非方法安全性):
提供用于创建
WebSecurityConfigurer
实例的便捷基类。该实现允许通过覆盖方法进行自定义。
因此,您AuthenticationManager
仅用于网络安全。
如果要配置(更改默认值)方法安全性,则可以扩展GlobalMethodSecurityConfiguration
:
基地
Configuration
为使全局方法的安全性。类可以扩展此类以自定义默认值,但必须确保EnableGlobalMethodSecurity
在子类上指定注释。
要配置AuthenticationManager
方法安全性,您可以
覆盖GlobalMethodSecurityConfiguration#configure
:
子类可以重写此方法以注册不同类型的身份验证。如果未覆盖,
configure(AuthenticationManagerBuilder)
将尝试按类型自动接线。
将您AuthenticationManager
的bean 公开为可以通过自动连接的bean GlobalMethodSecurityConfiguration
,请参见WebSecurityConfigurerAdapter#authenticationManagerBean
:
重写此方法以将
AuthenticationManager
fromconfigure(AuthenticationManagerBuilder)
公开为Bean。
AuthenticationManager
通过自动装配全局只使用一个全局AuthenticationManagerBuild
,请参见Spring Security 3.2.0.RC2发布:
例如,如果要配置全局身份验证(即,您只有一个AuthenticationManager),则应自动连接AuthenticationMangerBuilder:
Run Code Online (Sandbox Code Playgroud)@Autowired public void configureGlobal(AuthenticationManagerBuilder auth) { // ... configure it ... }
归档时间: |
|
查看次数: |
7928 次 |
最近记录: |