前言
传送门:
1、Shiro如何实现登录?
1.1 请求进入Controller 调用subject.login()
1.2 来到DelegatingSubject类,很明显还是securityManager干活
1.3 DefaultWebSecurityManager
该方法里会验证登录信息,如果登录通过会返回登录信息,如不通过则抛出异常;authenticate方法定义在父类AuthenticatingSecurityManager中
1.4 AuthenticatingSecurityManager
private Authenticator authenticator = new ModularRealmAuthenticator();
可以看到AuthenticatingSecurityManager将authenticate方法交给了ModularRealmAuthenticator类来完成
而ModularRealmAuthenticator 中的authenticate方法是继承自AbstractAuthenticator
doAuthenticate是一个抽象方法
protected abstract AuthenticationInfo doAuthenticate(AuthenticationToken var1) throws AuthenticationException;
由子类ModularRealmAuthenticator来实现
protected AuthenticationInfo doAuthenticate(AuthenticationToken authenticationToken) throws AuthenticationException {
this.assertRealmsConfigured();
Collection<Realm> realms = this.getRealms();
return realms.size() == 1 ? this.doSingleRealmAuthentication((Realm)realms.iterator().next(), authenticationToken) : this.doMultiRealmAuthentication(realms, authenticationToken);
}
这里可以看见Realm了,这里对单个Realm和多个Realm区分处理
我们直接看一下单个Realm如何处理的
这里就是真正调用Realm来实现登录了
看一下getAuthenticationInfo就懂了
第一个标记是从缓存中查看是否登录,
第二个标记就是自定义Realm中的身份认证方法了
第三个标记把此次的登录信息和 第二个标记中的身份认证方法从数据库中查询到的信息进行对比,信息一致则登录成功