Shiro的鉴权方式
一、 怎么用
Shiro 支持三种方式的授权
- 编程式:通过写 if/else 授权代码块完成:
Subject subject = SecurityUtils.getSubject();
if(subject.hasRole(“admin”)) {
//有权限
} else {
//无权限
}
- 注解式:通过在执行的 Java 方法上放置相应的注解完成:
@RequiresRoles("admin")
public void hello() {
//有权限
}
- JSP/GSP 标签:在 JSP/GSP 页面通过相应的标签完成
<shiro:hasRole name="admin">
<!— 有权限 —>
</shiro:hasRole>
二、啥原理
三种方式背后的原理是一样的,由简单到复杂的顺序来看看。
1、标签式
shiro提供了RoleTag和PermissionTag的抽象类,还有两个简单的实现:
public class HasRoleTag extends RoleTag {
//TODO - complete JavaDoc
public HasRoleTag() {
}
protected boolean showTagBody(String roleName) {
return getSubject() != null && getSubject().hasRole(roleName);
}
}
public class HasPermissionTag extends PermissionTag {
//TODO - complete JavaDoc
public HasPermissionTag() {
}
protected boolean showTagBody(String p) {
return isPermitted(p);
}
}
分别调用的是DelegatingSubject的hasRole方法和isPermitted方法:
public boolean hasRole(String roleIdentifier) {
return hasPrincipals() && securityManager.hasRole(getPrincipals(), roleIdentifier);
}
public boolean isPermitted(String permission) {
return hasPrincipals() && securityManager.isPermitted(getPrincipals(), permission);
}
然后又调用AuthorizingSecurityManager的相关方法,AuthorizingSecurityManager持有一个ModularRealmAuthorizer类型的Authorizer:
this.authorizer = new ModularRealmAuthorizer();
相关方法又转移至调用ModularRealmAuthorizer:
ModularRealmAuthorizer 进行多 Realm 匹配流程:
1、首先检查相应的 Realm 是否实现了实现了 Authorizer;
2、如果实现了 Authorizer,那么接着调用其相应的 isPermitted*/hasRole*接口进行匹配;
3、如果有一个 Realm 匹配那么将返回 true,否则返回 false。
2、注解式
@RequiresRoles("admin")
@RequiresPermissions("admin:role:view")
@RequestMapping(value = "/configIndex", method = { RequestMethod.GET })
public String index(Model model) {
return "rolemgr/roleConfig/configIndex";
}
技术细节
基于拦截器实现(AuthorizingAnnotationMethodInterceptor)
动态代理技术(CglibAopProxy)
spring InvocableHandlerMethod#invoke
CglibAopProxy.DynamicAdvisedInterceptor#intercept
AnnotationMethodInterceptor
AuthorizingAnnotationMethodInterceptor#assertAuthorized
RoleAnnotationHandler#assertAuthorized
委托调用
DelegatingSubject:
public void checkRole(String role) throws AuthorizationException {
assertAuthzCheckPossible();
securityManager.checkRole(getPrincipals(), role);
}
AuthorizingSecurityManager:
public void checkRole(PrincipalCollection principals, String role) throws AuthorizationException {
this.authorizer.checkRole(principals, role);
}
ModularRealmAuthorizer:
public void checkRole(PrincipalCollection principals, String role) throws AuthorizationException {
assertRealmsConfigured();
if (!hasRole(principals, role)) {
throw new UnauthorizedException("Subject does not have role [" + role + "]");
}
}
public boolean hasRole(PrincipalCollection principals, String roleIdentifier) {
assertRealmsConfigured();
for (Realm realm : getRealms()) {
if (!(realm instanceof Authorizer)) continue;
if (((Authorizer) realm).hasRole(principals, roleIdentifier)) {
return true;
}
}
return false;
}
AuthorizingRealm:
public boolean hasRole(PrincipalCollection principal, String roleIdentifier) {
AuthorizationInfo info = getAuthorizationInfo(principal);
return hasRole(roleIdentifier, info);
}
protected boolean hasRole(String roleIdentifier, AuthorizationInfo info) {
return info != null && info.getRoles() != null && info.getRoles().contains(roleIdentifier);
}
关于 getAuthorizationInfo 方法:
protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) {
if (principals == null) {
return null;
}
AuthorizationInfo info = null;
Cache<Object, AuthorizationInfo> cache = getAvailableAuthorizationCache();
if (cache != null) {
Object key = getAuthorizationCacheKey(principals);
info = cache.get(key);
}
if (info == null) {
// 如果cache中没有找到info则调用模板方法
info = doGetAuthorizationInfo(principals);
// info不为null并且cache已经创建了,则缓存info信息
if (info != null && cache != null) {
Object key = getAuthorizationCacheKey(principals);
cache.put(key, info);
}
}
return info;
}
- 首先还是找可用的权限缓存:
private Cache<Object, AuthorizationInfo> getAvailableAuthorizationCache() {
Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
if (cache == null && isAuthorizationCachingEnabled()) {
cache = getAuthorizationCacheLazy();
}
return cache;
}
回顾下安全管理器的结构,RealmSecurityManager继承了CachingSecurityManager,当CachingSecurityManager设置了cacheManager,会调用用子类的afterCacheManagerSet方法:
public void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
afterCacheManagerSet();
}
RealmSecurityManager将会为每个Realm设置cacheManager:
protected void afterCacheManagerSet() {
applyCacheManagerToRealms();
}
protected void applyCacheManagerToRealms() {
CacheManager cacheManager = getCacheManager();
Collection<Realm> realms = getRealms();
if (cacheManager != null && realms != null && !realms.isEmpty()) {
for (Realm realm : realms) {
if (realm instanceof CacheManagerAware) {
((CacheManagerAware) realm).setCacheManager(cacheManager);
}
}
}
}
看下AuthorizingRealm类关系图:
在不同层级的构造器中分别设置了是否启用权限缓存和身份验证缓存
this.authorizationCachingEnabled = true;
this.authenticationCachingEnabled = false;
再看CachingRealm,跟安全管理器的做法类似,并且为每个realm设置cacheManager的时候就已经触发了子类的相关操作:
public void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
afterCacheManagerSet();
}
AuthorizingRealm的实现就是通过cacheManager去获取权限相关的cache:
protected void afterCacheManagerSet() {
super.afterCacheManagerSet();
//trigger obtaining the authorization cache if possible
getAvailableAuthorizationCache();
}
如果我们没有明确配置cacheManager(作为securityManager的属性注入),那么此时是获取不到的,cache为null并且启用了权限缓存,现在就要临时构造一个:
cache = getAuthorizationCacheLazy();
- 获取AuthorizationInfo
以principals作为key取AuthorizationInfo:
info = cache.get(key);
if (info == null) {
info = doGetAuthorizationInfo(principals);
if (info != null && cache != null) {
Object key = getAuthorizationCacheKey(principals);
cache.put(key, info);
}
}
这个时候就转到我们自己的实现了,我们自己去获取权限,然后返回一个AuthorizationInfo,就是权限相关的信息。
3、总结
编程式很暴力也很直接,直接操作subject的相关方法来鉴权,其他两种方式拐弯抹角地也是操作的subject,然后再委托给securityManager。
具体是AuthorizingSecurityManager层实现的,它是直接new了一个ModularRealmAuthorizer,相关操作又转交给它,它又梳理一下,交给我们实现的realm(父类AuthorizingRealm层实现)。
ModularRealmAuthorizer是怎么获取到realm的?
也是我们给securityManager配置的,类似上面cacheManager的set方法,安全管理器用了很多这样的方法,给它本身注入相关属性时,就把相关联的set了。
AuthorizingSecurityManager这样实现的:
protected void afterRealmsSet() {
super.afterRealmsSet();
if (this.authorizer instanceof ModularRealmAuthorizer) {
((ModularRealmAuthorizer) this.authorizer).setRealms(getRealms());
}
}
从类的调用关系来看:
DelegatingSubject -> AuthorizingSecurityManager -> ModularRealmAuthorizer -> Realm
- 注解调用栈
RequiresRoles :checkRole -> checkRole -> checkRole -> hasRole
RequiresPermissions:checkPermission -> checkPermission -> checkPermission -> isPermitted
三、学到什
- Shiro作为框架是如何承上启下的,上接servlet规范,中搭spring顺风车,下给开发者自己实现。
- 实现的技巧
- 开发中有效的配置,哪些是必要的,哪些是默认的。
- 如何提升开发效率
例如在自己的XxRealm中:
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
if (MySubjectUtils.isPatformAdmin()) {
simpleAuthorizationInfo.addStringPermission("*");
} else {
//查询关联的权限adminUser:create:01001001
}
}
或者在自己的权限标签中:
public class HasAnyPermissionTag extends PermissionTag {
@Override
protected boolean showTagBody(String permissions) {
boolean hasPermission = false;
Subject subject = getSubject();
if(MySubjectUtils.isPatformAdmin()){
return true;
}
//......
}
}
这样作为开发者对于角色权限的配置可以省掉了,将特定id或者name的开发人员设置为平台管理员,可以坐拥天下,在功能不断完善的情况下不需要再补充权限。
Shiro的鉴权方式的更多相关文章
- web开发常见的鉴权方式
结合网上找的资料整理了一下,以下是web开发中常见的鉴权方法: 预备:一些基本的知识 RBAC(Role-Based Access Control)基于角色的权限访问控制(参考下面①的连接) l ...
- apigw鉴权分析(1-3)百度 AI - 鉴权方式分析
http://ai.baidu.com/docs#/Begin/top 一.访问入口 二.鉴权方式分析 1.鉴权认证方式一 - access_token - 针对HTTP API调用者 2.鉴权认证方 ...
- apigw鉴权分析(1-1)阿里数加 - 鉴权方式分析
一.访问方式 1.访问阿里云首页 https://www.aliyun.com/?utm_medium=text&utm_source=bdbrand&utm_campaign=bdb ...
- 【JMeter连接SQLServer】採用window鉴权方式连接(原创)
大家都知道Jmeter能够连接各种数据库.这方面我也不多说了,假设你还不知道怎么连接的话.能够參看我看的另外一篇博文.这边有具体的介绍 http://blog.csdn.net/lzqinfen/ar ...
- 一口气说出 OAuth2.0 的四种鉴权方式,面试官会高看一眼
本文收录在个人博客:www.chengxy-nds.top,技术资源共享,一起进步 上周我的自研开源项目开始破土动工了,<开源项目迈出第一步,10 选 1?页面模板成了第一个绊脚石 > , ...
- springcloud zuul shiro网关鉴权并向服务传递用户信息
1.pom文件 <dependencies> <!--eureka客户端--> <dependency> <groupId>org.springfram ...
- shiro jwt 构建无状态分布式鉴权体系
一:JWT 1.令牌构造 JWT(json web token)是可在网络上传输的用于声明某种主张的令牌(token),以JSON 对象为载体的轻量级开放标准(RFC 7519). 一个JWT令牌的定 ...
- .NET Core中的鉴权授权正确方式(.NET5)
一.简介 前后端分离的站点一般都会用jwt或IdentityServer4之类的生成token的方式进行登录鉴权.这里要说的是小项目没有做前后端分离的时站点登录授权的正确方式. 一.传统的授权方式 这 ...
- 搭建一个分布式MongoDB鉴权集群
今天休假在家,测试并搭建了一个replica set shard MongoDB鉴权集群.replica set shard 鉴权集群中文资料比较少,本文是个人笔记,同时也希望对后来者有所帮助.本文仅 ...
随机推荐
- Atcoder #017 agc017 A.Biscuits 简单数学
LINK 题意:水题 求取数,使得和为奇数或偶数的方案数. 思路:统计奇数和偶数,组合求一下发现结果就是$2^{odd-1} + 2^{eve-1}$ 注意特殊情况,即奇数个为0又要求和为奇数的方案数 ...
- 在外网使用ssh连接内网中的多台Linux服务器
最近因为要对全球工控机网络进行协议扫描,需要在实验室配置几台服务器,因为我们只有一个IP地址,所以是用路由器搭建了一个内网(拓扑结构如下图).但是这样做了之后无法在宿舍通过ssh直接连接服务器,因为那 ...
- 简易版jquery
最近写了一个简易版的jquery github地址:https://github.com/jiangzhenfei/Easy-Jquery 完成的方法: 1.$('#id') 2.extend扩展 ...
- ubuntu12.04 svn ssl错误
1,ubuntu12.04 svn ssl错误提示: OPTIONS of '<url>': SSL handshake failed: SSL error: Key usage viol ...
- C# 开发(创蓝253)手机短信验证码接口
创蓝253: https://www.253.com/ #region 获取手机验证码(创蓝253) /// <summary> /// 获取手机验证码(创蓝253) /// </s ...
- 设计模式之笔记--代理模式(Proxy)
代理模式(Proxy) 定义 代理模式(Proxy),为其他对象提供一种代理以控制对这个对象的访问. 类图 描述 Subject,定义了ConcreteSubject和Proxy的共用接口,这样就可以 ...
- php 全文搜索解决方法
全套解决方案 xunsearch 一.安装编译工具 yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-dev ...
- Tutorial 6: ViewSets & Routers
转载自:http://www.django-rest-framework.org/tutorial/6-viewsets-and-routers/ Tutorial 6: ViewSets & ...
- 6.Python3标准库--数学运算
''' 作为一种通用的变成语言,Python经常用来解决数学问题.它包含一些用于管理整数和浮点数的内置类型,这很适合完成一般应用中可能出现的基本数学运算. 而标准库中包含一些用于满足更高级需求的模块. ...
- 创建一个简单的Maven工程
Maven的工程结构如下图所示: 大致来看,Maven的工程结构如下: 在创建maven工程时,可以通过骨架创建,也可以不通过骨架创建. 我们先用idea通过骨架创建一个Maven工程. 配置pom. ...