关于shiro的 subject.getPrincipal()方法

1、说明

上一篇文章说明了 principal,而subject.getPrincipal();是用来干嘛的,他就是来获取你存储的principal,内部是怎么获取的那,多个principal怎么指定获取哪一个那。

2、解释

1)subject.getPrincipal();最后调用的是下面这个方法

public Object getPrimaryPrincipal() {
        if (isEmpty()) {
            return null;
        }
        return iterator().next();
    }

2)可见他便利的是集合,那就是说明每次调用都会不一样,那么就存在一定的风险性。如果我们希望每次调用都返回一个固定的值例如id,应该怎么办呐,其实非常简单,只需要扩展两个类即可。

3、编码

1)扩展SimplePrincipalCollection这个类,新建一个类来继承他,我们只需要重写他的一个方法就好,另外类他添加一个额外的来接受id的属性,这样在getPrimaryPrincipal方法中我们只需把id返回就好。代码如下

public class CustomSimplePrincipalCollection extends SimplePrincipalCollection {

    private Object primary;

    public CustomSimplePrincipalCollection() {
    }


    public CustomSimplePrincipalCollection(Object primary, Object principal, String realmName) {
        super(principal, realmName);
        this.primary = primary;
    }

    public CustomSimplePrincipalCollection(Object principal, String realmName) {
        super(principal, realmName);
    }

    public CustomSimplePrincipalCollection(Collection principals, String realmName) {
        super(principals, realmName);
    }

    public CustomSimplePrincipalCollection(PrincipalCollection principals) {
        super(principals);
    }

    @Override
    public Object getPrimaryPrincipal() {
        return primary;
    }

    public Object getCustomPrimaryPrincipal() {
        return primary;
    }
}

2)然后重写SimpleAuthenticationInfo,这个需要实现响应的接口,然后修改部分方法即可

public class CustomSimpleAuthenticationInfo implements MergableAuthenticationInfo, SaltedAuthenticationInfo {

    /**
     * The principals identifying the account associated with this AuthenticationInfo instance.
     */
    protected PrincipalCollection principals;
    /**
     * The credentials verifying the account principals.
     */
    protected Object credentials;

    /**
     * Any salt used in hashing the credentials.
     *
     * @since 1.1
     */
    protected ByteSource credentialsSalt;

    /**
     * Default no-argument constructor.
     */
    public CustomSimpleAuthenticationInfo() {
    }


    public CustomSimpleAuthenticationInfo(Object primary, Object principal, Object credentials, String realmName) {
        this.principals = new CustomSimplePrincipalCollection(primary, principal, realmName);
        this.credentials = credentials;
    }


    public CustomSimpleAuthenticationInfo(Object principal, Object hashedCredentials, ByteSource credentialsSalt, String realmName) {
        this.principals = new CustomSimplePrincipalCollection(principal, realmName);
        this.credentials = hashedCredentials;
        this.credentialsSalt = credentialsSalt;
    }


    public CustomSimpleAuthenticationInfo(PrincipalCollection principals, Object credentials) {
        this.principals = new CustomSimplePrincipalCollection(principals);
        this.credentials = credentials;
    }

    public CustomSimpleAuthenticationInfo(PrincipalCollection principals, Object hashedCredentials, ByteSource credentialsSalt) {
        this.principals = new CustomSimplePrincipalCollection(principals);
        this.credentials = hashedCredentials;
        this.credentialsSalt = credentialsSalt;
    }


    public PrincipalCollection getPrincipals() {
        return principals;
    }


    public void setPrincipals(PrincipalCollection principals) {
        this.principals = principals;
    }

    public Object getCredentials() {
        return credentials;
    }


    public void setCredentials(Object credentials) {
        this.credentials = credentials;
    }


    public ByteSource getCredentialsSalt() {
        return credentialsSalt;
    }


    public void setCredentialsSalt(ByteSource salt) {
        this.credentialsSalt = salt;
    }


    @SuppressWarnings("unchecked")
    public void merge(AuthenticationInfo info) {
        if (info == null || info.getPrincipals() == null || info.getPrincipals().isEmpty()) {
            return;
        }

        if (this.principals == null) {
            this.principals = info.getPrincipals();
        } else {
            if (!(this.principals instanceof MutablePrincipalCollection)) {
                this.principals = new SimplePrincipalCollection(this.principals);
            }
            ((MutablePrincipalCollection) this.principals).addAll(info.getPrincipals());
        }

        //only mess with a salt value if we don't have one yet.  It doesn't make sense
        //to merge salt values from different realms because a salt is used only within
        //the realm's credential matching process.  But if the current instance's salt
        //is null, then it can't hurt to pull in a non-null value if one exists.
        //
        //since 1.1:
        if (this.credentialsSalt == null && info instanceof SaltedAuthenticationInfo) {
            this.credentialsSalt = ((SaltedAuthenticationInfo) info).getCredentialsSalt();
        }

        Object thisCredentials = getCredentials();
        Object otherCredentials = info.getCredentials();

        if (otherCredentials == null) {
            return;
        }

        if (thisCredentials == null) {
            this.credentials = otherCredentials;
            return;
        }

        if (!(thisCredentials instanceof Collection)) {
            Set newSet = new HashSet();
            newSet.add(thisCredentials);
            setCredentials(newSet);
        }

        // At this point, the credentials should be a collection
        Collection credentialCollection = (Collection) getCredentials();
        if (otherCredentials instanceof Collection) {
            credentialCollection.addAll((Collection) otherCredentials);
        } else {
            credentialCollection.add(otherCredentials);
        }
    }


    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof SimpleAuthenticationInfo)) return false;

        CustomSimpleAuthenticationInfo that = (CustomSimpleAuthenticationInfo) o;

        //noinspection RedundantIfStatement
        if (principals != null ? !principals.equals(that.principals) : that.principals != null) return false;

        return true;
    }


    public int hashCode() {
        return (principals != null ? principals.hashCode() : 0);
    }


    public String toString() {
        return principals.toString();
    }

3)然后就是进行调用

 return new CustomSimpleAuthenticationInfo(admin.getId(), list, admin.getPsd(), this.getClass().getName());

4)之后你在调用subject.getPrincipal()返回的都是同一个值 也就是id,也就是CustomSimpleAuthenticationInfo(admin.getId(), list, admin.getPsd(), this.getClass().getName())的第一个参数。

你可能感兴趣的:(other)