Shiro源码分析之鉴权方式

一、 怎么用

Shiro 支持三种方式的授权

  • 编程式:通过写 if/else 授权代码块完成:
Subject subject = SecurityUtils.getSubject(); 
if(subject.hasRole(“admin”)) { 
 //有权限
} else { 
 //无权限
}
  • 注解式:通过在执行的 Java 方法上放置相应的注解完成:
@RequiresRoles("admin") 
public void hello() { 
 //有权限
} 
  • JSP/GSP 标签:在 JSP/GSP 页面通过相应的标签完成
<shiro:hasRole name="admin"> 
<!— 有权限 —> 
</shiro:hasRole> 

二、啥原理

三种方式背后的原理是一样的,由简单到复杂的顺序来看看。

1、标签式

shiro提供了RoleTag和PermissionTag的抽象类,还有两个简单的实现:

public class HasRoleTag extends RoleTag {

    //TODO - complete JavaDoc

    public HasRoleTag() {
    }

    protected boolean showTagBody(String roleName) {
        return getSubject() != null && getSubject().hasRole(roleName);
    }

}
public class HasPermissionTag extends PermissionTag {

    //TODO - complete JavaDoc

    public HasPermissionTag() {
    }

    protected boolean showTagBody(String p) {
        return isPermitted(p);
    }

}

分别调用的是DelegatingSubject的hasRole方法和isPermitted方法:

public boolean hasRole(String roleIdentifier) {
    return hasPrincipals() && securityManager.hasRole(getPrincipals(), roleIdentifier);
}

public boolean isPermitted(String permission) {
    return hasPrincipals() && securityManager.isPermitted(getPrincipals(), permission);
}


然后又调用AuthorizingSecurityManager的相关方法,AuthorizingSecurityManager持有一个ModularRealmAuthorizer类型的Authorizer:

this.authorizer = new ModularRealmAuthorizer();

相关方法又转移至调用ModularRealmAuthorizer:

ModularRealmAuthorizer 进行多 Realm 匹配流程:
1、首先检查相应的 Realm 是否实现了实现了 Authorizer;
2、如果实现了 Authorizer,那么接着调用其相应的 isPermitted*/hasRole*接口进行匹配;
3、如果有一个 Realm 匹配那么将返回 true,否则返回 false。

2、注解式

@RequiresRoles("admin")
@RequiresPermissions("admin:role:view")
@RequestMapping(value = "/configIndex", method = { RequestMethod.GET })
public String index(Model model) {
    return "rolemgr/roleConfig/configIndex";
}
技术细节

基于拦截器实现(AuthorizingAnnotationMethodInterceptor)
动态代理技术(CglibAopProxy)

spring InvocableHandlerMethod#invoke

CglibAopProxy.DynamicAdvisedInterceptor#intercept

AnnotationMethodInterceptor AuthorizingAnnotationMethodInterceptor#assertAuthorized RoleAnnotationHandler#assertAuthorized

委托调用

DelegatingSubject:

public void checkRole(String role) throws AuthorizationException {
    assertAuthzCheckPossible();
    securityManager.checkRole(getPrincipals(), role);
}

AuthorizingSecurityManager:

public void checkRole(PrincipalCollection principals, String role) throws AuthorizationException {
    this.authorizer.checkRole(principals, role);
}

ModularRealmAuthorizer:

public void checkRole(PrincipalCollection principals, String role) throws AuthorizationException {
    assertRealmsConfigured();
    if (!hasRole(principals, role)) {
        throw new UnauthorizedException("Subject does not have role [" + role + "]");
    }
}

public boolean hasRole(PrincipalCollection principals, String roleIdentifier) {
    assertRealmsConfigured();
    for (Realm realm : getRealms()) {
        if (!(realm instanceof Authorizer)) continue;
        if (((Authorizer) realm).hasRole(principals, roleIdentifier)) {
            return true;
        }
    }
    return false;
}

AuthorizingRealm:

public boolean hasRole(PrincipalCollection principal, String roleIdentifier) {
    AuthorizationInfo info = getAuthorizationInfo(principal);
    return hasRole(roleIdentifier, info);
}

protected boolean hasRole(String roleIdentifier, AuthorizationInfo info) {
    return info != null && info.getRoles() != null && info.getRoles().contains(roleIdentifier);
}

关于 getAuthorizationInfo 方法:

protected AuthorizationInfo getAuthorizationInfo(PrincipalCollection principals) {

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

    AuthorizationInfo info = null;

    Cache<Object, AuthorizationInfo> cache = getAvailableAuthorizationCache();
    if (cache != null) {
        Object key = getAuthorizationCacheKey(principals);
        info = cache.get(key);
    }

    if (info == null) {
        // 如果cache中没有找到info则调用模板方法
        info = doGetAuthorizationInfo(principals);
        // info不为null并且cache已经创建了,则缓存info信息
        if (info != null && cache != null) {
            Object key = getAuthorizationCacheKey(principals);
            cache.put(key, info);
        }
    }

    return info;
}
  • 首先还是找可用的权限缓存:
private Cache<Object, AuthorizationInfo> getAvailableAuthorizationCache() {
    Cache<Object, AuthorizationInfo> cache = getAuthorizationCache();
    if (cache == null && isAuthorizationCachingEnabled()) {
        cache = getAuthorizationCacheLazy();
    }
    return cache;
}

回顾下安全管理器的结构,RealmSecurityManager继承了CachingSecurityManager,当CachingSecurityManager设置了cacheManager,会调用用子类的afterCacheManagerSet方法:

public void setCacheManager(CacheManager cacheManager) {
    this.cacheManager = cacheManager;
    afterCacheManagerSet();
}

RealmSecurityManager将会为每个Realm设置cacheManager:

protected void afterCacheManagerSet() {
    applyCacheManagerToRealms();
}

protected void applyCacheManagerToRealms() {
    CacheManager cacheManager = getCacheManager();
    Collection<Realm> realms = getRealms();
    if (cacheManager != null && realms != null && !realms.isEmpty()) {
        for (Realm realm : realms) {
            if (realm instanceof CacheManagerAware) {
                ((CacheManagerAware) realm).setCacheManager(cacheManager);
            }
        }
    }
}

看下AuthorizingRealm类关系图: image

在不同层级的构造器中分别设置了是否启用权限缓存和身份验证缓存

this.authorizationCachingEnabled = true;

this.authenticationCachingEnabled = false;

再看CachingRealm,跟安全管理器的做法类似,并且为每个realm设置cacheManager的时候就已经触发了子类的相关操作:

public void setCacheManager(CacheManager cacheManager) {
    this.cacheManager = cacheManager;
    afterCacheManagerSet();
}

AuthorizingRealm的实现就是通过cacheManager去获取权限相关的cache:

protected void afterCacheManagerSet() {
    super.afterCacheManagerSet();
    //trigger obtaining the authorization cache if possible
    getAvailableAuthorizationCache();
}

如果我们没有明确配置cacheManager(作为securityManager的属性注入),那么此时是获取不到的,cache为null并且启用了权限缓存,现在就要临时构造一个:

cache = getAuthorizationCacheLazy();

  • 获取AuthorizationInfo

以principals作为key取AuthorizationInfo:

info = cache.get(key);

if (info == null) {

    info = doGetAuthorizationInfo(principals);
    if (info != null && cache != null) {
        Object key = getAuthorizationCacheKey(principals);
        cache.put(key, info);
    }
}

这个时候就转到我们自己的实现了,我们自己去获取权限,然后返回一个AuthorizationInfo,就是权限相关的信息。

3、总结

编程式很暴力也很直接,直接操作subject的相关方法来鉴权,其他两种方式拐弯抹角地也是操作的subject,然后再委托给securityManager。

具体是AuthorizingSecurityManager层实现的,它是直接new了一个ModularRealmAuthorizer,相关操作又转交给它,它又梳理一下,交给我们实现的realm(父类AuthorizingRealm层实现)。

ModularRealmAuthorizer是怎么获取到realm的?
也是我们给securityManager配置的,类似上面cacheManager的set方法,安全管理器用了很多这样的方法,给它本身注入相关属性时,就把相关联的set了。

AuthorizingSecurityManager这样实现的:

protected void afterRealmsSet() {
    super.afterRealmsSet();
    if (this.authorizer instanceof ModularRealmAuthorizer) {
        ((ModularRealmAuthorizer) this.authorizer).setRealms(getRealms());
    }
}

从类的调用关系来看:
DelegatingSubject -> AuthorizingSecurityManager -> ModularRealmAuthorizer -> Realm

  • 注解调用栈
    RequiresRoles :checkRole -> checkRole -> checkRole -> hasRole
    RequiresPermissions:checkPermission -> checkPermission -> checkPermission -> isPermitted

三、学到什

  • Shiro作为框架是如何承上启下的,上接servlet规范,中搭spring顺风车,下给开发者自己实现。
  • 实现的技巧
  • 开发中有效的配置,哪些是必要的,哪些是默认的。
  • 如何提升开发效率

例如在自己的XxRealm中:

protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
    if (MySubjectUtils.isPatformAdmin()) {
        simpleAuthorizationInfo.addStringPermission("*");
    } else {
        //查询关联的权限adminUser:create:01001001
    }
}

或者在自己的权限标签中:

public class HasAnyPermissionTag extends PermissionTag {

    @Override
    protected boolean showTagBody(String permissions) {
        boolean hasPermission = false;
        Subject subject = getSubject();

        if(MySubjectUtils.isPatformAdmin()){
            return true;
        }
        //......
    }
}

这样作为开发者对于角色权限的配置可以省掉了,将特定id或者name的开发人员设置为平台管理员,可以坐拥天下,在功能不断完善的情况下不需要再补充权限。

转载于:https://my.oschina.net/u/2275855/blog/1860797

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shiro分布式鉴权是指在分布式系统中使用Shiro进行身份验证和授权的过程。对于这个问题,可以参考引用和引用中提供的相关文档和示例代码来实现Shiro分布式鉴权的功能。首先,可以查看jsets-shiro-spring-boot-starter项目详情以了解如何在Spring Boot项目中集成Shiro。并且可以参考jsets-shiro-demo示例源码来了解如何在具体项目中应用Shiro进行鉴权。另外,可以查看使用说明来了解jsets-shiro-spring-boot-starter的具体使用方法。引用中提到了一种防止重放攻击的方法,通过将token的ID放入缓存(如redis、memcached)进行阅后即焚或销毁缓存来确保token只能使用一次。这是一种常见的防止重放攻击的解决方案。通过这些参考资料,你可以更好地理解和实践Shiro分布式鉴权的方案。123 #### 引用[.reference_title] - *1* *2* [shiro jwt 构建无状态分布式鉴权体系](https://blog.csdn.net/wj596/article/details/84914572)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] - *3* [分布式鉴权之JWT整合shiro](https://blog.csdn.net/weixin_43769053/article/details/107643606)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值