Authorization源码解析

1、首先调用 Subject.isPermitted*/hasRole* 接口,其会委托给SecurityManager。SecurityManager 接着会委托给 Authorizer;

Authorizer是真正的授权者,如果调用如isPermitted(“user:view”),其首先会通过• PermissionResolver 把字符串转换成相应的 Permission 实例;

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

 

2、SecurityManager 接着会委托给 Authorizer;

this.authorizer = new ModularRealmAuthorizer();
public
boolean hasRole(PrincipalCollection principals, String roleIdentifier) { return this.authorizer.hasRole(principals, roleIdentifier); }

 

3、ModularRealmAuthorizer 进行多 Realm 匹配流程

1)首先检查相应的 Realm 是否实现了实现了Authorizer;
2)如果实现了 Authorizer,那么接着调用其相应的isPermitted*/hasRole* 接口进行匹配

3)如果有一个Realm匹配那么将返回 true,否则返回 false。

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

 

 4、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);
    }

转载于:https://www.cnblogs.com/xiaoliangup/p/10460261.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值