rap*_*apt 56 spring spring-security
我定义了以下bean:
<sec:authentication-manager alias="authenticationManager">
<sec:authentication-provider
user-service-ref="userDetailsService" />
</sec:authentication-manager>
Run Code Online (Sandbox Code Playgroud)
我猜这里Spring使用了一些默认的实现AuthenticationManager
.
在我的Java代码中,我有:
@Resource(name = "authenticationManager")
private AuthenticationManager authenticationManager; // specific for Spring Security
public boolean login(String username, String password) {
try {
Authentication authenticate = authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(username, password));
if (authenticate.isAuthenticated()) {
SecurityContextHolder.getContext().setAuthentication(authenticate);
return true;
}
}
catch (AuthenticationException e) {
}
return false;
}
Run Code Online (Sandbox Code Playgroud)
这里AuthenticationManager.authenticate(...)
叫.但是我想知道AuthenticationManager
Spring默认使用哪种实现,以及它authenticate(...)
为了进行身份验证(即确保用户名与密码匹配)的作用.
你能解释一下吗?
cde*_*zaq 69
该AuthenticationManager
实际上只是身份验证提供一个容器,给人一种一致的接口给他们所有.在大多数情况下,默认AuthenticationManager
是绰绰有余.
你打电话的时候
.authenticate(new UsernamePasswordAuthenticationToken(username, password))`
Run Code Online (Sandbox Code Playgroud)
它将传递UsernamePasswordAuthenticationToken
给默认值AuthenticationProvider
,默认情况下将userDetailsService
根据用户名获取用户,并将该用户的密码与身份验证令牌中的密码进行比较.
一般情况下,AuthenticationManager
传递AuthenticationToken
给AuthenticationProviders
它们的每一个,并且每个都检查它,如果它们可以使用它进行身份验证,它们返回时带有"Authenticated","Unauthenticated"或"Not not authenticate"的指示(表示提供者不知道如何处理令牌,所以它传递了处理它)
这种机制允许您插入其他身份验证方案,例如针对LDAP或Active Directory服务器或OpenID进行身份验证,并且是Spring Security框架中的主要扩展点之一.
Ral*_*lph 36
Spring Security仅提供一个真正的AuthenticationManager
实现:
org.springframework.security.authentication.ProviderManager
Run Code Online (Sandbox Code Playgroud)
这AuthenticationProvider
对身份验证任务使用不同
该AuthenticationManagerBeanDefinitionParser
解析负责<sec:authentication-manager>
其Java文档的状态:
注册命名空间配置使用的中央ProviderManager,并允许配置别名,允许用户在其bean中引用它并清楚地看到名称的来源.
它创建ProviderManager
并添加指定的提供.如果在xml中没有指定提供,那么它会添加一个NullAuthenticationProvider
.这至少是一个注意到阻止配置异常的提供者.
来自Spring安全文档:
Spring Security 中的默认实现称为ProviderManager ,它本身不处理身份验证请求,而是委托给已配置的AuthenticationProvider列表,依次查询每个身份验证提供程序以查看是否可以执行身份验证。每个提供程序将抛出异常或返回完全填充的身份验证对象。
有关ProviderManager的信息也可以在主题指南 - Spring Security Architecture中找到:
AuthenticationManager最常用的实现是 ProviderManager,它委托给一系列AuthenticationProvider 实例。AuthenticationProvider有点像 AuthenticationManager但它有一个额外的方法来允许调用者查询它是否支持给定的身份验证类型...
归档时间: |
|
查看次数: |
74721 次 |
最近记录: |