概述
介绍
UserDetailsService
是Spring Security
提供的一个概念模型接口,用于抽象建模系统提供这样一种服务能力:管理用户详情。
UserDetailsService
只定义了一个方法UserDetails loadUserByUsername(String username) throws UsernameNotFoundException
,声明通过用户名username
可以获取用户详情UserDetails
,如果对应用户名username
的用户记录(一般也称作用户账号)不存在,则抛出异常UsernameNotFoundException
。
注意方法loadUserByUsername
的具体实现在比较用户名时可以大小写区分也可以大小写不区分,具体怎么做留给实现者决定。
方法loadUserByUsername
如果找到一个用户记录的话会返回一个可序列化的UserDetails
对象,它包含如下信息 :
- 用户名
- 账号是否过期
- 账号是否被锁定
- 账号安全凭证(通常意义上指的就是的密码)是否过期
- 账号是否被禁用
- 所赋予的权限集合
继承关系
从此继承关系图可见,UserDetailsService
是Spring Security
的一个基础接口。
源代码
源代码版本 : Spring Security Core 5.1.4.RELEASE
public interface UserDetailsService {
// ~ Methods
// ==========================================================
/**
* Locates the user based on the username. In the actual implementation, the search
* may possibly be case sensitive, or case insensitive depending on how the
* implementation instance is configured. In this case, the UserDetails
* object that comes back may have a username that is of a different case than what
* was actually requested..
*
* @param username the username identifying the user whose data is required.
*
* @return a fully populated user record (never null)
*
* @throws UsernameNotFoundException if the user could not be found or the user has no
* GrantedAuthority
*/
UserDetails loadUserByUsername(String username) throws UsernameNotFoundException;
}