鲁春利的工作笔记,好记性不如烂笔头
Shiro默认提供的Realm
认证(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
原文地址:http://luchunli.blog.51cto.com/2368057/1828795