java - Spring Security 默认使用的 AuthenticationProvider 和 UserDetailsS​​ervice

标签 java spring-security

如果我没有误解的话,AuthenticationProvider 使用 UserDetailsS​​ervice 来检索用户的属性,以便对 Authentication 对象进行身份验证。

问题是在接下来的代码中,没有为 AuthenticationProvider 配置,也没有为 UserDetailsS​​ervice 配置。

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().withUser("user").password("pass").roles("ADMIN").and().withUser("user1")
            .password("pass").roles("USER");
}

尚未设置认证服务。

我的问题是,是否有 AuthenticationProvider 的实现和 UserDetailsS​​ervice 的实现在内部添加到 spring 上下文中?在这种情况下,使用的实现是什么(在 memoryAuthentication 的情况下)。

配置的 *.withUser("user").password("pass").roles("ADMIN")* 部分是否表示 UserDetailsS​​ervice 实现?

最佳答案

是的,对于这样的配置,AuthenticationProviderUserDetailsS​​ervice bean 是隐式配置的。

.inMemoryAuthentication() 将 Spring Security 配置为使用 InMemoryUserDetailsManager,它(间接)实现了 UserDetailsS​​ervice 接口(interface),因此它是一个 UserDetailsS​​ervice 本身。

DaoAuthenticationProvider 默认用作 AuthenticationProvider 实现,带有 inMemoryAuthentication()

.withUser("user").password("pass").roles("ADMIN") 配置 InMemoryUserDetailsManager 已知的用户。这可用于填充您要用于登录的用户。

还有一件事:并不是所有的 AuthenticationProvider 都使用 UserDetailsS​​ervice 来获取用户详细信息。实际上,在标准的 AuthenticationProvider 实现中,只有 DaoAuthenticationProvider 类使用了 UserDetailsS​​ervice

关于java - Spring Security 默认使用的 AuthenticationProvider 和 UserDetailsS​​ervice,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44909073/

相关文章:

java - 当我想在方法中引用实例变量时,我应该使用 "this"关键字吗?

Java 进程终止 - onEnd()?

java - 单击按钮时 Spring 安全性检查用户是否已登录

java - 多个方法的 MethodSecurityInterceptor

java - 字段authenticationManager LoginController需要一个类型为AuthenticationManager'的bean,但无法找到

java - Java中中缀表达式的正则表达式问题

java - JSF 1.2生命周期理解: Executing the ValueChangeListener method in InvokeApplication phase

java - Spring MVC 显示日期变量

grails - Spring Security Core Plugin 2.0-RC2 升级问题?

java - Spring Security 设置过期 url