目錄
1、關于Shiro
官網介紹
Shiro特點
Shiro基本功能點
Shiro架構
2、快速入門
2.1、Shiro身份驗證
1、關于Shiro
官網介紹
Apache Shiro? is a powerful and easy-to-use Java security framework that performs authentication, authorization, cryptography, and session management. With Shiro’s easy-to-understand API, you can quickly and easily secure any application – from the smallest mobile applications to the largest web and enterprise applications.
Apache Shiro?是一個強大且易用的 Java 安全框架, 能夠用于身份驗證、授權、加密和會話管理,Shiro 擁有易于理解的 API, 您可以快速、輕松地獲得任何應用程式——從最小的移動應用程式到最大的網路和企業應用程式,
簡而言之,Apache Shiro 是一個強大靈活的開源安全框架,可以完全處理身份驗證、授權、加密和會話管理,
Shiro特點
- 易于使用——易用性是專案的最終目標,應用程式安全非常令人困惑和沮喪, 被認為是 “不可避免的災難”,如果你讓它簡化到新手都可以使用它, 它就將不再是一種痛苦了,
- 全面——沒有其他安全框架的寬度范圍可以同 Apache Shiro 一樣, 它可以成為你的 “一站式” 為您的安全需求提供保障,
- 靈活——Apache Shiro 可以在任何應用程式環境中作業,雖然在網路作業、EJB 和 IoC 環境中可能并不需要它,但 Shiro 的授權也沒有任何規范, 甚至沒有許多依賴關系,
- Web 支持——Apache Shiro 擁有令人興奮的 web 應用程式支持, 允許您基于應用程式的 url 創建靈活的安全策略和網路協議 (例如 REST), 同時還提供一組 JSP 庫控制頁面輸出,
- 低耦合——Shiro 干凈的 API 和設計模式使它容易與許多其他框架和應用程式集成,你會看到 Shiro 無縫地集成 Spring 這樣的框架, 以及 Grails, Wicket, Tapestry, Mule, Apache Camel, Vaadin... 等,
- 被廣泛支持——Apache Shiro 是 Apache 軟體基金會的一部分,專案開發和用戶組都有友好的網民愿意幫助,這樣的商業公司如果需要 Katasoft 還提供專業的支持和服務,
Shiro基本功能點
- Authentication:身份認證 / 登錄,驗證用戶是不是擁有相應的身份;
- Authorization:授權,即權限驗證,驗證某個已認證的用戶是否擁有某個權限;即判斷用戶是否能做事情,常見的如:驗證某個用戶是否擁有某個角色,或者細粒度的驗證某個用戶對某個資源是否具有某個權限;
- Session Management:會話管理,即用戶登錄后就是一次會話,在沒有退出之前,它的所有資訊都在會話中;會話可以是普通 JavaSE 環境的,也可以是如 Web 環境的;
- Cryptography:加密,保護資料的安全性,如密碼加密存盤到資料庫,而不是明文存盤;
- Web Support:Web 支持,可以非常容易的集成到 Web 環境;
- Caching:快取,比如用戶登錄后,其用戶資訊、擁有的角色 / 權限不必每次去查,這樣可以提高效率;
- Concurrency:shiro 支持多執行緒應用的并發驗證,即如在一個執行緒中開啟另一個執行緒,能把權限自動傳播過去;
- Testing:提供測驗支持;
- Run As:允許一個用戶假裝為另一個用戶(如果他們允許)的身份進行訪問;
- Remember Me:記住我,這個是非常常見的功能,即一次登錄后,下次再來的話不用登錄了,
記住一點,Shiro 不會去維護用戶、維護權限;這些需要我們自己去設計 / 提供;然后通過相應的介面注入給 Shiro 即可,
Shiro架構
在概念層,Shiro 架構包含三個主要的理念:Subject,SecurityManager 和 Realm,下面的圖展示了這些組件如何相互作用,我們將在下面依次對其進行描述,

- Subject:當前用戶,Subject 可以是一個人,但也可以是第三方服務、守護行程帳戶、時鐘守護任務或者其它–當前和軟體互動的任何事件,
- SecurityManager:管理所有 Subject,SecurityManager 是 Shiro 架構的核心,配合內部安全組件共同組成安全傘,
- Realms:用于進行權限資訊的驗證,我們自己實作,Realm 本質上是一個特定的安全 DAO:它封裝與資料源連接的細節,得到 Shiro 所需的相關的資料,在配置 Shiro 的時候,你必須指定至少一個 Realm 來實作認證(authentication)和 / 或授權(authorization),
我們需要實作 Realms 的 Authentication 和 Authorization,其中 Authentication 是用來驗證用戶身份,Authorization 是授權訪問控制,用于對用戶進行的操作授權,證明該用戶是否允許進行當前操作,如訪問某個鏈接,某個資源檔案等,
2、快速入門
2.1、Shiro身份驗證

shiro認證流程圖
示例代碼:
1、創建maven工程,引入相關依賴
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
2、創建測驗類
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.realm.SimpleAccountRealm;
import org.apache.shiro.subject.Subject;
import org.junit.Before;
import org.junit.Test;
public class AuthenticationTest {
SimpleAccountRealm simpleAccountRealm = new SimpleAccountRealm();
// 在方法開始前添加一個用戶
@Before
public void addUser() {
simpleAccountRealm.addAccount("admin", "111111");
}
@Test
public void testAuthentication() {
// 1.構建SecurityManager環境
DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager();
defaultSecurityManager.setRealm(simpleAccountRealm);
// 2.主體提交認證請求
SecurityUtils.setSecurityManager(defaultSecurityManager); // 設定SecurityManager環境
Subject subject = SecurityUtils.getSubject(); // 獲取當前主體
UsernamePasswordToken token = new UsernamePasswordToken("admin", "111111");
subject.login(token); // 登錄
// subject.isAuthenticated()方法回傳一個boolean值,用于判斷用戶是否認證成功
System.out.println("isAuthenticated:" + subject.isAuthenticated()); // 輸出true
subject.logout(); // 登出
System.out.println("isAuthenticated:" + subject.isAuthenticated()); // 輸出false
}
}
3、運行并查看結果
運行之后可以看到isAuthenticated先列印true,說明認證成功;第二次列印為false,是因為用戶已登出,認證失敗,
具體的身份驗證流程:

流程如下:
- 首先呼叫
Subject.login(token)進行登錄,其會自動委托給Security Manager,呼叫之前必須通過SecurityUtils.setSecurityManager()設定;SecurityManager負責真正的身份驗證邏輯;它會委托給Authenticator進行身份驗證;Authenticator才是真正的身份驗證者,Shiro API中核心的身份認證入口點,此處可以自定義插入自己的實作;Authenticator可能會委托給相應的AuthenticationStrategy進行多Realm身份驗證,默認ModularRealmAuthenticator會呼叫AuthenticationStrategy進行多Realm身份驗證;Authenticator會把相應的token傳入Realm,從Realm獲取身份驗證資訊,如果沒有回傳 / 拋出例外表示身份驗證失敗了,此處可以配置多個Realm,將按照相應的順序及策略進行訪問,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/239173.html
標籤:其他
上一篇:三、CSRF-SSRF
