`
李大宝
  • 浏览: 13859 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

shiro-权限认证(Authorization)

阅读更多

上一篇博文总结了一下Shiro中的身份认证,本文主要来总结一下Shiro中的权限认证(Authorization)功能,即授权。如下: 


1. 权限认证的核心要素

权限认证,也就是访问控制,即在应用中控制谁能访问哪些资源。在权限认证中,最核心的三个要素是:权限,角色和用户:

 

权限(permission):即操作资源的权利,比如访问某个页面,以及对某个模块的数据的添加,修改,删除,查看的权利; 
角色(role):指的是用户担任的的角色,一个角色可以有多个权限; 
用户(user):在Shiro 中,代表访问系统的用户,即上一篇博文提到的Subject认证主体

 

它们之间的的关系可以用下图来表示: 

 

 

 一个用户可以有多个角色,而不同的角色可以有不同的权限,也可由有相同的权限。比如说现在有三个角色,1是普通角色,2也是普通角色,3是管理员,角色1只能查看信息,角色2只能添加信息,管理员都可以,而且还可以删除信息,类似于这样。

 

 

2.1 基于角色的访问控制

  也就是说,授权过程是通过判断角色来完成的,哪个角色可以做这件事,哪些角色可以做这件事等等。它有如下API:

 

方法 作用
hasRole(String roleName) 判断是否有该角色访问权,返回boolen
hasRoles(List<String> roleNames) 判断是否有这些这些角色访问权,返回boolean[]
hasAllRoles(Collection<String> roleNames) 判断是否有这些这些角色访问权,返回boolean

 

 

 对这三个API,做一下简单的说明,第一个很简单,传入一个role即可,判断是否拥有该角色访问权,第二个方法是传入一个role的集合,然后Shiro会根据集合中的每一个role做一下判断,并且将每次的判断结果放到boolean[]数组中,顺序与集合中role的顺序一致;第三个方法也是传入一个role的集合,不同的是,返回boolean类型,必须集合中全部role都有才为true,否则为false。 

  用法如下:

Subject currentUser = SecurityUtils.getSubject();
if (currentUser.hasRole("administrator")) {
    //show the admin button or do administrator's things
} else {
    //don't show the button?  Grey it out? or others...
}

 除了这三个API外,Shiro还提供了check的API,与上面不同的是,has-xxx会返回boolean类型的数据,用来判断,而check-xxx不会返回任何东西,如果验证成功就继续处理下面的代码,否则会抛出一个异常,可以用来通过捕获异常来处理。API如下:

 

方法 作用
checkRole(String roleName) 如果判断失败抛出AuthorizationException异常
checkRoles(String... roleNames) 如果判断失败抛出AuthorizationException异常
checkRoles(Collection<String> roleNames) 如果判断失败抛出AuthorizationException异常

 

 类似的使用方法如下:

 

Subject currentUser = SecurityUtils.getSubject();
//guarantee that the current user is a bank teller and
//therefore allowed to open the account:
currentUser.checkRole("bankTeller");
openBankAccount();

 

2.2 基于权限的访问控制

  基于权限的访问控制和基于角色的访问控制在原理上是一模一样的,只不过API不同而已,我不再做过多的解释,API如下:

 

方法 作用
isPermitted(String perm) 判断是否有该权限,返回boolen
isPermitted(List<String> perms) 判断是否有这些这些权限,返回boolean[]
isPermittedAll(Collection<String> perms) 判断是否有这些这些权限,返回boolean
checkPermission(String perm) 如果判断失败抛出AuthorizationException异常
checkPermissions(String... perms) 如果判断失败抛出AuthorizationException异常
checkPermissionsAll(Collection<String> perms) 如果判断失败抛出AuthorizationException异常

 

3. 权限认证示例代码

  不管是身份认证还是权限认证,首先都需要创建SecurityManager工厂,SecurityManager,所以首先新建一个工具类专门做这个事情。

public class 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;
    }
}

 

提供一个静态方法,返回当前用户,在应用程序中我们直接调用这个类中的静态方法即可返回当前认证的用户了。 
  maven中的pom.xml文件内容和上一节一样的,不再赘述。 
  Shiro的配置文件shiro.ini:

#用户,role表示各个角色
[users]
csdn1=123,role1,role2,role3
csdn2=123,role1,role2

#定义不同角色都拥有哪些权限
[roles]
role1=user:select
role2=user:add,user:update
role3=user.delete

 

角色认证:

public class RoleTest {

    @Test
    public void testHasRole() {

        String configFile = "classpath:shiro.ini";
        String username = "csdn2";
        String password = "123";
        Subject currentUser = ShiroUtil.login(configFile, username, password);

        //测试hasRole
        System.out.println(currentUser.hasRole("role2")? "有role2这个角色" : "没有role2这个角色");

        //测试hasRoles
        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这个角色");

        //测试hasAllRoles     System.out.println(currentUser.hasAllRoles(Arrays.asList("role1","role2","role3")));

        currentUser.logout();
    }

    @Test
    public void testCheckRole() {

        String configFile = "classpath:shiro.ini";
        String username = "csdn2";
        String password = "123";
        Subject currentUser = ShiroUtil.login(configFile, username, password);

//      currentUser.checkRole("role3");//没有返回值。有就不报错,没有就会报错
//      currentUser.checkRoles(Arrays.asList("role1","role2","role3")); //同上
        currentUser.checkRoles(Arrays.asList("role1","role2")); //同上

        currentUser.logout();
    }
}

 

权限认证和角色认证的测试一样的,我就不再赘述了。当然了,这里只是单纯的测试,实际中,认证完了后还要做一些具体的业务逻辑处理。

 

  • 大小: 22.2 KB
  • 大小: 142.4 KB
分享到:
评论

相关推荐

    shiro-core-1.4.0-API文档-中文版.zip

    赠送jar包:shiro-core-1.4.0.jar; 赠送原API文档:shiro-core-1.4.0-javadoc.jar; 赠送源代码:shiro-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-core-1.4.0.pom; 包含翻译后的API文档:shiro-core...

    shiro-jar.zip

    shiro-all-1.7.1.jar,shiro-aspectj-1.7.1.jar,shiro-cache-1.7.1.jar,shiro-config-core-1.7.1.jar,shiro-config-ogdl-1.7.1.jar,shiro-core-1.7.1.jar,shiro-crypto-cipher-1.7.1.jar,shiro-crypto-core-1.7.1.jar...

    shiro-jar-1.7.0.zip相关资源包

    解决:升級1.7后附件...shiro-cas-1.7.0.jar shiro-core-1.7.0.jar shiro-ehcache-1.7.0.jar shiro-spring-1.7.0.jar shiro-web-1.7.0.jar CustomShiroFilterFactoryBean.java spring-context-shiro.xml 修改说明.txt

    shiro-ehcache-1.4.0-API文档-中文版.zip

    赠送jar包:shiro-ehcache-1.4.0.jar; 赠送原API文档:shiro-ehcache-1.4.0-javadoc.jar; 赠送源代码:shiro-ehcache-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-ehcache-1.4.0.pom; 包含翻译后的API文档...

    shiro-config-core-1.4.0-API文档-中文版.zip

    赠送jar包:shiro-config-core-1.4.0.jar; 赠送原API文档:shiro-config-core-1.4.0-javadoc.jar; 赠送源代码:shiro-config-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-config-core-1.4.0.pom; ...

    shiro-event-1.4.0-API文档-中文版.zip

    赠送jar包:shiro-event-1.4.0.jar; 赠送原API文档:shiro-event-1.4.0-javadoc.jar; 赠送源代码:shiro-event-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-event-1.4.0.pom; 包含翻译后的API文档:shiro-...

    shiro(shiro-all-1.8.0.jar)

    shiro(shiro-all-1.8.0.jar)

    shiro-core-1.7.1 jar

    shiro shiro-core-1.7.1 jar shiro漏洞

    shiro-core-1.3.2-API文档-中文版.zip

    赠送jar包:shiro-core-1.3.2.jar; 赠送原API文档:shiro-core-1.3.2-javadoc.jar; 赠送源代码:shiro-core-1.3.2-sources.jar; 包含翻译后的API文档:shiro-core-1.3.2-javadoc-API文档-中文(简体)版.zip ...

    shiro-core 低版本漏洞检测

    shiro-core 低版本漏洞检测工具

    shiro-crypto-core-1.4.0-API文档-中文版.zip

    赠送jar包:shiro-crypto-core-1.4.0.jar; 赠送原API文档:shiro-crypto-core-1.4.0-javadoc.jar; 赠送源代码:shiro-crypto-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-crypto-core-1.4.0.pom; ...

    shiro-crypto-cipher-1.4.0-API文档-中文版.zip

    赠送jar包:shiro-crypto-cipher-1.4.0.jar; 赠送原API文档:shiro-crypto-cipher-1.4.0-javadoc.jar; 赠送源代码:shiro-crypto-cipher-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-crypto-cipher-1.4.0....

    shiro-cas-1.2.3-API文档-中文版.zip

    赠送jar包:shiro-cas-1.2.3.jar; 赠送原API文档:shiro-cas-1.2.3-javadoc.jar; 赠送源代码:shiro-cas-1.2.3-sources.jar; 赠送Maven依赖信息文件:shiro-cas-1.2.3.pom; 包含翻译后的API文档:shiro-cas-...

    shiro-web-1.3.2-API文档-中文版.zip

    赠送jar包:shiro-web-1.3.2.jar; 赠送原API文档:shiro-web-1.3.2-javadoc.jar; 赠送源代码:shiro-web-1.3.2-sources.jar; 包含翻译后的API文档:shiro-web-1.3.2-javadoc-API文档-中文(简体)版.zip 对应...

    shiro-core-1.2.3-API文档-中文版.zip

    赠送jar包:shiro-core-1.2.3.jar; 赠送原API文档:shiro-core-1.2.3-javadoc.jar; 赠送源代码:shiro-core-1.2.3-sources.jar; 赠送Maven依赖信息文件:shiro-core-1.2.3.pom; 包含翻译后的API文档:shiro-core...

    shiro-spring-1.3.2-API文档-中英对照版.zip

    赠送jar包:shiro-spring-1.3.2.jar 赠送原API文档:shiro-spring-1.3.2-javadoc.jar 赠送源代码:shiro-spring-1.3.2-sources.jar 包含翻译后的API文档:shiro-spring-1.3.2-javadoc-API文档-中文(简体)-英语-...

    shiro-crypto-core-1.4.0-API文档-中英对照版.zip

    赠送jar包:shiro-crypto-core-1.4.0.jar; 赠送原API文档:shiro-crypto-core-1.4.0-javadoc.jar; 赠送源代码:shiro-crypto-core-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-crypto-core-1.4.0.pom; ...

    shiro-event-1.4.0-API文档-中英对照版.zip

    赠送jar包:shiro-event-1.4.0.jar; 赠送原API文档:shiro-event-1.4.0-javadoc.jar; 赠送源代码:shiro-event-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-event-1.4.0.pom; 包含翻译后的API文档:shiro-...

    shiro-root-1.2.3-source-release zipa包 和相关jar包

    shiro使用依赖的所有jar包,以及shiro-root-1.2.3-source-release的zip包

    shiro-cas-1.2.3-API文档-中英对照版.zip

    赠送jar包:shiro-cas-1.2.3.jar; 赠送原API文档:shiro-cas-1.2.3-javadoc.jar; 赠送源代码:shiro-cas-1.2.3-sources.jar; 赠送Maven依赖信息文件:shiro-cas-1.2.3.pom; 包含翻译后的API文档:shiro-cas-...

Global site tag (gtag.js) - Google Analytics