shiro登录流程源码分析

*关键点:跟踪token的去向

分析整个登录流程,以token去向为线索,层层追踪,直到跟我们自定义的realm验证方法结合。

1.获取Subject实例(实例通常是DelegatingSubject 或者是其子类)

Subject subject = SecurityUtils.getSubject();

2.调用登录

subject.login(token)(这里的token收集方式不重要)
可能是从页面传递上来的用户名和密码或者其他的方式收集的信息,通过:
UsernamePasswordToken token = new UsernamePasswordToken(userName(),password());
获取token,并调用subject.login(token)
这里的subject.login是DelegatingSubject实例的login,所以我们跟踪到DelegatingSubject的login
在该方法中,有源码如下
Subject subject = this.securityManager.login(this, token);

3.调用securityManager.login方法

SecurityManager是一个接口,我们追踪login的方法实现其实现类为:DefaultSecurityManager
方法中的源码如下

info = this.authenticate(token);

4.调用SecurityManager.authenticate方法

SecurityManager实现类DefaultSecurityManager中将token转交给自身实现了Ahthenticator接口的authenticate方法
在此方法中,只有简单的一句代码
return this.authenticator.authenticate(token);

5.调用Authenticator.authenticate方法

在上一步的SecurityManager方法中调用了成员变量authenticatorauthenticate方法。
SecurityManagerAuthenticatingSecurityManager的子类
在该类中实现了SecurityManager的authenticate方法,在实现方法中调用了:authenticator.authenticate(token)
成员authenticatorModularRealmAuthenticator类的实例
这个地方非常关键:也就是在这里,SecurityManager把token转交到realm中进行验证。
这里调用了ModularRealmAuthenticator的方法authenticate
但是authenticate方法的实现是在ModularRealmAuthenticator的父类AbstractAuthenticator中实现的
所以我们现在把视角转到AbstractAuthenticator.authenticate方法中。
AbstractAuthenticator.authenticate方法中,我们看到了token被传递给了自身的方法doAuthenticate
源码如下:
info = this.doAuthenticate(token);

6.调用ModularRealmAuthenticator.doAuthenticate方法

通过源码追踪,我们知道,只有ModularRealmAuthenticator类实现了doAuthenticate方法

到了这里,我们就可以看到一些比较熟悉的代码了:

   protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
   
      this.assertRealmsConfigured();
      Collection<Realm> realms = this.getRealms();
      return realms.size() == 1 ? this.doSingleRealmAuthentication((Realm)realms.iterator
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值