[shiro_permission.ini]

[users]
java1234=123456,role1,role2
jack=123,role1
[roles]
role1=user:select
role2=user:add,user=:update,user:detele

单个权限判断isPermitted

public void testIsPermitted() {
    Subject currentUser = ShiroUtil.login("classpath:shiro_permission.ini", "java1234", "123456");
    // 单个权限判断isPermitted
    if (currentUser.isPermitted("user:detele")) {
        System.out.println("有user:select的权限");
    } else {
        System.out.println("无user:select的权限");
    }
    
    // 多个权限传入,单个依次判断,返回一个boolean数组,依次对应输入的权限判断
    boolean[] results = currentUser.isPermitted("user:select", "user=:update", "user:detele");
    System.out.println(results[0]?"有权限":"没有权限");
    System.out.println(results[1]?"有权限":"没有权限");
    System.out.println(results[2]?"有权限":"没有权限");
    
    // 多个权限传入,一次性判断,
    if (currentUser.isPermittedAll("user:select", "user=:update", "user:detele", "user:insert")) {
        System.out.println("都拥有");
    } else {
        System.out.append("都不拥有");
    }
    
    currentUser.logout();
}

checkPermission

单个判断,checkPermission判断是否有该权限,如果没有该权限,会抛出异常

public void testCheckPermitted() {
    Subject currentUser = ShiroUtil.login("classpath:shiro_permission.ini", "java1234", "123456");
    // checkPermission判断是否有该权限,如果没有该权限,会抛出异常
    currentUser.checkPermission("user:select2");
    currentUser.logout();
}

多个权限全部判断,如果其中的一个权限没有,则会抛出异常

public void testCheckPermitted() {
    Subject currentUser = ShiroUtil.login("classpath:shiro_permission.ini", "java1234", "123456");
    // 多个权限全部判断,如果其中的一个权限没有,则会抛出异常
    currentUser.checkPermissions("user:select", "user:detele");
    currentUser.logout();
}