Shiro授权

上一篇我们已经讲解了Shiro的认证相关的知识了,现在我们来弄Shiro的授权

Shiro授权的流程和认证的流程其实是差不多的:

Shiro支持的授权方式

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>

使用编程式授权

同样的,我们是通过安全管理器来去授权的,因此我们还是需要配置对应的配置文件的:

shiro-permission.ini配置文件:


#用户
[users]
#用户zhang的密码是123,此用户具有role1和role2两个角色
zhang=123,role1,role2
wang=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 权限标识符号规则:**资源:操作:实例(中间使用半角:分隔)**
user:create:01 表示对用户资源的01实例进行create操作。
user:create:表示对用户资源进行create操作,相当于user:create:*,对所有用户资源实例进行create操作。
user:*:01 表示对用户资源实例01进行所有操作。

代码测试:



    // 角色授权、资源授权测试
@Test
public void testAuthorization() { // 创建SecurityManager工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory(
"classpath:shiro-permission.ini"); // 创建SecurityManager
SecurityManager securityManager = factory.getInstance(); // 将SecurityManager设置到系统运行环境,和spring后将SecurityManager配置spring容器中,一般单例管理
SecurityUtils.setSecurityManager(securityManager); // 创建subject
Subject subject = SecurityUtils.getSubject(); // 创建token令牌
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan",
"123"); // 执行认证
try {
subject.login(token);
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} System.out.println("认证状态:" + subject.isAuthenticated());
// 认证通过后执行授权 // 基于角色的授权
// hasRole传入角色标识
boolean ishasRole = subject.hasRole("role1");
System.out.println("单个角色判断" + ishasRole);
// hasAllRoles是否拥有多个角色
boolean hasAllRoles = subject.hasAllRoles(Arrays.asList("role1",
"role2", "role3"));
System.out.println("多个角色判断" + hasAllRoles); // 使用check方法进行授权,如果授权不通过会抛出异常
// subject.checkRole("role13"); // 基于资源的授权
// isPermitted传入权限标识符
boolean isPermitted = subject.isPermitted("user:create:1");
System.out.println("单个权限判断" + isPermitted); boolean isPermittedAll = subject.isPermittedAll("user:create:1",
"user:delete");
System.out.println("多个权限判断" + isPermittedAll);
// 使用check方法进行授权,如果授权不通过会抛出异常
subject.checkPermission("items:create:1"); }

自定义realm进行授权

一般地,我们的权限都是从数据库中查询的,并不是根据我们的配置文件来进行配对的。因此我们需要自定义reaml,让reaml去对比的是数据库查询出来的权限

shiro-realm.ini配置文件:将自定义的reaml信息注入到安全管理器中


[main]
#自定义 realm
customRealm=cn.itcast.shiro.realm.CustomRealm
#将realm设置到securityManager,相当 于spring中注入
securityManager.realms=$customRealm

我们上次已经使用过了一个自定义reaml,当时候仅仅重写了doGetAuthenticationInfo()方法,这次我们重写doGetAuthorizationInfo()方法

    // 用于授权
@Override
protected AuthorizationInfo doGetAuthorizationInfo(
PrincipalCollection principals) { //从 principals获取主身份信息
//将getPrimaryPrincipal方法返回值转为真实身份类型(在上边的doGetAuthenticationInfo认证通过填充到SimpleAuthenticationInfo中身份类型),
String userCode = (String) principals.getPrimaryPrincipal(); //根据身份信息获取权限信息
//连接数据库...
//模拟从数据库获取到数据
List<String> permissions = new ArrayList<String>();
permissions.add("user:create");//用户的创建
permissions.add("items:add");//商品添加权限
//.... //查到权限数据,返回授权信息(要包括 上边的permissions)
SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//将上边查询到授权信息填充到simpleAuthorizationInfo对象中
simpleAuthorizationInfo.addStringPermissions(permissions); return simpleAuthorizationInfo;
}

测试程序:


// 自定义realm进行资源授权测试
@Test
public void testAuthorizationCustomRealm() { // 创建SecurityManager工厂
Factory<SecurityManager> factory = new IniSecurityManagerFactory(
"classpath:shiro-realm.ini");
// 创建SecurityManager
SecurityManager securityManager = factory.getInstance();
// 将SecurityManager设置到系统运行环境,和spring后将SecurityManager配置spring容器中,一般单例管理
SecurityUtils.setSecurityManager(securityManager);
// 创建subject
Subject subject = SecurityUtils.getSubject(); // 创建token令牌
UsernamePasswordToken token = new UsernamePasswordToken("zhangsan",
"111111");
// 执行认证
try {
subject.login(token);
} catch (AuthenticationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} System.out.println("认证状态:" + subject.isAuthenticated());
// 认证通过后执行授权 // 基于资源的授权,调用isPermitted方法会调用CustomRealm从数据库查询正确权限数据
// isPermitted传入权限标识符,判断user:create:1是否在CustomRealm查询到权限数据之内
boolean isPermitted = subject.isPermitted("user:create:1");
System.out.println("单个权限判断" + isPermitted); boolean isPermittedAll = subject.isPermittedAll("user:create:1",
"user:create");
System.out.println("多个权限判断" + isPermittedAll); // 使用check方法进行授权,如果授权不通过会抛出异常
subject.checkPermission("items:add:1"); }


Shiro第三篇【授权、自定义reaml授权】的更多相关文章

  1. Qt学习之路:自定义Model三篇,自定义委托等等

    http://devbean.blog.51cto.com/448512/d-8/p-2

  2. 【.NET Core项目实战-统一认证平台】第六章 网关篇-自定义客户端授权

    [.NET Core项目实战-统一认证平台]开篇及目录索引 上篇文章我们介绍了网关使用Redis进行缓存,并介绍了如何进行缓存实现,缓存信息清理接口的使用.本篇我们将介绍如何实现网关自定义客户端授权, ...

  3. shiro源码篇 - shiro认证与授权,你值得拥有

    前言 开心一刻 我和儿子有个共同的心愿,出国旅游.昨天儿子考试得了全班第一,我跟媳妇合计着带他出国见见世面,吃晚饭的时候,一家人开始了讨论这个.我:“儿子,你的心愿是什么?”,儿子:“吃汉堡包”,我: ...

  4. shiro授权及自定义realm授权(七)

    1.授权流程

  5. Shiro第五篇【授权过滤、注解、JSP标签方式、与ehcache整合】

    授权过滤器测试 我们的授权过滤器使用的是permissionsAuthorizationFilter来进行拦截.我们可以在application-shiro中配置filter规则 <!--商品查 ...

  6. Shiro集成web环境[Springboot]-认证与授权

    Shiro集成web环境[Springboot]--认证与授权 在登录页面提交登陆数据后,发起请求也被ShiroFilter拦截,状态码为302 <form action="${pag ...

  7. java:shiro(认证,赋予角色,授权...)

    1.shiro(权限框架(认证,赋予角色,授权...)): readme.txt(运行机制): 1.从jsp的form中的action属性跳转到springmvc的Handler中(controlle ...

  8. openstack外篇之认识mysql授权及一些操作

    openstack外篇之认识mysql授权及一些操作  http://www.aboutyun.com/thread-11405-1-1.html

  9. 自定义View6 -塔防小游戏:第三篇防御塔随意放置+多组野怪

    第一篇:一个防御塔+多个野怪(简易版)第二篇:防御塔随意放置第三篇:防御塔随意放置+多组野怪 1.动态addView防御塔 2.防御塔放置后不可以移动 3.弯曲道路 4.素材替换 第四篇:多波野怪 第 ...

随机推荐

  1. 提升tomcat服务器性能的七条经验

    在线上环境中我们是采用了tomcat作为Web服务器,它的处理性能直接关系到用户体验,在平时的工作和学习中,归纳出以下七种调优经验. 1. 服务器资源 服务器所能提供CPU.内存.硬盘的性能对处理能力 ...

  2. Nodejs 复制文件/文件夹

    1.使用fs复制文件 使用fs对象的readFileSync,writeFileSync方法进行读写操作复制 cp.js function copyIt(from, to) { fs.writeFil ...

  3. 《NoSQL精粹》读书笔记

    NoSQL数据库数据模型的一般分类: 1. 键值数据模型 2. 文档数据模型 3. 列族数据模型 4. 图数据模型 常见NoSQL数据库: Redis, Cassandra, MongoDB, Neo ...

  4. C语言指针(三)指针传递给函数

    实例1:传递一个无符号的long型指针给该函数 #include<stdio.h>#include<time.h>void getSeconds(unsignedlong*pa ...

  5. jQuery ajax error函数(交互错误信息的获取)

    一般error函数返回的参数有三个: function(jqXHR jqXHR, String textStatus, String errorThrown).常见调用代码如下: $.ajax({ u ...

  6. Linux Command Line learning

    https://www.codecademy.com/en/courses/learn-the-command-line Background The command line is a text i ...

  7. web安全:QQ号快速登录漏洞及被盗原理

    为什么你什么都没干,但QQ空间中却发了很多小广告?也许你的QQ账号已经被盗.本文将讲解一个QQ的快速登录的漏洞. 我前阵子在论坛上看到一个QQ的快速登录的漏洞,觉得非常不错,所以把部分原文给转到园子来 ...

  8. VS2013禁用Browser Link

    禁用原因 VS2013新增的Browser Link功能虽然“强大”,但我并不需要. 但默认是开启的,会在页面中自动添加如下的代码,查看AJAX时造成很大的干扰. <!-- Visual Stu ...

  9. 再起航,我的学习笔记之JavaScript设计模式18(观察者模式)

    观察者模式 观察者模式(Observer): 又被称为发布-订阅者模式或消息机制,定义了一种依赖关系,解决了主体对象与观察者之间功能的耦合. 创建一个观察者对象 首先我们创建一个闭包对象,让其在页面加 ...

  10. 微信iOS收款到账语音提醒开发总结

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/yYCaPMxHGT9LyRyAPewVWQ 作者:l ...