如果我没有误解的话,AuthenticationProvider
使用 UserDetailsService
来检索用户的属性,以便对 Authentication
对象进行身份验证。
问题是在接下来的代码中,没有为 AuthenticationProvider
配置,也没有为 UserDetailsService
配置。
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("user").password("pass").roles("ADMIN").and().withUser("user1")
.password("pass").roles("USER");
}
尚未设置认证服务。
我的问题是,是否有 AuthenticationProvider
的实现和 UserDetailsService
的实现在内部添加到 spring 上下文中?在这种情况下,使用的实现是什么(在 memoryAuthentication 的情况下)。
配置的 *.withUser("user").password("pass").roles("ADMIN")*
部分是否表示 UserDetailsService
实现?
最佳答案
是的,对于这样的配置,AuthenticationProvider
和 UserDetailsService
bean 是隐式配置的。
.inMemoryAuthentication()
将 Spring Security 配置为使用 InMemoryUserDetailsManager
,它(间接)实现了 UserDetailsService
接口(interface),因此它是一个 UserDetailsService
本身。
DaoAuthenticationProvider
默认用作 AuthenticationProvider
实现,带有 inMemoryAuthentication()
。
.withUser("user").password("pass").roles("ADMIN")
配置 InMemoryUserDetailsManager
已知的用户。这可用于填充您要用于登录的用户。
还有一件事:并不是所有的 AuthenticationProvider
都使用 UserDetailsService
来获取用户详细信息。实际上,在标准的 AuthenticationProvider
实现中,只有 DaoAuthenticationProvider
类使用了 UserDetailsService
。
关于java - Spring Security 默认使用的 AuthenticationProvider 和 UserDetailsService,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44909073/