【Shiro】三、Apache Shiro认证
配置好并获取到SecurityManager,代表Shiro正常运行起来了,可以使用Shiro的其它功能。
1、认证流程(API的使用流程)
认证的数据:
Principals:标识
·识别Subject的数据
·如:用户名、身份证号等
·Primary Principal:Subject唯一主编号
Credentials:凭证
·Subject私密数据
·如:密码、指纹、视网膜等
认证的步骤:
收集数据→提交验证→结果处理
收集数据方式:
1、自行实现Shiro的AuthenticationToken接口
2、使用Shiro提供的一些默认AuthenticationToken接口实现,如UsernamePasswordToken实现
提交验证:
Subject代表当前用户,调用Subject的登录方法Subject.login()
结果处理
成功:使用Subject的isAuthenticated()方法查看
失败:抛出异常,可以捕获异常进行处理。
认证的代码示例:
//收集数据
UsernamePasswordToken token =
new UsernamePasswordToken(username,password);
token.setRememberMe(true);//默认实现提供的一些辅助功能 //提交验证
Subject currentUser = SecurityUtils.getSubject();
currentUser.login(token); //验证是否成功
Subject loginUser = SecutiryUtils.getSubject();
loginUser.isAuthenticated() == true; //登录失败时处理异常
try{
currentUser.login(token);
} catch ( ExType1 ex1){
} catch ( ExType2 ex2){
} catch ( ExType3 ex3){
}....
Remember Me
Remembered
·认证信息非空
·认证信息来自上一个Session的认证结果
·subject.isRememebered() = true 判断上次是否使用RememberMe
Authenticated
·认证信息来自当前Session的认证结果
·subject.isAuthenticated() = true 判断本次认证是否通过
使用场景:
商城购物车
1、上次登录了,这次开启浏览器未登录,想把一个货品加入到购物车(如果此时要求先登录,用户体验不好,现在的网站都是可以先加入购物车,然后付款时再登录付款的)
这时候,使用isRememebered(),获取上次的认证信息,把购物车数据直接存到上次认证的用户中。
2、需要下单付款了
此时再要求登录,然后使用isAuthenticated(),确定本次登录的用户是否正确。
Loggin out
//使Session无效,清空所有认证信息
currentUser.logout();
2、认证架构(框架的内部运作)
1、调用Subject.login(token)方法
2、找Security Manager(门面模式)
3、调用Authenticator组件
4、组件中有很多策略,这些策略会调用Realm获取数据,最终用来判断是否通过验证
5、通过Realm访问数据库等获取数据,用于判断是否通过认证
Authenticator
单个Realm
ModularRealmAuthenticator:仅有一个Realm,仅通过这个Realm就可以知道是否通过认证
多个Realm
AuthenticationStrategy:当有多个Realm时,通过某种策略去判断怎样才算认证通过
自定义Authenticator
当上面都无法满足的时候,我们可以自定义实现一个Authenticator
然后如下面代码一样,把这个Authenticator赋值给Security Manager即可
[main]
...
authenticator = com.foo.bar.CustomAuthenticator securityManager.authenticator = $authenticator
AuthenticationStrategy
AtLeastOneSuccessfulStrategy[默认值]
只要有一个Realm验证成功即可,返回所有Realm身份验证成功的认证信息。
FirstSuccessfulStrategy
只要有一个Reaml验证成功即可,只返回第一个Reaml身份验证成功的认证信息,其它忽略。
AllSuccessfulStrategy
所有Realm验证成功才算成功,切返回所有Realm身份验证成功的认证信息,如果有一个失败就失败。
[main]
...
authcStrategy = org.apache.shiro.authc.pam.FirstSuccessfulStrategy securityManager.authenticator.authenticationStrategy = $authcStrategy
Realm认证顺序
多个Realm认证顺序
迭代认证
隐式顺序
按照配置书写代码的顺序来认证
blahRealm = com.company.blah.Realm
fooRealm = com.company.foo.Realm
barRealm = com.company.another.Realm
显式顺序
使用securityManager.realms
blahRealm = com.company.blah.Realm
fooRealm = com.company.foo.Realm
barRealm = com.company.another.Realm
securityManager.realms = $fooRealm,$barRealm,$blahRealm
【Shiro】三、Apache Shiro认证的更多相关文章
- 【Shiro】Apache Shiro架构之权限认证(Authorization)
Shiro系列文章: [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之集成web [Shiro]Apache Shir ...
- 【Shiro】Apache Shiro架构之身份认证(Authentication)
Shiro系列文章: [Shiro]Apache Shiro架构之权限认证(Authorization) [Shiro]Apache Shiro架构之集成web [Shiro]Apache Shiro ...
- 【Shiro】Apache Shiro架构之自定义realm
[Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之权限认证(Authorization) [Shiro]Apache S ...
- 【Shiro】Apache Shiro架构之集成web
Shiro系列文章: [Shiro]Apache Shiro架构之身份认证(Authentication) [Shiro]Apache Shiro架构之权限认证(Authorization) [Shi ...
- [转载] 【Shiro】Apache Shiro架构之实际运用(整合到Spring中)
写在前面:前面陆陆续续对Shiro的使用做了一些总结,如题,这篇博文主要是总结一下如何将Shiro运用到实际项目中,也就是将Shiro整到Spring中进行开发.后来想想既然要整,就索性把Spring ...
- 让Apache Shiro保护你的应用
在尝试保护你的应用时,你是否有过挫败感?是否觉得现有的Java安全解决方案难以使用,只会让你更糊涂?本文介绍的Apache Shiro,是一个不同寻常的Java安全框架,为保护应用提供了简单而强大的方 ...
- Apache Shiro 手册
(一)Shiro架构介绍 一.什么是Shiro Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加密和会话管理等功能: 认证 - 用户身份识别,常被称为用户"登录 ...
- Apache Shiro 使用手册---转载
原文地址:http://www.360doc.com/content/12/0104/13/834950_177177202.shtml (一)Shiro架构介绍 一.什么是Shiro Apache ...
- Apache Shiro 使用手册
http://kdboy.iteye.com/blog/1154644 (一)Shiro架构介绍 一.什么是Shiro Apache Shiro是一个强大易用的Java安全框架,提供了认证.授权.加 ...
- Apache Shiro 快速入门教程,shiro 基础教程 (这篇文章非常好)
第一部分 什么是Apache Shiro 1.什么是 apache shiro : Apache Shiro是一个功能强大且易于使用的Java安全框架,提供了认证,授权,加密,和会话管理 ...
随机推荐
- Spring JDK动态代理
1. 创建项目在 MyEclipse 中创建一个名称为 springDemo03 的 Web 项目,将 Spring 支持和依赖的 JAR 包复制到 Web 项目的 WEB-INF/lib 目录中,并 ...
- html常用代码
<marquee width="70%" scrollamount="2">大家好</marquee> // 大家好 字符从左到右 ...
- python接口自动化测试三十六:数据驱动参数化之paramunittest
官方文档1.官方文档地址:https://pypi.python.org/pypi/ParamUnittest/2.github源码下载地址:https://github.com/rik0/Param ...
- appium常见问题10_MAC_终端输入aapt指令报错提示"command not found"
问题: MAC终端使用aapt指令"aapt dump badging xxx/xxx/xxx.apk"查看apk包名和activity时报错提示"command not ...
- LeetCode 95. Unique Binary Search Trees II 动态演示
比如输入为n, 这道题目就是让返回由1,2,... n的组成的所有二叉排序树,每个树都必须包含这n个数 这是二叉树的排列组合的题目.排列组合经常用DFS来解决. 这道题比如输入为3,也就是求start ...
- 【awk】 判断是不是纯ascii串
筛选出纯ascii串: awk '{ l = length($0); for (i = l; i > 0; i--) { if (substr($0,i,1) > "\177&q ...
- 【Nacos】数据一致性
转自:https://blog.csdn.net/liyanan21/article/details/89320872 目录 一.Raft算法 二.Nacos中Raft部分源码 init() 1. 获 ...
- 【UR #5】怎样跑得更快
题目 给定\(n,c,d\)和序列\(\{b_i\}\),求一个序列\(\{x_i\}\)满足 \[\sum_{j=1}^n\gcd(i,j)^c\times \rm{lcm(i,j)^d}\time ...
- Linux安装配置nfs实现共享远程目录
1. 服务端安装nfs yum -y install nfs-utils rpcbind 2.编辑/etc/exports /etc/exports文件内容格式: <输出目录> [客户端1 ...
- 2018-10-8-如何安装-btsync
title author date CreateTime categories 如何安装 btsync lindexi 2018-10-8 9:15:6 +0800 2018-2-13 17:23:3 ...