Shiro认证源码解析和工作原理

编程入门 行业动态 更新时间:2024-10-25 10:28:46

Shiro认证源码解析和<a href=https://www.elefans.com/category/jswz/34/1765046.html style=工作原理"/>

Shiro认证源码解析和工作原理

先行回顾一下使用shiro的步骤:
1. 创建Subject实例对象currUser;
2. 判断当前currUser是否认证过;
3. 如果没有认证过,那么应当调用currUser的login(token)方法,token也就是令牌,用以封装用户输入的登录信息;
4. 实现自定义Realm,用以完成和数据库的交互,由Shiro底层来完成用户输入信息和数据库信息的比对,完成认证。

当然与此同时,还需要完成一些Spring IOC 中bean的配置。

currUser的login()方法

首先Subject是一个接口,当我们查看login方法的源码时,该方法的具体实现是DelegatingSubject来完成的。
在DelegatingSubject类中,我们可以看到以下代码

//这说明login操作事实上是由securityManager来完成的
Subject subject = this.securityManager.login(this, token);

该securityManager已经在我们的Spring容器中配置,并且指定了它的cacheManager以及realm。再向下看,这个securityManager是DefaultSecurityManger的实例,DefaultSecurityManger继承了SessionsSecurityManager,由它来完成login操作。它的login方法

//它需要传入的参数是Subject实例以及一个token
public Subject login(Subject subject, AuthenticationToken token) throws AuthenticationException {AuthenticationInfo info;try {//这里的info就是数据库提取数据的封装info = this.authenticate(token);} catch (AuthenticationException var7) {AuthenticationException ae = var7;try {this.onFailedLogin(token, ae, subject);} catch (Exception var6) {if (log.isInfoEnabled()) {log.info("onFailedLogin method threw an exception.  Logging and propagating original AuthenticationException.", var6);}}throw var7;}Subject loggedIn = this.createSubject(token, info, subject);this.onSuccessfulLogin(token, info, loggedIn);return loggedIn;}

上述方法中,我们可以看到认证由这句代码完成

info = this.authenticate(token);

authenticate方法来自一个抽象类AuthenticatingSecurityManager,它是SessionsSecurityManager的祖父类,该类中有一个属性:

private Authenticator authenticator = new ModularRealmAuthenticator();

方法:

public AuthenticationInfo authenticate(AuthenticationToken token) throws AuthenticationException {return this.authenticator.authenticate(token);}

此处的authenticator就是AbstractAuthenticator,它的authenticate方法,该方法内部再调用doAuthenticate(token),它由AbstractAuthenticator的子类ModularRealmAuthenticator来实现,此处就开始出现数据库交互的Realm:

protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {this.assertRealmsConfigured();//此处已经出现了执行数据库交互的RealmCollection<Realm> realms = this.getRealms();//检查realm集合的尺寸大小,如果只有一个,那么执行doSingle...(,)return realms.size() == 1 ? this.doSingleRealmAuthentication((Realm)realms.iterator().next(), authenticationToken) : this.doMultiRealmAuthentication(realms, authenticationToken);}

doSingleRealmAuthentication((Realm)realms.iterator().next(), authenticationToken):

protected AuthenticationInfo doSingleRealmAuthentication(Realm realm, AuthenticationToken token) {
//检查传入的token是否属于支持的类型,不支持则抛出异常if (!realm.supports(token)) {String msg = "Realm [" + realm + "] does not support authentication token [" + token + "].  Please ensure that the appropriate Realm implementation is " + "configured correctly or that the realm accepts AuthenticationTokens of this type.";throw new UnsupportedTokenException(msg);} else {//如果支持,则调用realm的getAuthenticationInfo方法AuthenticationInfo info = realm.getAuthenticationInfo(token);if (info == null) {String msg = "Realm [" + realm + "] was unable to find account data for the " + "submitted AuthenticationToken [" + token + "].";throw new UnknownAccountException(msg);} else {return info;}}}

关于Realm的getAuthenticationInfo方法,它的内部又调用了doGetAuthenticationInfo(token),这是一个十分眼熟的方法,眼熟的原因在于,我们的自定义Realm就需要重写该方法。

所以,总结一下整个流程,就是,在我们调用currUser的login方法之后,它经历过一系列的步骤,调用了Realm中我们所定义的doGetAuthenticationInfo(token)方法,获取了一个AuthenticationInfo对象,该info或许是null,或者是提取到数据库数据之后创建的对象 ,在此之后就是之前Realm篇谈到的关于token和info比对的过程,这个过程我们再复习一遍,它是在AuthenticatingRealm中有一个assertCredentialsMatcher(token,info)的操作,该操作事实上又调用了HashedCreadentialsMatecher(这里因为我们在spring中的realm配置的是该加密器)的doCredentialsMatcher(token,info)方法。

在这个流程中,重点可以记住,是ModularRealmAuthenticator完成了我们调用Realm的操作。

更多推荐

Shiro认证源码解析和工作原理

本文发布于:2023-06-18 23:14:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/777185.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作原理   源码   Shiro

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!