Shiro系列記錄(二)——Shiro登錄源碼分析

前言

傳送門:

Springboot整合Shiro實現登錄登出

Shiro知識點概括

1、Shiro如何實現登錄?

1.1 請求進入Controller  調用subject.login()

1.2 來到DelegatingSubject類,很明顯還是securityManager幹活

1.3 DefaultWebSecurityManager

該方法裏會驗證登錄信息,如果登錄通過會返回登錄信息,如不通過則拋出異常;authenticate方法定義在父類AuthenticatingSecurityManager中

1.4  AuthenticatingSecurityManager

private Authenticator authenticator = new ModularRealmAuthenticator();

可以看到AuthenticatingSecurityManagerauthenticate方法交給了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中的身份認證方法了

第三個標記把此次的登錄信息第二個標記中的身份認證方法從數據庫中查詢到的信息進行對比,信息一致則登錄成功

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章