Shiro笔记(三)授权
Shiro笔记(三)授权
一、授权方式
1.编程式:
Subject subject=SecurityUtils.getSubject();
if(subject.hasRole("root")){
//有权限
} else {
//无权限
}
2.注解式:
@RequiresRoles("admin")
public void hello(){
//有权限才执行
}
3.JSP标签:
<shiro:hasRole name="root"> <!--有权限-->
</shiro:hasRole>
二、授权
1.基于角色的访问控制
规则:用户名=密码,角色1,角色2......
shiro-role.ini文件:
[users]
tang=123,role1,role2
wang=321,role1
核心代码:
/**
* @author Tang Jiujia
* @since 2017-10-16
*/
public class RoleTest extends BaseTest{ @Test
public void testHasRole(){
login("src/main/shiro-role.ini","tang","123");
Subject subject= SecurityUtils.getSubject();
Assert.assertTrue(subject.hasRole("role1"));
Assert.assertTrue(subject.hasAllRoles(Arrays.asList("role1","role2")));
boolean[] hasRoles = subject.hasRoles(Arrays.asList("role1", "role2", "role3")); for (int i=0;i<3;i++){
if (hasRoles[i]==true){
System.out.println("We have role"+(i+1));
}else {
System.out.println("We don't have role"+(i+1));
}
}
}
}
//checkRole与前面的hasRole不同的地方在于判断为假时会抛UnauthorizedException
@Test(expected = UnauthorizedException.class)
public void testCheckRole(){
login("src/main/shiro-role.ini","tang","123");
Subject subject= SecurityUtils.getSubject();
subject.checkRole("role1");
subject.checkRoles("role1","role5");
}
2.基于资源的访问控制
规则:用户名=密码,角色1,角色2 角色=权限1,权限2
[users]
tang=123,role1,role2
wang=321,role2
[roles]
role1=root:create,root:add,root:update
role2=root:delete,root:update
核心代码:
/**
* @author Tang Jiujia
* @since 2017-10-16
*/
public class PermissionTest extends BaseTest{ @Test
public void testIsPermission(){
login("src/main/shiro-permission.ini","tang","123456");
Subject subject = SecurityUtils.getSubject();
if (subject.isPermitted("root:add1")) {
System.out.println("have add");
} else {
System.out.println("do not have add");
}
if (subject.isPermitted("root:update")) System.out.println("Have update permission");
} @Test
public void testCheckPermission(){
login("src/main/shiro-permission.ini","tang","123456");
Subject subject = SecurityUtils.getSubject();
subject.checkPermission("root:add");
}
}
Shiro笔记(三)授权的更多相关文章
- Shiro:学习笔记(2)——授权
Shiro:学习笔记(2)——授权 Shiro的三种授权方式 编程式: Subject subject = SecurityUtils.getSubject(); if(subject.hasRole ...
- Shiro第三篇【授权、自定义reaml授权】
Shiro授权 上一篇我们已经讲解了Shiro的认证相关的知识了,现在我们来弄Shiro的授权 Shiro授权的流程和认证的流程其实是差不多的: Shiro支持的授权方式 Shiro支持的授权方式有三 ...
- 构建高性能WEB站点笔记三
构建高性能WEB站点笔记三 第10章 分布式缓存 10.1数据库的前端缓存区 文件系统内核缓冲区,位于物理内存的内核地址空间,除了使用O_DIRECT标记打开的文件以外,所有对磁盘文件的读写操作都要经 ...
- Shiro中的授权问题(二)
上篇博客(Shiro中的授权问题 )我们介绍了Shiro中最最基本的授权问题,以及常见的权限字符的匹配问题.但是这里边还有许多细节需要我们继续介绍,本节我们就来看看Shiro中授权的一些细节问题. 验 ...
- Shiro中的授权问题
在初识Shiro一文中,我们对Shiro的基本使用已经做了简单的介绍,不懂的小伙伴们可以先阅读上文,今天我们就来看看Shiro中的授权问题. Shiro中的授权,大体上可以分为两大类,一类是隐式角色, ...
- 【shiro】(4)---Shiro认证、授权案例讲解
Shiro认证.授权案例讲解 一.认证 1. 认证流程 2.用户密码已经加密.加盐的用户认证 (1)测试类 // 用户登陆和退出,这里我自定了一个realm(开发肯定需要自定义realm获取 ...
- shiro源码篇 - shiro认证与授权,你值得拥有
前言 开心一刻 我和儿子有个共同的心愿,出国旅游.昨天儿子考试得了全班第一,我跟媳妇合计着带他出国见见世面,吃晚饭的时候,一家人开始了讨论这个.我:“儿子,你的心愿是什么?”,儿子:“吃汉堡包”,我: ...
- Shiro笔记(四)编码/加密
Shiro笔记(四)编码/加密 一.编码和解码 //base64编码.解码 @Test public void testBase64(){ String str="tang"; b ...
- Shiro笔记(二)身份验证
Shiro笔记(二)身份验证 一.核心代码 @Test public void helloWorldTest(){ IniSecurityManagerFactory factory = new In ...
随机推荐
- 移动端rem单位适配使用
1.适配方法 //缩放比例!function(e,t){function i(){o=1,e.devicePixelRatioValue=o,s=1/o;var t=a.createElement(& ...
- 【转】理解*(void**)
#include <stdio.h> int main() { int *p; ; unsigned ; p = &a; printf("%d\n", *p); ...
- mysql锁表与不锁表设置主从复制的方法
有时候MySQL主从同步不一致比较严重的时候,需要手动同步.先说说在锁表的情况下如何操作:以下是其简要过程 1.先对主库锁表FLUSH TABLES WITH READ LOCK; 2.备份数据mys ...
- tomcat jsp页面乱码解决
浏览器接收服务器响应的中文参数: JSP页面中告诉浏览器使用什么编码: <%@ page language="java" contentType="text/htm ...
- 028_rync和inotify实现实时备份
一.服务节点安装inotify-tools. 确保系统后以下输出=> [root@xxxx]# ll /proc/sys/fs/inotify/ total 0 -rw-r--r-- 1 roo ...
- openwrt 分区
下面以ar9344 16M flash为例子: uboot启动时传递给内核的参数为: bootargs=console=ttyS0,115200 root=31:02 rootfstype=jffs2 ...
- zabbix通过简单shell命令监控elasticsearch集群状态
简单命令监控elasticsearch集群状态 原理: 使用curl命令模拟访问任意一个es节点可以反馈的集群状态,集群的状态需要为green curl -sXGET http://serverip: ...
- zabbix客户端日志报错no active checks on server [192.168.3.108:10051]: host [192.168.3.108] not found
zabbix客户端日志报错: 45647:20160808:220507.717 no active checks on server [192.168.3.108:10051]: host [192 ...
- js 数组、对象转json 以及json转 数组、对象
1.JS对象转JSON 方式:JSON.stringify(obj) var json = {"name":"iphone","price" ...
- 步步为营-62-Excel的导入和导出
说明:NPOI组件的使用 1 添加引用 2 代码 using System; using System.Collections.Generic; using System.ComponentModel ...