Shiro认证流程详解(源码分析)

Shiro认证流程详解

首先,我们调用subject.login(token);

我们将进入到Subject实现类DelegatingSubject的login方法
在这里插入图片描述
可以看到,又继续调用与Subject绑定的SecurityManager的login方法,因为它是Shiro的管家,所以请求会到它这里

程序继续来到SecurityManager的实现类DefaultSecurityManager的login方法中
在这里插入图片描述
可以看到,SecurityManager将请求交给了内部的认证器Authenticator来执行
在这里插入图片描述
继续来到AbstractAuthenticator中的authenticate方法
又调用了doAuthenticate方法,该方法在ModularRealmAuthenticator中
在这里插入图片描述
如果是单Realm模式,将调用doSingleRealmAuthentication方法

在这里插入图片描述
接下来调用Realm的getAuthenticationInfo方法获取信息
(多Realm只是循环调用getAuthenticationInfo方法,然后根据认证策略判断结果,操作步骤差不多)
在这里插入图片描述
程序进入AuthenticatingRealm并调用doGetAuthenticationInfo方法(该方法由各个Realm实现,一般是自定义,根据token(用户输入的用户名密码)获取数据并判断数据然后返回AuthenticationInfo对象)
在这里插入图片描述

Subject执行登录操作
提交到SecurityManager
SecurityManager将请求交给Authenticator
Authenticator从Realm获取数据
调用CredentialsMatcher对输入的数据与从Realm获取的数据进行比对

——上面两步是认证的关键,如果这两步抛出了异常表示认证失败!

剩下的就是往回看就行了。