06 Shrio Authenticator及AuthenticationStrategy

简介: 06 Shrio Authenticator及AuthenticationStrategy

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<org.apache.shiro.mgt.SecurityManager> 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.inishiro-authenticator-all-success.ini不同的配置是使用了securityManager.realms=$myRealm1,$myRealm2;myRealm验证失败。

对于AtLeastOneSuccessfulStrategyFirstSuccessfulStrategy的区别,请参照testAtLeastOneSuccessfulStrategyWithSuccesstestFirstOneSuccessfulStrategyWithSuccess测试方法。唯一不同点一个是返回所有验证成功的Realm的认证信息;另一个是只返回第一个验证成功的Realm的认证信息。

自定义AuthenticationStrategy实现,首先看其API:

//在所有Realm验证之前调用  
AuthenticationInfo beforeAllAttempts(  
Collection<? extends Realm> realms, AuthenticationToken token)   
throws AuthenticationException;  
//在每个Realm之前调用  
AuthenticationInfo beforeAttempt(  
Realm realm, AuthenticationToken token, AuthenticationInfo aggregate)   
throws AuthenticationException;  
//在每个Realm之后调用  
AuthenticationInfo afterAttempt(  
Realm realm, AuthenticationToken token,   
AuthenticationInfo singleRealmInfo, AuthenticationInfo aggregateInfo, Throwable t)  
throws AuthenticationException;  
//在所有Realm之后调用  
AuthenticationInfo afterAllAttempts(  
AuthenticationToken token, AuthenticationInfo aggregate)   
throws AuthenticationException;

因为每个AuthenticationStrategy实例都是无状态的,所有每次都通过接口将相应的认证信息传入下一次流程;通过如上接口可以进行如合并/返回第一个验证成功的认证信息。

自定义实现时一般继承org.apache.shiro.authc.pam.AbstractAuthenticationStrategy即可,具体可以参考代码com.github.zhangkaitao.shiro.chapter2.authenticator.strategy包下OnlyOneAuthenticatorStrategyAtLeastTwoAuthenticatorStrategy

目录
相关文章
|
Dubbo 应用服务中间件
java.io.IOException: invalid constant type: 18
java.io.IOException: invalid constant type: 18
1168 0
java.io.IOException: invalid constant type: 18
|
前端开发 JavaScript API
React开发需要了解的10个库
本文首发于微信公众号“前端徐徐”,介绍了React及其常用库。React是由Meta开发的JavaScript库,用于构建动态用户界面,广泛应用于Facebook、Instagram等知名网站。文章详细讲解了Axios、Formik、React Helmet、React-Redux、React Router DOM、Dotenv、ESLint、Storybook、Framer Motion和React Bootstrap等库的使用方法和应用场景,帮助开发者提升开发效率和代码质量。
629 4
React开发需要了解的10个库
|
Shell Linux Windows
忘掉Iterm2,试试这款跨平台终端工具
Mac用户最多的用的就是Iterm2了,windows之前因为丑陋的终端也开发了新的终端工具,很神奇的是,很长一段时间里,都没有一款真正好用的能跨平台的终端工具,直到我发现了hyper。无论Windows、Mac、Debian、Fedora还是其他Linux系统,hyper都能支持。
1611 0
忘掉Iterm2,试试这款跨平台终端工具
|
存储 安全 容灾
同步与备份
【7月更文挑战第1天】同步与备份
1527 70
|
前端开发 JavaScript 容器
文字溢出隐藏及鼠标悬停显示效果
文字溢出隐藏及鼠标悬停显示效果
682 0
|
安全 算法 数据安全/隐私保护
什么是身份验证器应用?
【5月更文挑战第14天】什么是身份验证器应用?
1506 0
|
存储 API Python
python之代理ip的配置与调试
python之代理ip的配置与调试
484 7
|
NoSQL Redis
The last packet sent successfully to the server was 0 milliseconds ago. 若依修改,redis中的主机忘记修改
The last packet sent successfully to the server was 0 milliseconds ago. 若依修改,redis中的主机忘记修改
|
人工智能 Shell 语音技术
极速进化,光速转录,C++版本人工智能实时语音转文字(字幕/语音识别)Whisper.cpp实践
业界良心OpenAI开源的[Whisper模型](https://v3u.cn/a_id_272)是开源语音转文字领域的执牛耳者,白璧微瑕之处在于无法通过苹果M芯片优化转录效率,Whisper.cpp 则是 Whisper 模型的 C/C++ 移植版本,它具有无依赖项、内存使用量低等特点,重要的是增加了 Core ML 支持,完美适配苹果M系列芯片。 Whisper.cpp的张量运算符针对苹果M芯片的 CPU 进行了大量优化,根据计算大小,使用 Arm Neon SIMD instrisics 或 CBLAS Accelerate 框架例程,后者对于更大的尺寸特别有效,因为 Accele
|
存储 安全 网络安全
HTTP与HTTPS的区别:安全性、协议地址和默认端口等比较
HTTP与HTTPS的区别:安全性、协议地址和默认端口等比较
1200 0