一、shiro-permission.ini

shiro-permission.ini里面的内容相当于在数据库

#用户
[users]
#用户zhang的密码是123,此用户具有role1和role2两个角色
zhangsan=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("user:create:1");//不抛
subject.checkPermission("item:create:1");//抛异常 }

自定义realm进行授权

实际开发中从数据库中获取权限数据。就需要自定义realm,由realm从数据库查询权限数据。

realm根据用户身份查询权限数据,将权限数据返回给authorizer(授权器)。

public class CustomRealm extends AuthorizingRealm {

    // 设置realm的名称
@Override
public void setName(String name) {
super.setName("customRealm");
} // 用于认证
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken token) throws AuthenticationException { // token是用户输入的
// 第一步从token中取出身份信息
String userCode = (String) token.getPrincipal(); // 第二步:根据用户输入的userCode从数据库查询
// .... // 如果查询不到返回null
//数据库中用户账号是zhangsansan
/*if(!userCode.equals("zhangsansan")){//
return null;
}*/ // 模拟从数据库查询到密码
String password = "111111"; // 如果查询到返回认证信息AuthenticationInfo SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(
userCode, password, this.getName()); return simpleAuthenticationInfo;
} // 用于授权
@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;
} }

shiro-realm.ini

在shiro-realm.ini中配置自定义的realm,将realm设置到securityManager中。

[main]
#自定义 realm
customRealm=cn.itcast.shiro.realm.CustomRealm securityManager.realms=$customRealm

测试程序:

// 自定义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授权的更多相关文章

  1. Apache Shiro 使用手册(三)Shiro 授权

    授权即访问控制,它将判断用户在应用程序中对资源是否拥有相应的访问权限. 如,判断一个用户有查看页面的权限,编辑数据的权限,拥有某一按钮的权限,以及是否拥有打印的权限等等. 一.授权的三要素 授权有着三 ...

  2. Apache shiro集群实现 (四)shiro授权(Authentication)--访问控制

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  3. Shiro笔记(五)Shiro授权

    Shiro授权 也叫访问控制,即在应用中控制谁能访问那些资源(如访问页面.编辑数据.页面操作等).在授权中需要了解几个关键对象:主体(subject).资源(resource).权限(Permissi ...

  4. Shiro授权管理

    一.授权 授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等).在授权中需了解的几个关键对象:主体(Subject).资源(Resource).权限(Permissi ...

  5. 转:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法、shiro认证与shiro授权

    原文地址:JAVAWEB开发之权限管理(二)——shiro入门详解以及使用方法.shiro认证与shiro授权 以下是部分内容,具体见原文. shiro介绍 什么是shiro shiro是Apache ...

  6. shiro授权-记调试过程

    根据张开涛老师的shiro教程学习过程中 感觉shiro授权这块有点绕 调试了十几遍 大概有个思路  记录一下 1.单元测试入口 2.subject().isPermitted("+user ...

  7. frame shiro 授权及原理简述

    shiro 授权模式 shiro采用的是rbac授权模式rbac,基于角色的权限管理,谁扮演什么角色,被允许做什么事情. shiro 授权流程 shiro 授权方式 1.编程式 通过写if/else授 ...

  8. Apache Shiro 使用手册(三)Shiro 授权(转发:http://kdboy.iteye.com/blog/1155450)

    授权即访问控制,它将判断用户在应用程序中对资源是否拥有相应的访问权限. 如,判断一个用户有查看页面的权限,编辑数据的权限,拥有某一按钮的权限,以及是否拥有打印的权限等等. 一.授权的三要素 授权有着三 ...

  9. Shiro授权流程

    1,授权中涉及的一些概念      [1]授权:访问控制,即在应用中认证用户能否访问的系统资源(如一个页面,一个按钮等).      [2]资源:在Web应用中反应为用户可以访问的URL.       ...

  10. shiro授权+注解式开发

    shiro授权和注解式开发 1.shiro授权角色.权限 2.Shiro的注解式开发 ShiroUserMapper.xml <select id="getRolesByUserId& ...

随机推荐

  1. python高阶函数式编程

    from functools import reduce def str2int(s): def fn(x, y): return x * 10 + y def char2num(s): return ...

  2. Redis进阶实践之十四 Redis-cli命令行工具使用详解第一部分

    一.介绍       redis学了有一段时间了,以前都是看视频,看教程,很少看官方的东西.现在redis的东西要看的都差不多看完了.网上的东西也不多了.剩下来就看看官网的东西吧,一遍翻译,一遍测试. ...

  3. (2018干货系列三)最新PHP学习路线整合

    怎么学PHP PHP是一种通用开源脚本语言.语法吸收了C语言.Java和Perl的特点,利于学习,使用广泛,主要适用于Web开发领域. 菜鸟到大神,一步到位,正式开启干货模式: PHP初级开发工程师 ...

  4. 讯飞语音JavaWeb语音合成解决方案

    在线语音合成 将文字信息转化为声音信息,给应用配上"嘴巴".我们提供了众多极具特色的发音人(音库)供您选择.其合成音在音色.自然度等方面的表现均接近甚至超过了人声.这种语音合成体验 ...

  5. cookie、session和java过滤器

    基础知识理解: cookie.session和过滤器通常都是用在web应用中,cookie和session用来保存一定的数据,过滤器Filter则是在浏览器发出请求之后,而后台执行特定的请求之前发生一 ...

  6. R语言︱H2o深度学习的一些R语言实践——H2o包

    每每以为攀得众山小,可.每每又切实来到起点,大牛们,缓缓脚步来俺笔记葩分享一下吧,please~ --------------------------- R语言H2o包的几个应用案例 笔者寄语:受启发 ...

  7. 错误代码: 1248 Every derived table must have its own alias

    1.错误描述 1 queries executed, 0 success, 1 errors, 0 warnings 查询:SELECT stu_id, (SELECT stu_name FROM t ...

  8. struts2的配置文件

    struts2的配置文件 1.配置Action的struts.xml 2.配置Struts2有关属性的struts.properties

  9. TypeError:Error #1009

    1.错误描述 TypeError:Error #1009:无法访问空对象引用的属性和方法 2.错误原因 3.解决办法

  10. pat1111-1120

    1111 比较麻烦的最短路 #include<cmath> #include<map> #include<iostream> #include<cstring ...