下面内容是在看了涛哥的《跟我一起学shiro》 和 视频《一头扎入进shiro》 后整理出来备忘和方便自己和其它人学习。

个人主页:http://www.itit123.cn/ 很多其它干货等你来拿

授权相关概念了解

权限认证:什么样的用户拥有什么样的权限做什么样的事。

三要素:权限,角色,用户。

角色:权限的集合。一个角色能够拥有多个权限

用户:角色的集合。一个用户能够拥有多个角色。也就是Subject

上代码:

为了方便函数的调用,将用户登录代码封装一下:

package com.shiro.utils;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.Factory; public class ShiroUtil { public static Subject login(String config , String userName ,String password){
// 1.读取配置文件,初始化SecurityManager工厂
Factory<SecurityManager> factory=new IniSecurityManagerFactory(config);
// 2.获取securityManager实例
SecurityManager securityManager=factory.getInstance();
// 3.把securityManager实例绑定到SecurityUtils
SecurityUtils.setSecurityManager(securityManager);
// 4.获取当前运行的用户
Subject currentUser=SecurityUtils.getSubject();
// 5.创建token令牌。username/密码
UsernamePasswordToken token=new UsernamePasswordToken(userName, password);
try{
// 6.登录时认证身份
currentUser.login(token);
System.out.println("身份认证成功! ");
}catch(AuthenticationException e){
e.printStackTrace();
System.out.println("身份认证失败!");
}
return currentUser;
} }

基于角色的訪问控制

在之前的基础上新建shiro_role.ini

[users]
ITDragon=123456,role1,role2
other=123456,role1
another=123456,role2

单元測试类:

hasRole拥有什么权限,checkRole检查有什么权限

// 基于角色
@Test
public void hasRoleTest(){
Subject user = ShiroUtil.login("classpath:shiro_role.ini", "other", "123456");
System.out.println(user.hasRole("role1")? "是role1角色":"不是role1角色");
// 測试时不能同一时候存在。由于另外一个还没有退出
//Subject user2 = ShiroUtil.login("classpath:shiro_role.ini", "ITDragon", "123456");
boolean[] result = user.hasRoles(Arrays.asList("role1","role2"));
for (boolean role : result) {
System.out.println(role);
}
System.out.println(user.hasAllRoles(Arrays.asList("role1","role2"))?"都拥有role1。role2角色":"不全拥有role1,role2角色");
user.logout();
} @Test
public void checkRoleTest(){
Subject user = ShiroUtil.login("classpath:shiro_role.ini", "ITDragon", "123456");
// 验证不通过报错
user.checkRole("role1");
user.checkRoles(Arrays.asList("role1","role2"));
user.checkRoles("role1","role2");
user.logout();
}

基于权限的訪问控制:

[users]
ITDragon=123456,role1,role2
other=123456,role1
another=123456,role2
[roles]
role1=user:select
role2=user:update,user:delete

单元測试类:

// 基于权限
@Test
public void isPermittedTest(){
Subject user = ShiroUtil.login("classpath:shiro_permission.ini", "other", "123456");
System.out.println(user.isPermitted("user:select")?"拥有select权限":"没有select权限");
boolean[] result = user.isPermitted("user:select","user:update","user:delete");
for (boolean role : result) {
System.out.println(role);
}
System.out.println(user.isPermittedAll("user:select","user:update")?"都拥有select,updata权限":"不全拥有select,updata权限");
user.logout();
} @Test
public void checkPermittedTest(){
Subject user = ShiroUtil.login("classpath:shiro_permission.ini", "other", "123456");
user.checkPermission("user:select");
user.checkPermissions("user:select","user:update","user:delete");
user.logout();
}

犯的错误:

在一个測试方法中登入了两个用户,并没有做用户退出操作。影响:权限推断错误。

Apache shiro 笔记整理之编程式授权的更多相关文章

  1. Apache shiro 笔记整理之web整合一

    下面内容是在看了涛哥的<跟我一起学shiro> 和 视频<一头扎入进shiro> 后整理出来备忘和方便自己和其它人学习. 个人主页:http://www.itit123.cn/ ...

  2. Shiro基础知识03----shiro授权(编程式授权),Permission详解,授权流程(zz)

    授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等).  在权限认证中,最核心的是:主体/用户(Subject).权限(Permission).角色(Role).资源 ...

  3. Shiro-权限认证(授权)-编程式授权

    权限认证 权限认证也就是访问控制,即在应用中控制谁能访问哪些资源 权限认证核心要素 权限 : 即操作资源的权利,比如访问某个页面,以及对某个模块的数据的添加,修改,删除,查看的权利 角色 : 是权限的 ...

  4. 使用shiro做权限管理的学习笔记整理

    Shiro权限管理 参考:https://www.cnblogs.com/jpfss/p/8352031.html Shiro解决的问题 授权和鉴别的问题:Authenrization(授权) Aut ...

  5. apache shiro内置过滤器 标签 注解

    内置过滤器 anon(匿名)  org.apache.shiro.web.filter.authc.AnonymousFilter authc(身份验证)       org.apache.shiro ...

  6. Shiro笔记(三)授权

    Shiro笔记(三)授权 一.授权方式 1.编程式: Subject subject=SecurityUtils.getSubject(); if(subject.hasRole("root ...

  7. Apache Shiro安全(权限框架)学习笔记二

    课程目标 通过学习本课程掌握权限管理的设计思想及方法,使用Shiro框架完成权限管理功能开发. 1.  理解基于资源的权限管理方法. 2.  掌握权限管理的数据模型. 3.  掌握不使用shiro开发 ...

  8. Apache Shiro安全(权限框架)学习笔记一

    1. 授权需要继承 AuthorizingRealm 类, 并实现其 doGetAuthorizationInfo 方法 2. AuthorizingRealm 类继承自 Authenticating ...

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

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

随机推荐

  1. ZOJ 2913 Bus Pass (近期的最远BFS HDU2377)

    题意  在全部城市中找一个中心满足这个中心到全部公交网站距离的最大值最小 输出最小距离和满足最小距离编号最小的中心 最基础的BFS  对每一个公交网站BFS  dis[i]表示编号为i的点到全部公交网 ...

  2. 2015.04.30,外语,读书笔记-《Word Power Made Easy》 14 “如何谈论日常现象” SESSION 40

    1. money, and what it will buy penury(['penjuri] n. 贫穷,拮据),来自拉丁词语penuria(need,needness的意思),主要指缺乏财富资源 ...

  3. Android::开机自启动C程序【转】

    本文转载自:http://blog.csdn.net/Kaiwii/article/details/7681736 之前一篇博文介绍了shell脚本文件的开机启动,地址是http://blog.chi ...

  4. 求区间连续不超过K段的最大和--线段树+大量代码

    题目描述: 这是一道数据结构题. 我们拥有一个长度为n的数组a[i]. 我们有m次操作.操作有两种类型: 0 i val:表示我们要把a[i]修改为val; 1 l r k:表示我们要求出区间[l,r ...

  5. Swift - 使用CollectionView实现图片Gallery画廊效果(左右滑动浏览图片)

    1,效果图 (1)图片从左至右横向排列(只有一行),通过手指拖动可以前后浏览图片. (2)视图滚动时,每张图片根据其与屏幕中心距离的不同,显示尺寸也会相应地变化.越靠近屏幕中心尺寸就越大,远离屏幕中心 ...

  6. 9. Palindrome Number[E]回文数

    题目 Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same b ...

  7. BZOJ 3796 后缀数组+KMP

    思路: 写得我头脑发蒙,,, 旁边还有俩唱歌的 抓狂 (感谢lh大爷查错) 首先 1.w是s1的子串 2.w是s2的子串 这两步很好办啊~ 后缀数组一下O(n)就可以搞 重点是 这个:3.s3不是w的 ...

  8. ES6 | ES6新语法 在编码实践中的应用

    本章探讨如何将 ES6 的新语法,运用到编码实践之中,与传统的 JavaScript 语法结合在一起,写出合理的.易于阅读和维护的代码. 多家公司和组织已经公开了它们的风格规范,本文的内容主要参考了  ...

  9. 自定义view 之多个引导层动画效果

    SupernatantView 如果我英文还可以的话这个应该叫做漂浮在上层的view---引导层 今天闲来无事看了网上的一些引导层案例总感觉如果不是很舒服,就是类似于很死板的显示和消失 我在想能不能弄 ...

  10. 编程范式(Programming Paradigm)-[ 程序员的编程世界观 ]

    编程范式(Programming Paradigm)是某种编程语言典型的编程风格或者说是编程方式.随着编程方法学和软件工程研究的深入,特别是OO思想的普及,范式(Paradigm)以及编程范式等术语渐 ...