shiro学习(4):shiro认证流程
Shiro登錄校驗流程實現與分析
什么是Shiro
Apache Shiro是一個強大且易用的Java安全框架,執行身份驗證、授權、密碼和會話管理。使用Shiro的易于理解的API,您可以快速、輕松地獲得任何應用程序,從最小的移動應用程序到最大的網絡和企業應用程序。
三個核心組件
Subject, SecurityManager 和 Realms.
①Subject:即“當前操作用戶”。但是,在Shiro中,Subject這一概念并不僅僅指人,也可以是第三方進程、后臺帳戶(Daemon Account)或其他類似事物。它僅僅意味著“當前跟軟件交互的東西”。但考慮到大多數目的和用途,你可以把它認為是Shiro的“用戶”概念。Subject代表了當前用戶的安全操作,SecurityManager則管理所有用戶的安全操作。
②SecurityManager:它是Shiro框架的核心,典型的Facade模式,Shiro通過SecurityManager來管理內部組件實例,并通過它來提供安全管理的各種服務。
③Realm: Realm充當了Shiro與應用安全數據間的“橋梁”或者“連接器”。也就是說,當對用戶執行認證(登錄)和授權(訪問控制)驗證時,Shiro會從應用配置的Realm中查找用戶及其權限信息。
登錄認證過程簡述
①調用subject.login方法進行登錄,其會自動委托給securityManager,login方法進行登錄;
②securityManager通過 Authenticator(認證器)進行認證
③Authenticator的實現ModularRealmAuthenticaton調用realm從ini配置文件取用戶真實的賬號和密碼,這里使用的是IniRealm ( shiro自帶,相當于數據源) ;
?
④IniRealm先根據token中的賬號去ini中找該賬號,如果找不到則給ModularRealmAuthenticator返回null ,如果找到則匹配密碼,匹配密碼成功則認證通過。
image.png
具體實現
<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><!-- https://mvnrepository.com/artifact/commons-logging/commons-logging --><dependency><groupId>commons-logging</groupId><artifactId>commons-logging</artifactId><version>1.1.3</version></dependency><!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core --><dependency><groupId>org.apache.shiro</groupId><artifactId>shiro-core</artifactId><version>1.4.0</version></dependency>image.png
package com.swl;import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.config.IniSecurityManagerFactory; import org.apache.shiro.mgt.SecurityManager; import org.apache.shiro.subject.Subject; import org.apache.shiro.util.Factory; import org.junit.Test;/*** <Description>ShiroTest<br>** @author DaShi<br>* @CreateDate 2019-02-15 09:43 <br>*/ public class ShiroTest {@Testpublic void loginTest() throws Exception{//創建IniSecurityManager工程對象:加載配置文件Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini");//通過工廠對象創建SecurityManager對象SecurityManager securityManager = factory.getInstance();// 將SecurityManager綁定到當前運行環境中,讓系統隨時可以訪問SecurityManager對象SecurityUtils.setSecurityManager(securityManager);//創建登錄主體 注意:此時主體沒有經過驗證,僅僅是個空的對象Subject subject = SecurityUtils.getSubject();//綁定主體登陸的身份、憑證 即賬號密碼UsernamePasswordToken token = new UsernamePasswordToken("zhangsan","666");//主體登陸try{subject.login(token);}catch (Exception e){//拋錯誤}//登陸判斷System.out.println("登錄結果" + subject.isAuthenticated() );//登出判斷subject.logout();System.out.println("登出結果" + subject.isAuthenticated());} }結果:
?
image.png
源碼分析
?
?
①我們在登錄操作時打好斷點,debug運行
image.png
?
②我們進入到subject主體的實現類中,注意標記部分,subject將登錄操作委托給了securitymanager,參數除了頁面傳入的token以外還有subject主體
image.png
③進入到securitymanager實現類中,開始執行認證方法
image.png
④點進去看一下,我們發現securitymanager將認證任務委托給了認證器,由認證器執行認證方法
image.png
?
⑤我們進入到認證方法中
image.png
⑥繼續執行doAuthenticate,我們發現這里面有個realm,他是啥呢?
image.png
我們看到realms這個集合里面其實就是我們之前加載的realms.ini中的內容
image.png
⑦因為我們只有一個realm,所以進入了true條件中,這時我們剛才標記出的AuthenticationInfo出現了,看看他是啥
image.png
點進去看看
image.png
看不懂,繼續進去看看,我們看到他先把我們的token包裝成了 UsernamePasswordToken的形式 然后執行了getuser()參數是我們頁面傳進來的token的name
image.png
⑧我們看一下里面是什么,是通過頁面傳入的那個用戶名,去我們剛才realm里面的user列表去查找有沒有這個用戶 如果找到了,將realm中該用戶信息返回上一層
image.png
?
⑨這時我們可以看到account中包含了張三的信息
image.png
⑩接下來我們繼續返回上一層,因為我們已經在realm中找到了頁面輸入的token的用戶名,下一步進行密碼驗證
image.png
密碼驗證就很簡單了,重點就是這個判斷
image.png
其實就是把我們剛才取得的那個realm中的張三的密碼和我們頁面傳入的密碼比較
image.png
好了整個過程大概就是這個樣子啦!!!
總結
以上是生活随笔為你收集整理的shiro学习(4):shiro认证流程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 驱动开发入门 - 之二:Win7-x64
- 下一篇: hashmap 从头到尾_如何从头到尾设