shiro登錄認證過程

shiro登錄認證過程

登錄方法

可以看到已經獲取到了username、password和rememberMe ,爲了接下來的認證過程,我們需要獲取subject對象,也就是代表當前登錄用戶,並且要將username和password、rememberMe 兩個變量設置到UsernamePasswordToken對象的token中, 調用SecurityUtils.getSubject().login(token)方法,將 token傳入;
下面來看 subject.login(token)方法

主要通過securityManager安全管理器調用securityManager.login(this, token);方法,下面來看

方法中定義了AuthenticationInfo對象來接受從Realm傳來的認證信息,進入authenticate(token)方法中


繼續跟進去,進入authenticator.authenticate(token)方法

再繼續跟進去看doAuthenticate(token)方法的實現

其中,this.assertRealmsConfigured();是判斷當前的realm是否存在,不存在則拋出異常

當前項目中只配置了一個realm,則會進入doSingleRealmAuthentication((Realm)realms.iterator().next(), authenticationToken)方法,並且會將 realm和token作爲參數傳入,這裏的realm其實就是自己定義的UserRealm,繼續進入doSingleRealmAuthentication方法

這裏會先判斷realm是否支持token,然後進入else方法執行realm.getAuthenticationInfo(token)方法,繼續跟進

this.getCachedAuthenticationInfo(token)這個方法是從shiro緩存中讀取用戶信息,如果沒有,才從realm中獲取信息。如果是第一次登陸,緩存中肯定沒有認證信息,所以會執行this.doGetAuthenticationInfo(token)這個方法,

在執行登錄認證的時候需要選擇我們自己實現的realm方法

讀取數據庫信息進行驗證,並封裝成SimpleAuthenticationInfo中返回
再次查看getAuthenticationInfo

assertCredentialsMatch(token, info)方法用於密碼校驗,點進去可以看到

cm.doCredentialsMatch(token, info)執行密碼校驗

點進去可以看到

通過從token中取出的密碼與從數據庫取出的info中的密碼進行比較,認證相同返回true;失敗就返回false,並拋出AuthenticationException,將info返回到defaultSecurityManager中,到此認證過程結束
參考文獻

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