首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 綜合教學

Apache Shiro 核心架構深度解析:Subject、SecurityManager、Realm 三大組件交互圖解

     編輯:Java 綜合教學

Apache Shiro 核心架構深度解析:Subject、SecurityManager、Realm 三大組件交互圖解

Shiro 設計哲學:「以用戶為中心的安全模型」。本文深度解析 Shiro 核心架構圖,拆解 Subject、SecurityManager、Realm 及其內部組件 (Authenticator、Authorizer、SessionManager、CacheManager、Cryptography) 的職責與交互流程,為後續整合配置打下理論基礎。

一、高層架構三大核心

┌─────────────────────────────────────────────────────────────┐
│                      Application                            │
└──────────────────────────┬──────────────────────────────────┘
                           │ 調用 Subject API
                           ▼
┌─────────────────────────────────────────────────────────────┐
│  Subject (當前用戶/進程/任務)                                │
│  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐          │
│  │ Principals  │  │ Credentials │  │  Permissions│          │
│  └─────────────┘  └─────────────┘  └─────────────┘          │
└──────────────────────────┬──────────────────────────────────┘
                           │ 委託
                           ▼
┌─────────────────────────────────────────────────────────────┐
│  SecurityManager (核心大腦)                                 │
│  ┌────────────┐ ┌────────────┐ ┌──────────┐ ┌────────────┐  │
│  │Authenticator│ │ Authorizer │ │SessionMgr│ │ CacheMgr   │  │
│  └────────────┘ └────────────┘ └──────────┘ └────────────┘  │
│  ┌────────────┐ ┌────────────┐ ┌────────────┐               │
│  │  Realms    │ │ Cryptography│ │ EventBus  │               │
│  └────────────┘ └────────────┘ └────────────┘               │
└──────────────────────────┬──────────────────────────────────┘
                           │ 協調
                           ▼
┌─────────────────────────────────────────────────────────────┐
│  Realms (數據源適配器)  ←→  DB / LDAP / INI / ActiveDirectory │
└─────────────────────────────────────────────────────────────┘

核心職責

組件職責關鍵點
Subject安全視圖門面,代表當前交互主體綁定 SecurityManager;API 極簡 (login/logout/hasRole/isPermitted)
SecurityManager安全操作總調度器,管理所有組件生命週期單例;配置完成後開發者極少直接調用
Realm安全數據源橋梁 (DAO 模式)至少一個;可配置多個,按順序嘗試;實現 doGetAuthenticationInfo/doGetAuthorizationInfo

二、SecurityManager 內部組件詳解

SecurityManager
├── Authenticator (認證器)
│   ├── 支援多 Realm 串聯/並聯認證
│   └── AuthenticationStrategy (策略):
│       ├── FirstSuccessfulStrategy (默認,一成功即成功)
│       ├── AtLeastOneSuccessfulStrategy (至少一個成功)
│       └── AllSuccessfulStrategy (全部成功)
├── Authorizer (授權器)
│   ├── 基於角色/權限字符串/通配符的授權決策
│   └── 內建 WildcardPermission (資源:操作:實例)
├── SessionManager (會話管理)
│   ├── 非 Web 環境也可用的原生 Session (獨立於 Servlet)
│   ├── SessionDAO (持久化策略):Memory/EnterpriseCache/Redis/JDBC
│   └── SessionValidationScheduler (定時清理過期 Session)
├── CacheManager (緩存管理)
│   ├── 抽象緩存創建/生命週期 (Ehcache/Redis/Caffeine)
│   └── 緩存認證/授權/Session 數據,極大提升性能
├── Cryptography (加密體系)
│   ├── Hash (MD5/SHA/AES) + Salt + 多次迭代
│   ├── CipherService (對稱/非對稱加解密)
│   └── RandomNumberGenerator (安全隨機數)
└── Realms[] (多數據源協調)
    ├── 支援 JDBC/LDAP/ActiveDirectory/Properties/自定義
    └── 配置順序決定嘗試順序

三、認證/授權流程時序

認證流程

  1. subject.login(token) → SecurityManager.authenticate(token)
  2. Authenticator 遍歷 Realms → realm.getAuthenticationInfo(token)
  3. Realm 查詢 DB → 封裝 AuthenticationInfo (含 principals、credentials、salt)
  4. Authenticator 用 CredentialsMatcher 比對憑證 (含重試限制、加密驗證)
  5. 成功 → 創建 Subject、綁定 Session、觸發登錄事件;失敗 → 拋 AuthenticationException

授權流程

  1. subject.checkPermission("user:delete") → SecurityManager.authorize()
  2. Authorizer 遍歷 Realms → realm.getAuthorizationInfo(principals)
  3. Realm 查詢權限數據 → 封裝 AuthorizationInfo (roles、stringPermissions、objectPermissions)
  4. Authorizer 用 Permission.implies() 匹配 → 通過/拋 AuthorizationException

四、Session 管理獨特優勢

  • 非侵入式:無需 Servlet 容器,Java SE/Swing/FX/微服務均可用
  • 集群就緒:SessionDAO + Redis 實現分佈式 Session 共享
  • 細粒度控制:全局超時、單用戶最大會話數、踢出用戶、會話監聽器

五、配置最佳實踐

  1. Realm 盡量薄:只做數據查詢,加密/重試/緩存交給 SecurityManager 組件
  2. 緩存分區:authenticationCache / authorizationCache / activeSessionCache 分離配置 TTL
  3. 憑證匹配器鏈:RetryLimitHashedCredentialsMatcher → 先重試限制,再哈希驗證
  4. RememberMe 安全:AES 加密 Cookie + 定期輪換密鑰 + HttpOnly + Secure

架構感悟:Shiro 把「安全數據從哪來」(Realm) 與「安全邏輯怎麼判」(Authenticator/Authorizer) 徹底解耦,這才是框架該有的樣子。理解了這層,配置文件就不再是無意義的 XML 堆砌,而是清晰的組件裝配圖。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved