Authenticator的职责是验证用户帐号,是Shiro API中身份验证核心的入口点:
public AuthenticationInfo authenticate(AuthenticationToken authenticationToken)
throws AuthenticationException;
如果验证成功,将返回AuthenticationInfo
验证信息;此信息中包含了身份及凭证;如果验证失败将抛出相应的AuthenticationException
实现。
SecurityManager
接口继承了Authenticator
,另外还有一个ModularRealmAuthenticator
实现,其委托给多个Realm
进行验证,验证规则通过AuthenticationStrategy
接口指定,默认提供的实现:
FirstSuccessfulStrategy: 只要有一个Realm
验证成功即可,只返回第一个Realm
身份验证成功的认证信息,其他的忽略;
AtLeastOneSuccessfulStrategy: 只要有一个Realm
验证成功即可,和FirstSuccessfulStrategy
不同,返回所有Realm
身份验证成功的认证信息;
AllSuccessfulStrategy: 所有Realm
验证成功才算成功,且返回所有Realm
身份验证成功的认证信息,如果有一个失败就失败了。
ModularRealmAuthenticator
默认使用AtLeastOneSuccessfulStrategy
策略。
假设我们有三个realm: myRealm1: 用户名/密码为zhang/123时成功,且返回身份/凭据为zhang/123; myRealm2: 用户名/密码为wang/123时成功,且返回身份/凭据为wang/123; myRealm3: 用户名/密码为zhang/123时成功,且返回身份/凭据为zhang@163.com/123,和myRealm1不同的是返回时的身份变了;
1、ini配置文件(shiro-authenticator-all-success.ini)
#指定securityManager的authenticator实现
authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
securityManager.authenticator=$authenticator
#指定securityManager.authenticator的authenticationStrategy
allSuccessfulStrategy=org.apache.shiro.authc.pam.AllSuccessfulStrategy
securityManager.authenticator.authenticationStrategy=$allSuccessfulStrategy
myRealm1=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm1
myRealm2=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm2
myRealm3=com.github.zhangkaitao.shiro.chapter2.realm.MyRealm3
securityManager.realms=$myRealm1,$myRealm3
2、测试代码(com.github.zhangkaitao.shiro.chapter2.AuthenticatorTest)
2.1、首先通用化登录逻辑
private void login(String configFile) {
//1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager
Factory factory =
new IniSecurityManagerFactory(configFile);
//2、得到SecurityManager实例 并绑定给SecurityUtils
org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
SecurityUtils.setSecurityManager(securityManager);
//3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证)
Subject subject = SecurityUtils.getSubject();
UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
subject.login(token);
}
2.2、测试AllSuccessfulStrategy成功:
@Test
public void testAllSuccessfulStrategyWithSuccess() {
login("classpath:shiro-authenticator-all-success.ini");
Subject subject = SecurityUtils.getSubject();
//得到一个身份集合,其包含了Realm验证成功的身份信息
PrincipalCollection principalCollection = subject.getPrincipals();
Assert.assertEquals(2, principalCollection.asList().size());
}
即PrincipalCollection包含了zhang和zhang@163.com身份信息。
2.3、测试AllSuccessfulStrategy失败:
@Test(expected = UnknownAccountException.class)
public void testAllSuccessfulStrategyWithFail() {
login("classpath:shiro-authenticator-all-fail.ini");
Subject subject = SecurityUtils.getSubject();
}
shiro-authenticator-all-fail.ini
与shiro-authenticator-all-success.ini
不同的配置是使用了securityManager.realms=$myRealm1,$myRealm2;
即myRealm
验证失败。
对于AtLeastOneSuccessfulStrategy
和FirstSuccessfulStrategy
的区别,请参照testAtLeastOneSuccessfulStrategyWithSuccess
和testFirstOneSuccessfulStrategyWithSuccess
测试方法。唯一不同点一个是返回所有验证成功的Realm
的认证信息;另一个是只返回第一个验证成功的Realm
的认证信息。
自定义AuthenticationStrategy
实现,首先看其API:
//在所有Realm验证之前调用
AuthenticationInfo beforeAllAttempts(
Collection
关注
打赏