http://blog.csdn.net/facekbook/article/details/54910606

本文介绍

  • 授权流程
  • 授权方式
  • 授权测试
  • 自定义授权realm

授权流程

开始构造SecurityManager环境subject.isPermitted()授权securityManager.isPermitted()执行授权Authorizer执行授权Realm根据身份获取资源权限信息结束

授权方式

Shiro支持三种方式的授权:

  • 编程式:通过写if/else授权代码块完成。
Subject subject = SecurityUtils.getSubject();
if(subject.hasRole("admin")){
//有权限
}else{
//没有权限
}
  • 注解式:通过在执行的Java方法上放置相应的注解完成。
@RequiresRole("admin")
public void hell(){
//有权限
}
  • JSP标签:在Jsp页面通过相应的标签完成:
<shiro:hasRole name="admin">
<!--有权限-->
</shiro:hasRole>

授权测试

创建ini配置文件

在classpath路径下创建shiro-permission.ini文件,内容如下:

[users]
#用户zhangsan的密码是123,此用户具有role1和role2两个角色
zhangsan=123,role1,role2
lisi=123,role2 [roles]
#角色role1对资源user拥有create、update权限
role1=user:create,user:update
#角色role2对资源user拥有create、delete权限
role2=user:create,user.delete
#角色role3对资源user拥有create权限
role3=user:create

在ini文件中用户、角色、权限的配置规则是: 用户名=密码,角色1,角色2 、角色=权限1,权限2。首先根据用户找角色,再根据角色找权限,角色是权限集合。

权限字符串规则

权限字符串的规则是:”资源标识符:操作:资源实例标识符”,意思是对哪个资源的哪个实例具有什么操作,”:”是资源/操作/实例的分割符,权限字符串也可以使用*通配符。 
例子: 
用户创建权限:user:create或user:create:* 
用户修改实例001的权限:user:update:001 
用户实例001的所有权限:user:*:001

测试代码

    @Test
public void testPermission() {
// 构建SecurityManager工厂,IniSecurityManagerFactory可以从ini文件中初始化SecurityManager环境
Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-permission.ini");
// 通过工厂创建SecurityManager
SecurityManager securityManager = factory.getInstance();
// 将SecurityManager设置到运行环境中
SecurityUtils.setSecurityManager(securityManager);
//创建一个Subject实例,该实例认证需要使用上面创建的SecurityManager
Subject subject = SecurityUtils.getSubject();
//创建token令牌,账号和密码是ini文件中配置的
//AuthenticationToken token = new UsernamePasswordToken("zhangsan", "123");//账号密码正确token 角色是 role1,role2
AuthenticationToken token = new UsernamePasswordToken("lisi", "123");//lisi账号的token 角色是role3
/**
* role1=user:create,user:update
role2=user:create,user.delete
role3=user:create
*/
try {
//用户登录
subject.login(token);
} catch (AuthenticationException e) {
e.printStackTrace();
}
//用户认证状态
Boolean isAuthenticated = subject.isAuthenticated();
System.out.println("用户认证状态:"+isAuthenticated);//输出true //用户授权检测
//是否拥有一个角色
System.out.println("是否拥有一个角色:"+subject.hasRole("role1"));
//是否拥有多个角色
System.out.println("是否拥有多个角色:"+subject.hasAllRoles(Arrays.asList("role1","role2"))); //检测权限
try {
subject.checkPermission("user:create");
} catch (AuthorizationException e) {
e.printStackTrace();
}
try {
subject.checkPermissions("user:create","user.delete");
} catch (AuthorizationException e) {
e.printStackTrace();
}
}

基于角色的授权

//是否拥有一个角色
System.out.println("是否拥有一个角色:"+subject.hasRole("role1"));
//是否拥有多个角色
System.out.println("是否拥有多个角色:"+subject.hasAllRoles(Arrays.asList("role1","role2")));

对应的check方法:

subject.checkRole("role1");
subject.checkRoles(Arrays.asList("role1","role2"));

上边check方法如果授权失败则抛出异常:

org.apache.shiro.authz.UnauthorizedException: Subject does not have role [.....]

基于资源授权

System.out.println("是否拥有某个权限:"+subject.isPermitted("user:create"));
System.out.println("是否拥有多个权限:"+subject.isPermittedAll("user:create","user.delete"));

对应的check方法:

subject.checkPermission("user:create");
subject.checkPermissions("user:create","user.delete");

上边check方法如果授权失败则抛出异常:

org.apache.shiro.authz.UnauthorizedException: Subject does not have permission [....]

自定义realm

shiro认证那篇博客中编写的自定义realm类中有一个doGetAuthorizationInfo 方法,此方法需要完成:根据用户身份信息从数据库查询权限字符串,由shiro进行授权。

realm代码

/**
* 授权方法
*/
@Override
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
//后去身份信息,这个字段就是前面的username
String username = (String)principals.getPrimaryPrincipal(); //模拟通过身份信息获取所有权限
List<String> permissions = new ArrayList<>();
permissions.add("user:create");
permissions.add("user:delete"); //将权限信息封装到AuthorizationInfo中,并返回
SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
authorizationInfo.addStringPermissions(permissions);
return authorizationInfo;
}

shiro-realm.ini文件

需要使用这个配置文件。

测试代码

同上边的授权测试代码,注意修改ini地址为shiro-realm.ini。

授权执行流程

1、 执行subject.isPermitted(“user:create”) 
2、 securityManager通过ModularRealmAuthorizer进行授权 
3、 ModularRealmAuthorizer调用realm获取权限信息 
4、 ModularRealmAuthorizer再通过permissionResolver解析权限字符串,校验是否匹配

该文章涉及的代码

代码地址

(转)shiro权限框架详解05-shiro授权的更多相关文章

  1. (转) shiro权限框架详解06-shiro与web项目整合(上)

    http://blog.csdn.net/facekbook/article/details/54947730 shiro和web项目整合,实现类似真实项目的应用 本文中使用的项目架构是springM ...

  2. (转)shiro权限框架详解03-shiro介绍

    http://blog.csdn.net/facekbook/article/details/54893740 shiro介绍 本文正式进入主题.本文将介绍如下内容: 什么是shiro 为什么需要学习 ...

  3. (转)shiro权限框架详解06-shiro与web项目整合(下)

    http://blog.csdn.net/facekbook/article/details/54962975 shiro和web项目整合,实现类似真实项目的应用 web项目中认证 web项目中授权 ...

  4. (转)shiro权限框架详解02-权限理论介绍

    http://blog.csdn.net/facekbook/article/details/54893042 权限管理解决方案 本文主要介绍权限管理的解决方法: 粗颗粒度和细颗粒度 基于url拦截 ...

  5. (转) shiro权限框架详解04-shiro认证

    http://blog.csdn.net/facekbook/article/details/54906635 shiro认证 本文介绍shiro的认证功能 认证流程 入门程序(用户登录和退出) 自定 ...

  6. (转)shiro权限框架详解01-权限理论介绍

    http://blog.csdn.net/facekbook/article/details/54890365 权限管理 本文介绍权限管理的理论和权限管理的一些名词. 介绍权限管理 理解身份认证和授权 ...

  7. Shiro 安全框架详解二(概念+权限案例实现)

    Shiro 安全框架详解二 总结内容 一.登录认证 二.Shiro 授权 1. 概念 2. 授权流程图 三.基于 ini 的授权认证案例实现 1. 实现原理图 2. 实现代码 2.1 添加 maven ...

  8. Shiro 安全框架详解一(概念+登录案例实现)

    shiro 安全框架详细教程 总结内容 一.RBAC 的概念 二.两种常用的权限管理框架 1. Apache Shiro 2. Spring Security 3. Shiro 和 Spring Se ...

  9. Shiro权限管理框架详解

    1 权限管理1.1 什么是权限管理 基本上涉及到用户参与的系统都要进行权限管理,权限管理属于系统安全的范畴,权限管理实现对用户访问系统的控制,按照安全规则或者安全策略控制用户可以访问而且只能访问自己被 ...

随机推荐

  1. let、var、const用法区别

    1.var var 声明的变量为全局变量,并会进行变量提升:也可以只声明变量而不进行赋值,输出为undefined,以下写法都是合法的. var a var a = 123  2.let let 声明 ...

  2. [luogu1627 CQOI2009] 中位数 (乱搞)

    传送门 Solution 好水的题(ーー;) Code //By Menteur_Hxy #include <map> #include <queue> #include &l ...

  3. 只允许一个 <configSections> 元素。它必须是根 <configuration> 元素的第一个子元素- HTTP Error 500.19

    这还是我第一次遇到这个错误,以前都没太注意配置文件中元素的放置顺序.这次在调试一个ASP.NET MVC项目的时候,突然就爆出HTTP Error 500.19错误,提示无法访问请求的页面,因为该页的 ...

  4. 【例题 4-4 uva 213】Message Decoding

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 输入的二进制长度最长为7 所以得开个sta[7][2^7]的样子才存的下所有的字符的.. 定义这么一个数组当字典. 然后一个字符一个 ...

  5. (16)Spring Boot使用Druid(编程注入)【从零开始学Spring Boot】

    在上一节使用是配置文件的方式进行使用druid,这里在扩散下使用编程式进行使用Druid,在上一节我们新建了一个类:DruidConfiguration我在这个类进行编码: package com.k ...

  6. fzu 2138

    //假设n个人每个人都做对了两道题,那么要想获奖人数最少的话,那么做题数目肯定最多即全做对的,中间可能会小于零那么没有获奖的 #include<stdio.h> int main() { ...

  7. Java EE: XML Schemas for Java EE Deployment Descriptors(Java Web的web.xml头web-app标签上的XML模式)

    继上几篇文章 http://www.cnblogs.com/EasonJim/p/6221952.html http://www.cnblogs.com/EasonJim/p/6959120.html ...

  8. Maven使用package打包Spring Boot时出现:Unable to find a single main class from the following candidates的问题解决

    问题如下: [ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.3.5.RELEASE ...

  9. RabbitMQ发布订阅实战-实现延时重试队列

    RabbitMQ是一款使用Erlang开发的开源消息队列.本文假设读者对RabbitMQ是什么已经有了基本的了解,如果你还不知道它是什么以及可以用来做什么,建议先从官网的 RabbitMQ Tutor ...

  10. [LeetCode]Wildcard Matching 通配符匹配(贪心)

    一開始採用递归写.TLE. class Solution { public: bool flag; int n,m; void dfs(int id0,const char *s,int id1,co ...