Shiro 笔记 实现role permission验证
maven配置:添加shiro相关的包,以及jdbc相关的包,junit包
-
<dependency>
-
<groupId>junit </groupId>
-
<artifactId>junit </artifactId>
-
<version>4.12 </version>
-
</dependency>
添加log4j.properties
配置ShiroUtil工具类
-
public static Subject login(String configFile,String userName,String password){
-
// 读取配置文件,初始化SecurityManager工厂
-
Factory <SecurityManager> factory=new IniSecurityManagerFactory(configFile);
-
// 获取securityManager实例
-
SecurityManager securityManager=factory.getInstance();
-
// 把securityManager实例绑定到SecurityUtils
-
SecurityUtils.setSecurityManager(securityManager);
-
// 得到当前执行的用户
-
Subject currentUser=SecurityUtils.getSubject();
-
// 创建token令牌,用户名/密码
-
UsernamePasswordToken token=new UsernamePasswordToken(userName, password);
-
try{
-
// 身份认证
-
currentUser.login(token);
-
System.out.println("身份认证成功!");
-
}catch(AuthenticationException e){
-
e.printStackTrace();
-
System.out.println("身份认证失败!");
-
}
-
return currentUser;
-
}
shiro_role.ini
-
[users]
-
java1234=123456,role1,role2
-
jack=123,role1
RoleTest测试类
-
@Test
-
public void testHasRole() {
-
Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "java1234", "123456");
-
// Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "jack", "123");
-
System.out.println(currentUser.hasRole("role1")?"有role1这个角色":"没有role1这个角色");
-
boolean []results=currentUser.hasRoles(Arrays.asList("role1","role2","role3"));
-
System.out.println(results[0]?"有role1这个角色":"没有role1这个角色");
-
System.out.println(results[1]?"有role2这个角色":"没有role2这个角色");
-
System.out.println(results[2]?"有role3这个角色":"没有role3这个角色");
-
System.out.println(currentUser.hasAllRoles(Arrays.asList("role1","role2"))?"role1,role2这两个角色都有":"role1,role2这个两个角色不全有");
-
-
currentUser.logout();
-
}
-
-
@Test
-
public void testCheckRole() {
-
Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "java1234", "123456");
-
// Subject currentUser=ShiroUtil.login("classpath:shiro_role.ini", "jack", "123");
-
currentUser.checkRole("role1");
-
currentUser.checkRoles(Arrays.asList("role1","role2"));
-
currentUser.checkRoles("role1","role2","role3");
-
-
currentUser.logout();
-
}
shiro_permission.ini配置
-
[users]
-
java1234=123456,role1,role2
-
jack=123,role1
-
[roles]
-
role1=user:select
-
role2=user:add,user:update,user:delete
PermissionTest测试类
-
@Test
-
public void testIsPermitted() {
-
Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "java1234", "123456");
-
// Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "jack", "123");
-
System.out.println(currentUser.isPermitted("user:select")?"有user:select这个权限":"没有user:select这个权限");
-
System.out.println(currentUser.isPermitted("user:update")?"有user:update这个权限":"没有user:update这个权限");
-
boolean results[]=currentUser.isPermitted("user:select","user:update","user:delete");
-
System.out.println(results[0]?"有user:select这个权限":"没有user:select这个权限");
-
System.out.println(results[1]?"有user:update这个权限":"没有user:update这个权限");
-
System.out.println(results[2]?"有user:delete这个权限":"没有user:delete这个权限");
-
System.out.println(currentUser.isPermittedAll("user:select","user:update")?"有user:select,update这两个权限":"user:select,update这两个权限不全有");
-
-
currentUser.logout();
-
}
-
-
@Test
-
public void testCheckPermitted() {
-
Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "java1234", "123456");
-
// Subject currentUser=ShiroUtil.login("classpath:shiro_permission.ini", "jack", "123");
-
currentUser.checkPermission("user:select");
-
currentUser.checkPermissions("user:select","user:update","user:delete");
-
currentUser.logout();
-
}