FirstSuccessfulStrategy类源码解析

FirstSuccessfulStrategy类主要是返回第一个认证成功的认证信息,它继承了AbstractAuthenticationStrategy抽象类,先对其分析如下:

1.AbstractAuthenticationStrategy抽象类

此抽象类可以参照AbstractAuthenticationStrategy抽象类源码解析,主要实现了beforeAllAttempts(所有realm认证之前进行的操作),beforeAttempt(某一个realm认证之前进行的操作),afterAttempt(某一个realm认证之后进行的操作),merge(之前认证的认证信息与当前realm认证之后获取的认证信息的合并),afterAllAttempts(所有的realm认证完成之后的操作)。

2.FirstSuccessfulStrategy类(第一个认证成功策略)

2.1.所有的认证之前返回空(覆盖了AbstractAuthenticationStrategy的方法)

public AuthenticationInfo beforeAllAttempts(Collection<? extends Realm> realms, AuthenticationToken token) throws AuthenticationException {
        return null;
}

2.2.认证信息的合并(如果之前认证的结果aggregate不为空,则证明已经认证成功,返回aggregate;如果之前认证的结果aggregate为空,而现在认证的结果info不为空,则返回当前认证的结果,覆盖了AbstractAuthenticationStrategy的方法)

protected AuthenticationInfo merge(AuthenticationInfo info, AuthenticationInfo aggregate) {
        if (aggregate != null && !CollectionUtils.isEmpty(aggregate.getPrincipals())) {
            return aggregate;
        }
        return info != null ? info : aggregate;
    }

猜你喜欢

转载自yansxjl.iteye.com/blog/2333976