Apache shiro 笔记整理之编程式授权
下面内容是在看了涛哥的《跟我一起学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 笔记整理之编程式授权的更多相关文章
- Apache shiro 笔记整理之web整合一
下面内容是在看了涛哥的<跟我一起学shiro> 和 视频<一头扎入进shiro> 后整理出来备忘和方便自己和其它人学习. 个人主页:http://www.itit123.cn/ ...
- Shiro基础知识03----shiro授权(编程式授权),Permission详解,授权流程(zz)
授权,也叫访问控制,即在应用中控制谁能访问哪些资源(如访问页面/编辑数据/页面操作等). 在权限认证中,最核心的是:主体/用户(Subject).权限(Permission).角色(Role).资源 ...
- Shiro-权限认证(授权)-编程式授权
权限认证 权限认证也就是访问控制,即在应用中控制谁能访问哪些资源 权限认证核心要素 权限 : 即操作资源的权利,比如访问某个页面,以及对某个模块的数据的添加,修改,删除,查看的权利 角色 : 是权限的 ...
- 使用shiro做权限管理的学习笔记整理
Shiro权限管理 参考:https://www.cnblogs.com/jpfss/p/8352031.html Shiro解决的问题 授权和鉴别的问题:Authenrization(授权) Aut ...
- apache shiro内置过滤器 标签 注解
内置过滤器 anon(匿名) org.apache.shiro.web.filter.authc.AnonymousFilter authc(身份验证) org.apache.shiro ...
- Shiro笔记(三)授权
Shiro笔记(三)授权 一.授权方式 1.编程式: Subject subject=SecurityUtils.getSubject(); if(subject.hasRole("root ...
- Apache Shiro安全(权限框架)学习笔记二
课程目标 通过学习本课程掌握权限管理的设计思想及方法,使用Shiro框架完成权限管理功能开发. 1. 理解基于资源的权限管理方法. 2. 掌握权限管理的数据模型. 3. 掌握不使用shiro开发 ...
- Apache Shiro安全(权限框架)学习笔记一
1. 授权需要继承 AuthorizingRealm 类, 并实现其 doGetAuthorizationInfo 方法 2. AuthorizingRealm 类继承自 Authenticating ...
- Apache Shiro 使用手册(三)Shiro 授权
授权即访问控制,它将判断用户在应用程序中对资源是否拥有相应的访问权限. 如,判断一个用户有查看页面的权限,编辑数据的权限,拥有某一按钮的权限,以及是否拥有打印的权限等等. 一.授权的三要素 授权有着三 ...
随机推荐
- HDU 4046 Panda(树状数组)
Panda Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- intel dpdk在ubuntu12.04中測试testpmd、helloworld程序
一.測试环境 操作系统:ubuntu12.04 x86_64 dpdk版本号:1.6.0r2 虚拟机:vmware 10 网卡: Intel Corporation 82545EM Gigabit ...
- NAT&Port Forwarding&Port Triggering
NAT Nat,网络地址转换协议.主要功能是实现局域网内的本地主机与外网通信. 在连接外网时,内部Ip地址须要转换为网关(一般为路由器Ip地址)(port号也须要对应的转换) ...
- spring batch(二):核心部分(1):配置Spring batch
spring batch(二):核心部分(1):配置Spring batch 博客分类: Spring 经验 java chapter 3.Batch configuration 1.spring ...
- [Oracle] - Connect to a PDB of Oracle12c
Story about CDB and PDB Oracle12c has a new feature and definition of CDB and PDB. If you first use ...
- HDU 1113 Word Amalgamation (map 容器 + string容器)
http://acm.hdu.edu.cn/showproblem.php?pid=1113 Problem Description In millions of newspapers across ...
- win32 Service memory leak
https://stackoverflow.com/questions/2728578/how-to-get-phyiscal-path-of-windows-service-using-net ht ...
- 31.QT坐标系
dialog.h #ifndef DIALOG_H #define DIALOG_H #include <QDialog> #include <QLabel> #include ...
- 基于Redis实现分布式应用限流--转
原文地址:https://my.oschina.net/giegie/blog/1525931 摘要: 限流的目的是通过对并发访问/请求进行限速或者一个时间窗口内的的请求进行限速来保护系统,一旦达到限 ...
- 以SqlHelper为例论面向对象中封装的使用
引言: 在使用面向对象方法编写的程序中,会有一些工具类,如Utility,xxHelper等. 比如1)操作数据库的过程,一般步骤都是:1.准备数据库地址.表名等信息:2.建立连接:3.准备要执行sq ...