Apache Shiro学习笔记(三)用户授权
2016-07-22 14:48
501 查看
鲁春利的工作笔记,好记性不如烂笔头
Shiro默认提供的Realm
![](http://s4.51cto.com/wyfs02/M00/84/7A/wKioL1eRwWaCzchkAAJXWZSp3ME654.jpg)
认证(Authentication)用来证明用户身份是合法的;而授权(Authorize)用来控制合法用户能够做什么(能访问哪些资源)。
实际系统应用中一般继承AuthorizingRealm(授权)即可;其继承了AuthenticatingRealm(即身份验证),而且也间接继承了CachingRealm(带有缓存实现)。
在授权中需了解的几个关键对象:主体(Subject)、资源(Resource)、权限(Permission)、角色(Role)。
Shiro的ini配置文件(shiro-authorize-permission.ini)
基于角色的访问控制
基于资源的访问控制
本文出自 “闷葫芦的世界” 博客,请务必保留此出处http://luchunli.blog.51cto.com/2368057/1828795
Shiro默认提供的Realm
![](http://s4.51cto.com/wyfs02/M00/84/7A/wKioL1eRwWaCzchkAAJXWZSp3ME654.jpg)
认证(Authentication)用来证明用户身份是合法的;而授权(Authorize)用来控制合法用户能够做什么(能访问哪些资源)。
实际系统应用中一般继承AuthorizingRealm(授权)即可;其继承了AuthenticatingRealm(即身份验证),而且也间接继承了CachingRealm(带有缓存实现)。
在授权中需了解的几个关键对象:主体(Subject)、资源(Resource)、权限(Permission)、角色(Role)。
Shiro的ini配置文件(shiro-authorize-permission.ini)
[main] # 定义变量 # 变量名=全类名 [users] # 用户名=密码,角色1,角色2,...,角色N lucl=123,role1,role2 zs=123,role1 [roles] # 角色=权限1,权限2,...,权限N role1=user:create,user:update role2=user:create,user:delete
基于角色的访问控制
/** * 基于角色的访问控制 */ @Test public void testWhetherHasRole () { // 1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro/authorize/shiro-authorize-permission.ini"); // 2、得到SecurityManager实例并绑定给SecurityUtils org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); // 3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证) Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken("lucl", "123"); try{ // 4、登录,即身份验证 subject.login(token); } catch (AuthenticationException e) { // 5、身份验证失败 logger.info("用户身份验证失败"); e.printStackTrace(); } // 用户身份得到确认 if (subject.isAuthenticated()) { logger.info("用户登录成功。"); /** * 进行权限判断 */ // 判断拥有角色:role1 Assert.assertTrue(subject.hasRole("role1")); // 判断拥有角色:role1 and role2 Assert.assertTrue(subject.hasAllRoles(Arrays.asList("role1", "role2"))); // 判断拥有角色:role1 and role2 and !role3 boolean[] result = subject.hasRoles(Arrays.asList("role1", "role2", "role3")); Assert.assertEquals(true, result[0]); Assert.assertEquals(true, result[1]); Assert.assertEquals(false, result[2]); // Shiro 提供了hasRole/hasRole 用于判断用户是否拥有某个角色/某些权限; // 但是没有提供如hashAnyRole用于判断是否有某些权限中的某一个。 // 断言拥有角色:role1 subject.checkRole("role1"); // 断言拥有角色:role1 and role3 失败抛出异常 subject.checkRoles("role1", "role3"); } else { logger.info("用户登录失败。"); } // 6、退出 subject.logout(); }
基于资源的访问控制
/** * 基于资源的访问控制 */ @Test public void testWhetherHasPermission () { // 1、获取SecurityManager工厂,此处使用Ini配置文件初始化SecurityManager Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro/authorize/shiro-authorize-permission.ini"); // 2、得到SecurityManager实例并绑定给SecurityUtils org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance(); SecurityUtils.setSecurityManager(securityManager); // 3、得到Subject及创建用户名/密码身份验证Token(即用户身份/凭证) Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken token = new UsernamePasswordToken("lucl", "123"); try{ // 4、登录,即身份验证 subject.login(token); } catch (AuthenticationException e) { // 5、身份验证失败 logger.info("用户身份验证失败"); e.printStackTrace(); } // 用户身份得到确认 if (subject.isAuthenticated()) { logger.info("用户登录成功。"); /** * 进行权限判断 */ // 判断拥有权限:user:create Assert.assertTrue(subject.isPermitted("user:create")); // 判断拥有权限:user:update and user:delete Assert.assertTrue(subject.isPermittedAll("user:update", "user:delete")); // 判断没有权限:user:view Assert.assertFalse(subject.isPermitted("user:view")); // 断言拥有权限:user:create subject.checkPermission("user:create"); // 断言拥有权限:user:delete and user:update subject.checkPermissions("user:delete", "user:update"); // 断言拥有权限:user:view 失败抛出异常 subject.checkPermissions("user:view"); } else { logger.info("用户登录失败。"); } // 6、退出 subject.logout(); }
本文出自 “闷葫芦的世界” 博客,请务必保留此出处http://luchunli.blog.51cto.com/2368057/1828795
相关文章推荐
- 搭建Apache+python+Flask环境
- zookeeper集群配置 Error: Could not find or load main class org.apache.solr.cloud.ZkCLI
- apache commons fileupload 安全漏洞(2016-07-01更新)
- The import org.apache.tools cannot be resolved 想必大家普遍遇到过吧。完美解决
- Centos6.6中Apache服务器的安装
- Apache上传漏洞(实验环境)
- Mac OS X中Apache开启ssl
- Ubuntu下Apache开启rewrite模块
- [李景山php]thinkphp核心源码注释|Apachenote.class.php
- Apache的配置文件,使用了自己的目录,添加了别名的使用
- Apache2.4虚拟主机+别名配置
- (转载)Windows 上搭建Apache FtpServer
- 【linux】ubantu下Apache无法启动(80端口被占用)
- 关于Xutils框架出现无法访问HttpRequestBase 找不到org.apache.http.client.methods.H...
- Apache POI的小测试
- Linux下安装Apache Openssl
- 二:apache的Qpid消息中间件介绍
- python 计算apache进程占用的内存大小以及占物理内存的比例
- ubuntu下apache2 SSL证书部署
- Apache Log4j配置说明