基于debug 来看 shiro 如何 解析role 的权限

我们接着上个文章说。

我们还是基于上次说的那个例子。地址为:

https://github.com/fengyapeng/shiro-example/tree/master/shiro-example-chapter3

上个文章,我们了解了,针对用户,shiro如何是解析权限的,这个文章说一说角色。好吧,我认为,这个角色有点儿坑。

根据debug 来的源代码,

我们只能判断某一个用户是不是有某一个角色,

至于角色的权限,实际上有已经解析好了都。

我先来截图,说明一下基于配置文件中的的Realm 。

,好的。我们继续。

其中最关键的部分为:SimpleAccountRealm 这一部分。

我们先看这一段代码:

org.apache.shiro.authz.ModularRealmAuthorizer

    /**
     * Returns <code>true</code> if any of the configured realms'
     * {@link #isPermitted(org.apache.shiro.subject.PrincipalCollection, String)} returns <code>true</code>,
     * <code>false</code> otherwise.
     */
    public boolean isPermitted(PrincipalCollection principals, String permission) {
        assertRealmsConfigured();
        for (Realm realm : getRealms()) {
            if (!(realm instanceof Authorizer)) continue;
            if (((Authorizer) realm).isPermitted(principals, permission)) {
                return true;
            }
        }
        return false;
    }

这一段代码熟悉,上节中,我们就提到了这个类的这个方法。

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

org.apache.shiro.realm.AuthorizingRealm extends AuthenticatingRealm
        implements Authorizer, Initializable, PermissionResolverAware, RolePermissionResolverAware

[java] view
plain
copy

  1. public boolean isPermitted(PrincipalCollection principals, Permission permission) {
  2. AuthorizationInfo info = getAuthorizationInfo(principals);
  3. return isPermitted(permission, info);
  4. }
以上代码为:Realm 拿到 AuthorizationInfo ,通过 Permission,AuthorizationInfo 权限认定

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

引自上篇文章中的一段,

AuthorizationInfo info = getAuthorizationInfo(principals); info中已经包含了user 的role 和 操作。

这个函数调用的方法为:

org.apache.shiro.realm.AuthorizingRealm extends AuthenticatingRealm
        implements Authorizer, Initializable, PermissionResolverAware, RolePermissionResolverAware

protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) {

        if (principals == null) {
            return null;
        }

        AuthorizationInfo info = null;

        if (log.isTraceEnabled()) {
            log.trace("Retrieving AuthorizationInfo for principals [" + principals + "]");
        }

        Cache<Object, AuthorizationInfo> cache = getAvailableAuthorizationCache();
        if (cache != null) {
            if (log.isTraceEnabled()) {
                log.trace("Attempting to retrieve the AuthorizationInfo from cache.");
            }
            Object key = getAuthorizationCacheKey(principals);
            info = cache.get(key);
            if (log.isTraceEnabled()) {
                if (info == null) {
                    log.trace("No AuthorizationInfo found in cache for principals [" + principals + "]");
                } else {
                    log.trace("AuthorizationInfo found in cache for principals [" + principals + "]");
                }
            }
        }

        if (info == null) {
            // Call template method if the info was not found in a cache
            info = doGetAuthorizationInfo(principals);
            // If the info is not null and the cache has been created, then cache the authorization info.
            if (info != null && cache != null) {
                if (log.isTraceEnabled()) {
                    log.trace("Caching authorization info for principals: [" + principals + "].");
                }
                Object key = getAuthorizationCacheKey(principals);
                cache.put(key, info);
            }
        }

        return info;
    }

其中,doGetAuthorizationInfo(principals);才是真正的 拿到info 对象。

org.apache.shiro.realm.SimpleAccountRealm extends AuthorizingRealm

    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String username = getUsername(principals);
        USERS_LOCK.readLock().lock();
        try {
            return this.users.get(username);
        } finally {
            USERS_LOCK.readLock().unlock();
        }
    }

this.users.get(username)后面的代码,我就不贴了。users中就是username,AuthorizationInfo  的映射。

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

org.apache.shiro.realm.AuthorizingRealm extends AuthenticatingRealm
        implements Authorizer, Initializable, PermissionResolverAware, RolePermissionResolverAware

[java] view plaincopy
  1. private boolean isPermitted(Permission permission, AuthorizationInfo info) {
  2. Collection<Permission> perms = getPermissions(info);
  3. if (perms != null && !perms.isEmpty()) {
  4. for (Permission perm : perms) {
  5. if (perm.implies(permission)) {
  6. return true;
  7. }
  8. }
  9. }
  10. return false;
  11. }
以上代码为: Permission调用implies ,通过自定义实现的Permission,判定权限

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

以上代码是上篇文章中提到的。

其中getPermissions(info) 是通过info 拿到 Permission 的集合。跳进去看看是怎么一回事。

org.apache.shiro.realm.AuthorizingRealm extends AuthenticatingRealm
        implements Authorizer, Initializable, PermissionResolverAware, RolePermissionResolverAware

 private Collection<Permission> getPermissions(AuthorizationInfo info) {
        Set<Permission> permissions = new HashSet<Permission>();

        if (info != null) {
            Collection<Permission> perms = info.getObjectPermissions();
            if (!CollectionUtils.isEmpty(perms)) {
                permissions.addAll(perms);
            }
            perms = resolvePermissions(info.getStringPermissions());
            if (!CollectionUtils.isEmpty(perms)) {
                permissions.addAll(perms);
            }

            perms =<span style="color:#ff0000;"> resolveRolePermissions(info.getRoles());</span>
            if (!CollectionUtils.isEmpty(perms)) {
                permissions.addAll(perms);
            }
        }

        if (permissions.isEmpty()) {
            return Collections.emptySet();
        } else {
            return Collections.unmodifiableSet(permissions);
        }
    }
 private Collection<Permission> resolveRolePermissions(Collection<String> roleNames) {
        Collection<Permission> perms = Collections.emptySet();
        RolePermissionResolver resolver = getRolePermissionResolver();
        if (resolver != null && !CollectionUtils.isEmpty(roleNames)) {
            perms = new LinkedHashSet<Permission>(roleNames.size());
            for (String roleName : roleNames) {
                Collection<Permission> resolved = resolver.resolvePermissionsInRole(roleName);
                if (!CollectionUtils.isEmpty(resolved)) {
                    perms.addAll(resolved);
                }
            }
        }
        return perms;
    }

我能说,

RolePermissionResolver resolver = getRolePermissionResolver();拿到的值是null 么?
我把info 中的值给截图看看。
<img src="http://img.blog.csdn.net/20141205185700968?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZmVuZzI3MTU2/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />

好吧。其他的我都不说了,其实都是Realm 中定义的东西。

另外,我再贴一个url:http://jinnianshilongnian.iteye.com/blog/2020017,这个讲的不错。在此学习的。

时间: 2024-07-30 22:55:06

基于debug 来看 shiro 如何 解析role 的权限的相关文章

基于Nutch&amp;Solr定向采集解析和索引搜索的整合技术指南文档

基于Nutch&Solr定向采集解析和索引搜索的整合技术指南文档 内容来源于开源项目: http://git.oschina.net/xautlx/nutch-ajax https://github.com/xautlx/nutch-ajax 如何阅读本文档 本教程文档原始基于Markdown编写,如果你熟悉Markdown文件及相关工具使用,可以直接通过Markdown阅读或编辑工具查看本教程.md格式文件. 由于Markdown语法暂时没有目录支持,如果希望以目录导航方式查看文档,可参考如下

Forms身份验证和基于Role的权限验证

Forms身份验证和基于Role的权限验证 从Membership到SimpleMembership再到ASP.NET Identity,ASP.NET每一次更换身份验证的组件,都让我更失望.Membership的唯一作用就是你可以参考它的实现,它的数据库创建和扩展方面就真的让人实在无法使用了. 当大家欢呼着让ASP.NET开发走上ASP MVC的正确道路时,身份验证组件却走的更远了:SimpleMembership除了第三方验证的参考价值,它的主键和对领域模型的入侵让它成了摆设,而ASP.NE

蓝缘系统第三版本即将开源;基于springMVC+Apache shiro? 1.2.3+Mybai

蓝缘系统第三版本即将开源:基于springMVC+Apache shiro 1.2.3+Mybaits3.x的权限系统,,开放源码,支持开源 1.0版和2.0版的源码已经开源1.0版本:http://blog.csdn.net/mmm333zzz/article/details/16863543 2.0版本:http://blog.csdn.net/mmm333zzz/article/details/37773589 关于3.0新版本的说明: 一大亮点: 采用最新的技术流行框架:springMV

项目一:第十三天 1、菜单数据管理 2、权限数据管理 3、角色数据管理 4、用户数据管理 5、在realm中动态查询用户权限,角色 6、Shiro中整合ehcache缓存权限数据

1 课程计划 菜单数据管理 权限数据管理 角色数据管理 用户数据管理 在realm中动态查询用户权限,角色 Shiro中整合ehcache缓存权限数据         2 菜单数据添加 2.1 使用combotree父菜单项数据     1. 页面:menu_add.jsp 2. 修改组件样式:easyui-combotree,修改url  树型表格treeGrid跟下来数combotree要求数据格式基本一致. Combotree通过text属性展示文本.   3. 使用treegrid组件的

SpringBoot集成Shiro 实现动态加载权限

一.前言 本文小编将基于 SpringBoot 集成 Shiro 实现动态uri权限,由前端vue在页面配置uri,Java后端动态刷新权限,不用重启项目,以及在页面分配给用户 角色 . 按钮 .uri 权限后,后端动态分配权限,用户无需在页面重新登录才能获取最新权限,一切权限动态加载,灵活配置 基本环境 spring-boot 2.1.7 mybatis-plus 2.1.0 mysql 5.7.24 redis 5.0.5 温馨小提示:案例demo源码附文章末尾,有需要的小伙伴们可参考哦 ~

应用商城 下载apk 安装包解析错误 没有权限 Permission denied Android - failed to open zip archive

1.错误提示: 03-31 16:48:43.740: INFO/ActivityManager(59): Start proc com.android.packageinstaller for activity com.android.packageinstaller/.PackageInstallerActivity: pid=620 uid=10026 gids={} 03-31 16:48:44.749: WARN/zipro(620): Unable to open zip '/dat

分享一个基于ligerui的系统应用案例ligerRM V2(权限管理系统)(提供下载)

阅读目录 简介 系统特色 系统介绍 - 首页 系统介绍 - 列表页 系统介绍 - 明细页(表单) 系统介绍 - 菜单/按钮 系统介绍 - 权限中心 系统介绍 - 数据权限 系统介绍 - 字段权限 系统介绍 - 下拉框应用细节 结语 简介 ligerRM V2是基于 ligerui的web应用系统.以权限管理作为设计重点,引入Northwind作为主要的数据演示模块.权限方面,在上一个版本的基础上面加多了数据权限控制.后台方面采用dot net 3.5框架开发. 系统演示:http://case.

基于OpenCV进行图像拼接原理解析和编码实现(提纲 代码和具体内容在课件中)

一.背景 1.1概念定义 我们这里想要实现的图像拼接,既不是如题图1和2这样的"图片艺术拼接",也不是如图3这样的"显示拼接",而是实现类似"BaiDU全景"这样的全部的或者部分的实际场景的重新回放. 对于图像拼接的流程有很多定义方式,本教程中主要介绍实现主流方法,总结梳理如下: 图像采集->投影变换->特征点匹配->拼接对准->融合->反投影 图像采集不仅仅指的是普通的图像数据的获取.为了能够拼接过程能够顺利进行.

基于DPI(深度报文解析)的应用识别

一.概述 1.DPI(Deep packet inspection,深度报文解析) 所谓"深度"是和普通的报文分析层次相比较而言的,"普通报文检测"仅分析IP包4 层以下的内容,包括源地址.目的地址.源端口.目的端口以及协议类型,而DPI 除了对前面的层次分析外,还增加了应用层分析,识别各种应用及其内容,主要实现一下功能: 1)应用分析--网络流量构成分析.性能分析.流向分析等: 2)用户分析--用户群区分.行为分析.终端分析.趋势分析等: 3)网元分析--根据区域