shiro学习(二)---授权入门程序
来源:互联网 发布:it helpdesk 工作内容 编辑:程序博客网 时间:2024/05/18 01:14
1、创建shiro-permission.ini文件
#用户[users]#用户张三的密码是123,此用户具有role1和role2两个角色zhangsan=123,role1,role2wang=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
2、创建AuthorizationTest测试类
public class AuthorizationTest { // 角色授权、资源授权测试 @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"); }}
3、进行Junit Test
阅读全文
0 0
- shiro学习(二)---授权入门程序
- shiro授权入门程序
- shiro授权入门程序
- Shiro入门学习二
- Shiro入门学习二
- Shiro学习总结(二)--Shiro的入门小例子
- Shiro学习(3)授权
- Shiro学习(3)授权
- shiro学习(一)---认证入门程序
- Shiro 学习记录 Shiro 入门程序
- shiro身份验证授权入门
- Shiro入门-授权初步
- 五.shiro授权入门
- Shiro入门—授权
- 深入浅出学Shiro(二)--授权认证
- shiro学习:shiro实现授权
- Apache Shiro学习(三)授权
- shiro身份验证和授权入门
- PyQt4-入门学习(1)
- 对缓存技术(redis)的理解
- The Beginning Of All
- faster rcnn demo.py:在一个窗口显示所有类别标注
- 【技术重温】引言
- shiro学习(二)---授权入门程序
- 右键的那些事
- 对rbac理解笔记
- (一)安装Scrapy库出现的问题及解决方法
- 数据库事务的隔离机制和四大特性
- Delphi编写Android程序中ListView自适应屏幕宽度设置
- 浅谈 PHP类的自动载入
- TCP/IP四层模型深入理解
- 白盒测试----六种覆盖方法