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-memoryJDBC, 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 

public interface UserDetailsService
Core interface which loads user-specific data. 加载用户指定数据的核心接口。

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

public interface UserDetails extends Serializable
Provides core user information.    提供核心的用户信息

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

public class DaoAuthenticationProvider extends AbstractUserDetailsAuthenticationProvider
 
An AuthenticationProvider implementation that retrieves user details from a UserDetailsService.
一个 AuthenticationProvider 实现,用于从 UserDetailsService 检索用户详细信息。
posted @   红拂夜奔  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结
点击右上角即可分享
微信分享提示