1 认证原理

1.1 Principals与Credentials

认证就是进行身份确认的过程,也就是用户(对应Shiro中的Subject)需要提供证明来证实自己的身份 就像到自动取款机取款,持有银行卡的人就可以理解为此处的用户,银行卡的取款密码就是证明材料,如果输入正确的密码,就可以进行取款 在这个过程中,有两个概念,用户和证明材料,对应Shiro中的就分别是Principals与Credentials

1.2 认证步骤

要进行认证,我们需要先收集用户的Principals与Credentials 比如用户通过页面上的表单提交用户名和密码,APP用户通过提交手机号与短信验证码,然后交由服务端进行处理。

①服务端首先收集Principals与Credentials,对应Shiro的代码

UsernamePasswordToken token = new UsernamePasswordToken("username", "passwd");

这里我们使用Shiro中通过的用户名/密码认证方式,或者你可以实现AuthenticationToken接口来自定义

②接下来进行提交,对应代码

Subject currentUser = SecurityUtils.getSubject();
currentUser.login(token);

通过SecurityUtils得到Subject,其会自动绑定到当前线程如果在web环境在请求结束时需要解除绑定 然后获取身份验证的Token,如用户名/密码;

③认证结果

if (currentUser.isAuthenticated()) {// success do something
} else {// fail throw exception
}

1.3 认证原理

在了解了Shiro认证过程的基本代码操作后,我们来看下底层是到底如何实现 首先我们先通过Shiro官方给出的一张认证流程图来作全局的了解,看看底层认证都涉及到了哪些东西

① 获取Subject对象

然后收集用户的认证资料,调用Subject对象的login(token)方法

② 将方法的调用传递给底层的SecurityManager

DelegatingSubject作为Subject的实现,本身并不负责处理认证与授权的逻辑 本质上,DelegatingSubject只是SecurityManager的代理类,①中login(token)方法的调用,本质上调用的是SecurityManager接口的login(token)方法,而DefaultSecurityManager作为SecurityManager的默认实现,将调用Authenticator进行认证逻辑处理

③ Authenticator接口是Shiro API中的主要入口之一,就是用来负责应用中的认证操作的

该类作为顶级接口,只有一个authenticate(AuthenticationToken token)方法 而ModularRealmAuthenticator作为Shiro默认的认证处理实现类将会接过认证处理的枪,通过doAuthenticate(AuthenticationToken token)进行认证 源码如下

Collection<Realm> realms = getRealms();
if (realms.size() == 1) {return doSingleRealmAuthentication(realms.iterator().next(), authenticationToken);
} else {return doMultiRealmAuthentication(realms, authenticationToken);
}

④ 通常情况下应用中会使用单个的Realm来进行认证授权处理,但是强大的Shiro却支持配置多个Realm,在多个Realm对象存在的情况下,就需要指定认证策略AuthenticationStrategy ,Shiro提供了三种具体的认证策略实现

  • AtLeastOneSuccessfulStrategy

ModularRealmAuthenticator的默认实现,多个Realm中,如果有一个或以上认证通过,就表示认证成功

  • FirstSuccessfulStrategy:只使用第一个认证通过的Realm返回的信息,后面的Realm将会被忽略
  • AllSuccessfulStrategy:所有Realm认证通过才算认证成功,否则认证失败
public class AllSuccessfulStrategy extends AbstractAuthenticationStrategy {/** Private class log instance. */private static final Logger log = LoggerFactory.getLogger(AllSuccessfulStrategy.class);/*** Because all realms in this strategy must complete successfully, this implementation ensures that the given* <code>Realm</code> {@link org.apache.shiro.realm.Realm#supports(org.apache.shiro.authc.AuthenticationToken) supports} the given* <code>token</code> argument.  If it does not, this method throws an* {@link UnsupportedTokenException UnsupportedTokenException} to end the authentication* process immediately. If the realm does support the token, the <code>info</code> argument is returned immediately.*/public AuthenticationInfo beforeAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo info) throws AuthenticationException {if (!realm.supports(token)) {String msg = "Realm [" + realm + "] of type [" + realm.getClass().getName() + "] does not support " +" the submitted AuthenticationToken [" + token + "].  The [" + getClass().getName() +"] implementation requires all configured realm(s) to support and be able to process the submitted " +"AuthenticationToken.";throw new UnsupportedTokenException(msg);}return info;}/*** Merges the specified <code>info</code> into the <code>aggregate</code> argument and returns it (just as the* parent implementation does), but additionally ensures the following:* <ol>* <li>if the <code>Throwable</code> argument is not <code>null</code>, re-throws it to immediately cancel the* authentication process, since this strategy requires all realms to authenticate successfully.</li>* <li>neither the <code>info</code> or <code>aggregate</code> argument is <code>null</code> to ensure that each* realm did in fact authenticate successfully</li>* </ol>*/public AuthenticationInfo afterAttempt(Realm realm, AuthenticationToken token, AuthenticationInfo info, AuthenticationInfo aggregate, Throwable t)throws AuthenticationException {if (t != null) {if (t instanceof AuthenticationException) {//propagate:throw ((AuthenticationException) t);} else {String msg = "Unable to acquire account data from realm [" + realm + "].  The [" +getClass().getName() + " implementation requires all configured realm(s) to operate successfully " +"for a successful authentication.";throw new AuthenticationException(msg, t);}}if (info == null) {String msg = "Realm [" + realm + "] could not find any associated account data for the submitted " +"AuthenticationToken [" + token + "].  The [" + getClass().getName() + "] implementation requires " +"all configured realm(s) to acquire valid account data for a submitted token during the " +"log-in process.";throw new UnknownAccountException(msg);}log.debug("Account successfully authenticated using realm [{}]", realm);// If non-null account is returned, then the realm was able to authenticate the// user - so merge the account with any accumulated before:merge(info, aggregate);return aggregate;}
}

⑤ 通过Realm进行认证最终的逻辑判断,我们此处以应用只存在单个Realm来进行介绍。Realm首先会通过realm.supports(token)进行验证,验证Realm是否支持对应的token进行认证操作,如果返回true,将会进行认证逻辑处理,否则直接忽略认证逻辑,如果我们的应用只想处理授权,可以自定义Realm,并将supports方法返回false即可。

Realm会通过token与INI配置文件中的配置项进行对比,或者与我们数据库存储的数据进行对比,如果相同则认证通过。

1.4 IniRealm认证实现

Shiro默认使用IniRealm

但是前提是我们在INI配置中指定了[users]或[roles]有效配置数据,否则就会用配置中指定的securityManager的realms,如果两者都没有指定那么就会抛出错误,因为Shiro应用,至少要配置一个Realm

IniRealm在初始化onInit()时,会将已经加载的INI文件中的[users]、[roles]配置进行处理,分别转换为SimpleRole、SimpleAccount,再将SimpleAccount与SimpleRole进行绑定,至此,IniRealm对INI配置文件处理已经完毕。

但是认证的操作并没有完成,IniRealm仍需要与传递过来的token进行对比,判断是否相同,具体的判断逻辑交由AuthenticatingRealm来进行。

1.5 AuthenticatingRealm

AuthenticatingRealm是Realm的顶级抽象实现类

主要用于处理认证操作,至于授权等操作则交由该类的子类去处理。

AuthenticatingRealm拿到token后,会先去缓存中查找是否存在对应的认证信息,如果存在直接使用缓存中的认证信息与token进行比对,如果缓存中不存在,则直接获取IniRealm中的认证信息进行比对,比对通过后,返回认证成功的Subject对象。

shiro原理_Shiro-实战(二)-身份认证相关推荐

  1. Spring Boot2整合Shiro(1):身份认证

    Spring Boot2整合Shiro(1):身份认证 前言 本文主要介绍了在Spring Boot2项目中整合Shiro实现登录认证.本文假设读者已经对Shiro和基于RBAC的权限控制系统有了基本 ...

  2. shiro中的验证用户身份认证以及授权

    目录 1.运用shiro进行用户身份认证: 1.1导入基于Shiro的数据库脚本 1.2.引入依赖(shiro-1.4.1) shiro-core  shiro-web  shiro-spring 1 ...

  3. Springboot整合shiro基于url身份认证和授权认证

    你还不会shiro吗? 前奏 shiro核心配置文件(rolesFilter可选). 身份认证 多表登录源如何操作? 授权管理 如何解决界面多角色/资源问题 访问效果 权限管理在日常开发中很重要,所以 ...

  4. mysql url认证_Springboot+shiro基于url身份认证和授权认证

    你还不会shiro吗?前奏 shiro核心配置文件(rolesFilter可选). 身份认证 多表登录源如何操作? 授权管理 如何解决界面多角色/资源问题 访问效果 权限管理在日常开发中很重要,所以硬 ...

  5. Shiro(三) 身份认证源码分析与 MD5 盐值加密

    文章目录 1. 身份认证 2. 身份验证的基本流程 3. 身份验证实现 3.1 在 `login.jsp` 添加登录表单 3.2 添加表单提交的 Controller 3.3 完善 Realm 的身份 ...

  6. Shiro学习总结(3)——Apache Shiro身份认证

    身份验证,即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明. 在shiro中,用户需要提供principals (身份)和cre ...

  7. Shiro学习(2)身份验证

    身份验证:即在应用中谁能证明他就是他本人.一般提供如他们的身份ID一些标识信息来表明他就是他本人,如提供身份证,用户名/密码来证明. 在shiro中,用户需要提供principals (身份)和cre ...

  8. Shiro身份认证授权原理

    shiro在应用程序中的使用是用Subject为入口的, 最终subject委托给真正的管理者ShiroSecurityMannager Realm是Shiro获得身份认证信息和来源信息的地方(所以这 ...

  9. Taro多端开发实现原理与项目实战(二)

    Taro多端开发实现原理与项目实战(二) 多端电商平台项目概述及开发准备 学习了前面的基础知识和进阶后是否跃跃欲试?我们准备了一个电商平台的项目来和大家一起实践使用 Taro 开发电商平台. 项目概述 ...

  10. Shiro身份认证---转

    目录 1. Shro的概念 2. Shiro的简单身份认证实现 3. Shiro与spring对身份认证的实现 前言: Shiro 可以非常容易的开发出足够好的应用,其不仅可以用在 JavaSE 环境 ...

最新文章

  1. 性能测试---商场模型分析
  2. JavaScript(数据类型、字符串操作)
  3. linux 3gpp格式转换器,Movavi Video Converter
  4. Qt对象类型转换(char* int与Qstring间的转换)
  5. Android深度探索(卷一)第四章读书笔记
  6. 【来自项目的技术点】— 聊聊键盘长按的时候解决卡顿方案
  7. java集合类(简介)
  8. C语言学习笔记---随机数rand()函数
  9. BZOJ 3689 异或之
  10. 利用python将长视频、长语音转换成文字教程 ,非常好用
  11. 【DFS 水洼数目】
  12. 幻灯片放映时无法切换到下一张
  13. 当SAP遇上马云,智慧企业加速中国数字化转型
  14. [Puppeteer]Puppeteer+Mocha+Chai实现端到端测试
  15. 微信小程序之手机号快速注册
  16. 百度智能云开物工业互联网平台解决方案亮相2021服贸会成果发布会
  17. 不留痕迹的清除部分history历史命令记录
  18. java编写的atm机项目结题报告_《计算机学院视频教程网站的创建》教学研究项目结题报告.pdf...
  19. linux如何判断网线插入_网卡如何识别10M/100M,如何检测网线插入
  20. 利用SwitchyOmega和Jmeter实现Jmeter录制功能

热门文章

  1. python选择题总结
  2. android手机开机密码,安卓手机锁屏密码忘了怎么办 锁屏密码解决方法
  3. 致知在格物,物格而后知至,知至而后意诚,意诚而后心正,心正而后身修,身修而后家齐,家齐而后...
  4. PCI、PCIE转的并口需要用ECP或EPP模式怎么办?
  5. Zeppelin0.8.1上操作hive(使用jdbc解释器)
  6. 全球与中国烧碱片市场深度研究分析报告
  7. 【英语语法入门】 第13讲 形容词
  8. 蓝色主题登陆页面界面模板
  9. [luogu P2183] [国家集训队]礼物 {exlucas}
  10. 把计算机网络关闭啦怎么打开,网络发现已关闭怎么办?Win7系统启用/关闭网络发现方法(图文)...