【问题标题】:AuthenticationProvider and UserDetailsService used by default by Spring SecuritySpring Security 默认使用的 AuthenticationProvider 和 UserDetailsS​​ervice
【发布时间】:2017-12-08 02:38:00
【问题描述】:

如果我没有误解,AuthenticationProvider 使用UserDetailsService 检索用户的属性以验证Authentication 对象。

问题在于,在下一个代码中,AuthenticationProviderUserDetailsService 都没有配置。

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

但是认证服务已经设置好了。

我的问题是,AuthenticationProviderUserDetailsService 的另一个实现是否已在内部添加到 spring 上下文中?在这种情况下,使用的实现是什么(在 memoryAuthentication 的情况下)。

配置的*.withUser("user").password("pass").roles("ADMIN")* 部分是否代表UserDetailsService 实现?

【问题讨论】:

    标签: java spring-security


    【解决方案1】:

    是的,对于这样的配置,AuthenticationProviderUserDetailsService bean 是隐式配置的。

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

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

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

    还有一点:并非所有AuthenticationProviders 都使用UserDetailsService 来获取用户详细信息。实际上,在标准的AuthenticationProvider 实现中,只有DaoAuthenticationProvider 类使用了UserDetailsService

    【讨论】:

    • 你是说Spring boot默认使用DaoAuthenticationProvider和InMemoryUserDetailManager?
    • 罗曼,当使用CustomUserDetailsS​​ervice时,使用哪个AuthenticationProvider实现?
    猜你喜欢
    • 2014-10-06
    • 2015-10-16
    • 2012-06-11
    • 2016-12-19
    • 2016-01-24
    • 1970-01-01
    • 1970-01-01
    • 2015-04-07
    • 1970-01-01
    相关资源
    最近更新 更多