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

Shiro 實現 Session 共享與 Cache 共享:基於 Redis 的分佈式會話管理方案

     編輯:Java 綜合教學

Shiro 實現 Session 共享與 Cache 共享:基於 Redis 的分佈式會話管理方案

負載均衡環境下,用戶請求分發到不同應用節點,單機 Session 導致登錄狀態丟失。Shiro 提供 SessionDAO 和 CacheManager 兩大擴展點,結合 Redis 實現分佈式 Session 與緩存共享。

架構設計

┌─────────────┐     ┌─────────────┐     ┌─────────────┐
│  應用節點 A  │     │  應用節點 B  │     │  應用節點 N  │
│  Shiro Session│     │  Shiro Session│     │  Shiro Session│
└──────┬──────┘     └──────┬──────┘     └──────┬──────┘
       │                   │                   │
       └───────────────────┼───────────────────┘
                           ▼
                    ┌─────────────┐
                    │   Redis     │
                    │  (Session/  │
                    │   Cache)    │
                    └─────────────┘

一、Session 共享實現

核心類繼承關係

AbstractSessionDAO (Shiro)
    ↑
CustomShiroSessionDao (自定義)
    ├── create(session)     → Redis 存儲
    ├── readSession(id)     → Redis 讀取
    ├── update(session)     → Redis 更新
    ├── delete(session)     → Redis 刪除
    └── getActiveSessions() → Redis 掃描

SessionDAO 類圖

關鍵設計點

  • 繼承 AbstractSessionDAO 而非實現接口,復用默認邏輯
  • 注入 ShiroSessionRepository 抽象數據訪問,底層可換 Redis/DB
  • Session ID 生成器可配置(UUID/自定義)
  • 序列化:Session 實現 Serializable,用 Java 原生/Protobuf/Kryo

二、Cache 共享實現

核心接口

CacheManager (Shiro)
    ↑
CustomShiroCacheManager (自定義)
    └── getCache(name) → 返回 Cache 實例

Cache (Shiro)
    ↑
JedisShiroCache (Redis 實現)
    ├── get(key) / put(key, value)
    ├── remove(key) / clear()
    └── keys() / values() / size()

CacheManager 類圖

設計亮點

  • ConcurrentHashMap 緩存 Cache 實例,避免重復創建
  • 抽象 ShiroCacheManager,支持多種底層存儲擴展
  • Redis 操作封裝在 RedisManager,統一連接池、TTL、序列化

三、Spring 配置整合

RedisManager 配置

<bean id="redisManager" class="com.example.shiro.util.RedisManager">
    <property name="host" value="127.0.0.1"/>
    <property name="port" value="6379"/>
    <property name="expire" value="1800"/>  <!-- 30分鐘 -->
</bean>

Cache 配置

<bean id="shiroCacheManager" class="com.example.shiro.cache.JedisShiroCacheManager">
    <property name="redisManager" ref="redisManager"/>
</bean>

<bean id="shiroJedisManager" class="com.example.shiro.cache.CustomShiroCacheManager">
    <property name="shiroCacheManager" ref="shiroCacheManager"/>
</bean>

SessionDAO 配置

<bean id="shiroSessionRepository" 
      class="com.example.shiro.session.JedisShiroSessionRepository">
    <property name="redisManager" ref="redisManager"/>
</bean>

<bean id="sessionDAO" class="com.example.shiro.session.CustomShiroSessionDao">
    <property name="sessionIdGenerator" ref="sessionIdGenerator"/>
    <property name="shiroSessionRepository" ref="shiroSessionRepository"/>
</bean>

SecurityManager 綁定

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
    <property name="realm" ref="myRealm"/>
    <property name="sessionManager" ref="sessionManager"/>
    <property name="cacheManager" ref="shiroJedisManager"/>
</bean>

<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
    <property name="sessionDAO" ref="sessionDAO"/>
    <property name="globalSessionTimeout" value="1800000"/>
</bean>

四、完整代碼倉庫

GitHub: https://github.com/zljk0306/shiro-redis-share

總結

Shiro Session/Cache 共享核心:實現 AbstractSessionDAO + CacheManager 接口,底層委託 Redis。抽象 Repository 層解耦存儲實現,配合 Spring 裝配,實現無狀態應用橫向擴展。生產環境注意:Redis 高可用(Sentinel/Cluster)、Session 序列化性能、大 Key 問題、網絡延遲影響。

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