Authentication - Username/Password - UserDetailsService
UserDetailsService
UserDetailsService
is used by DaoAuthenticationProvider
for retrieving a username, a password, and other attributes for authenticating with a username and password. Spring Security provides in-memory, JDBC, and caching implementations of UserDetailsService
.
DaoAuthenticationProvider 使用 UserDetailsService 来检索用户名、密码和其他属性,以便使用用户名和密码进行身份验证。Spring Security 提供了UserDetailsService的in-memory、JDBC 和 caching 实现。
You can define custom authentication by exposing a custom UserDetailsService
as a bean. For example, the following listing customizes authentication, assuming that CustomUserDetailsService
implements UserDetailsService
:
您可以通过将自定义 UserDetailsService 公开为 Bean 来定义自定义身份验证。例如,下面的清单自定义身份验证,假设 CustomUserDetailsService 实现 UserDetailsService
Custom UserDetailsService Bean
@Bean CustomUserDetailsService customUserDetailsService() { return new CustomUserDetailsService(); }
UserDetailsService
It is used throughout the framework as a user DAO and is the strategy used by the DaoAuthenticationProvider
.
The interface requires only one read-only method, which simplifies support for new data-access strategies.
UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException
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..
Parameters:
username - the username identifying the user whose data is required.
Returns:
a fully populated user record (never null)
Throws:
UsernameNotFoundException - if the user could not be found or the user has no GrantedAuthority
UserDetails
Implementations are not used directly by Spring Security for security purposes. They simply store user information which is later encapsulated into Authentication
objects. This allows non-security related user information (such as email addresses, telephone numbers etc) to be stored in a convenient location.
出于安全目的,Spring Security 不直接使用 Implementations。它们只是存储用户信息,这些信息稍后被封装到 Authentication 对象中。这允许将非安全相关的用户信息(例如电子邮件地址、电话号码等)存储在方便的位置。
Concrete implementations must take particular care to ensure the non-null contract detailed for each method is enforced. See User
for a reference implementation (which you might like to extend or use in your code).
具体实现必须特别小心,以确保强制执行针对每种方法详述的非 null 协定。请参阅 User 以获取参考实现(您可能希望在代码中扩展或使用)。
DaoAuthenticationProvider
AuthenticationProvider
implementation that retrieves user details from a UserDetailsService
.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结