Shiro认证源码图文解析,吃一堑长一智

**/

protected Collection getPrincipalsLazy(String realmName) {

//我们可以看到,其实这个方法是用来初始化relamPrincipals的

//如果是第一次加入,先对realmPrincipals进行初始化

if (this.realmPrincipals == null) {

//第一次定义为是一个LinkedHashMap

//这个LinkedHashMap,键是realmName,值是一个set集合

this.realmPrincipals = new LinkedHashMap();

}

//通过键值对方式,获取realmPrincipals的对应realmName的set集合

Set principals = (Set)this.realmPrincipals.get(realmName);

//如果没有,证明该Realm是第一次存放认证信息,还没有进行与其他Realm的信息合并

if (principals == null) {

//set集合具体是一个LinkedHashSet

principals = new LinkedHashSet();

//往realmPrincipals中放入这realmName为key,principals为值的键值对

this.realmPrincipals.put(realmName, principals);

}

//如果已经有了,证明已经合并过了

//返回realmName对应的set集合

return (Collection)principals;

}

所以SimplePrincipalCollection的底层是一个LinkedHashMap,以RealmName为键,是一个字符串对象,值对应的是一个LinkedHashSet集合,里面存放的就是principal(账号信息)

在这里插入图片描述

我们可以看到key是Realm的全限定符,value里面存的就是是一个以账号组成的LinkedHashSet。

为什么要用LinkedHashSet呢?一个Realm里面还会存在多个账号的吗?

在这里插入图片描述

下面回到我们的认证过程

DefaultSecurityManager调用自身的authenticate方法发来获取认证信息,该方法实现具体如下

在这里插入图片描述

点进去,发现Authenticator是一个接口,而且拥有两个实现类,那么具体是哪一个呢?

在这里插入图片描述

这里,我们必须返回到上一层看一下DefaultSecurityManager的authenticator是哪一个,去看看到底调用的是哪一个实现类,前面已经提到过,DefaultSecurityManager的authenticator(认证器)是AuthenticatingSecurityManager的认证器,所以默认的类型是ModularRealmAuthenticator。

在这里插入图片描述

我们可以看到ModularRealmAuthenticator是继承AbstractAuthenticator的,而且并没有authenticate方法的实现,所以可以断定,authenticate的实现肯定在父类AbstractAuthenticator中

在这里插入图片描述

在里面,很轻易找到了对应具体实现方法

public final AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {

//如果token不存在,抛出异常

if (token == null) {

throw new IllegalArgumentException(“Method argument (authentication token) cannot be null.”);

} else {

log.trace(“Authentication attempt received for token [{}]”, token);

//该变量用于记录认证信息

AuthenticationInfo info;

try {

//调用自身的doAuthenticate方法获取认证信息

info = this.doAuthenticate(token);

if (info == null) {

String msg = “No account information found for authentication token [” + token + "] by this " + “Authenticator instance. Please check that it is configured correctly.”;

throw new AuthenticationException(msg);

}

} catch (Throwable var8) {

AuthenticationException ae = null;

if (var8 instanceof AuthenticationException) {

ae = (AuthenticationException)var8;

}

if (ae == null) {

String msg = “Authentication failed for token submission [” + token + "]. Possible unexpected " + “error? (Typical or expected login exceptions should extend from AuthenticationException).”;

ae = new AuthenticationException(msg, var8);

if (log.isWarnEnabled()) {

log.warn(msg, var8);

}

}

try {

this.notifyFailure(token, ae);

} catch (Throwable var7) {

if (log.isWarnEnabled()) {

String msg = “Unable to send notification for failed authentication attempt - listener error?. Please check your AuthenticationListener implementation(s). Logging sending exception and propagating original AuthenticationException instead…”;

log.warn(msg, var7);

}

}

throw ae;

}

log.debug(“Authentication successful for token [{}]. Returned account [{}]”, token, info);

this.notifySuccess(token, info);

return info;

}

}

我们可以看到,这里是调用了自身的doAuthenticate方法去获取认证信息的,所以,下面就去看看这个方法

protected abstract AuthenticationInfo doAuthenticate(AuthenticationToken var1) throws AuthenticationException;

这个方法是一个抽象方法,所以肯定是由AbstractAuthenticator的子类ModularRealmAuthenticator去实现的

在这里插入图片描述

具体实现如下

protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {

//判断是否有Realm装配

this.assertRealmsConfigured();

//获取所有Realm

Collection realms = this.getRealms();

//如果只有一个Realm,就只调用那个Realm(通过迭代器获取)

//如果有多个Realm,就多个执行

return realms.size() == 1 ? this.doSingleRealmAuthentication((Realm)realms.iterator().next(), authenticationToken) : this.doMultiRealmAuthentication(realms, authenticationToken);

}

好了,现在弄清楚认证方法是怎样的了,具体的规则如下

  • 如果只有一个Realm,就只调用那一个Realm

  • 如果有多个Re

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值