Authorizationソースコード解析

3148 ワード

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