天天看點

shiro登入認證過程

shiro登入認證過程

登入方法

shiro登入認證過程

可以看到已經擷取到了username、password和rememberMe ,為了接下來的認證過程,我們需要擷取subject對象,也就是代表目前登入使用者,并且要将username和password、rememberMe 兩個變量設定到UsernamePasswordToken對象的token中, 調用SecurityUtils.getSubject().login(token)方法,将 token傳入;

下面來看 subject.login(token)方法

shiro登入認證過程

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

下面來看

shiro登入認證過程

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

shiro登入認證過程

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

shiro登入認證過程

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

shiro登入認證過程

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

shiro登入認證過程

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

shiro登入認證過程

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

shiro登入認證過程

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

shiro登入認證過程

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

shiro登入認證過程

讀取資料庫資訊進行驗證,并封裝成SimpleAuthenticationInfo中傳回

再次檢視getAuthenticationInfo

shiro登入認證過程

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

shiro登入認證過程

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

shiro登入認證過程

點進去可以看到

shiro登入認證過程

通過從token中取出的密碼與從資料庫取出的info中的密碼進行比較,認證相同傳回true;失敗就傳回false,并抛出AuthenticationException,将info傳回到defaultSecurityManager中,到此認證過程結束