Shiro如何使用Ehcache实现Session共享

最近项目中用到的Session共享场景:两个独立应用,希望实现DB层共享用户,而且用户只需要登录一次。

分析:这种场合,不适用单点,因为用户数据并不需要单独在第三方应用管理,而且添加单点也会增加整个系统的复杂度

两种实现思路:Session数据存在DB中或者缓存Ehcache中

决策:考虑到查询效率问题,使用缓存机制。

步骤如下:(已经过实际项目检验,如您遇到问题,请在评论中回复)

1. ehcache.xml配置<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE ehcache>
<ehcache name="shiro" updateCheck="false" monitoring="autodetect"
    dynamicConfig="true">
    <diskStore path="java.io.tmpdir/ehcache" />
    <!-- 默认缓存配置. 自动失效:最后一次访问时间间隔300秒失效,若没有访问过自创建时间600秒失效。->
    <defaultCache maxEntriesLocalHeap="1000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="600"
        overflowToDisk="true" statistics="true" />
    
    <!-- 会话缓存 -->
    <cache name="shiro-activeSessionCache" maxEntriesLocalHeap="10000"
           eternal="true"
           timeToIdleSeconds="0" timeToLiveSeconds="0"
           overflowToDisk="true" statistics="true" >
        <cacheEventListenerFactory
                class="net.sf.ehcache.distribution.RMICacheReplicatorFactory"/>
    </cache>

    <!-- 监听服务,用户监听其它应用通过过来的session操作 -->
    <cacheManagerPeerListenerFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerListenerFactory"
            properties="hostName=localhost, port=40002,
            socketTimeoutMillis=2000">
    </cacheManagerPeerListenerFactory>
    <!-- 提供服务,当本应用对session操作时,会复制到其它应用(如果是多个应用则需要在rmiUrls属性写多个url,并用中竖线分割 -->
    <cacheManagerPeerProviderFactory
            class="net.sf.ehcache.distribution.RMICacheManagerPeerProviderFactory"
            properties="peerDiscovery=manual,
            rmiUrls=//localhost:40001/shiro-activeSessionCache">
    </cacheManagerPeerProviderFactory>

</ehcache>

2. 配置(ini或者config)

1)配置CacheManager

/**
     * 核心:SecurityManager,权限管理,这个类组合了登陆,登出,权限,session的处理,是个比较重要的类。
     * 依赖项设置:Realm,SessionManager,CacheManager
     */
    @Bean(name = "securityManager")
    public DefaultWebSecurityManager defaultWebSecurityManager(SystemAuthorizingRealm systemAuthorizingRealm,
            DefaultWebSessionManager sessionManager, EhCacheManager examCacheManager) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(systemAuthorizingRealm);
        defaultWebSecurityManager.setSessionManager(sessionManager);
        EhCacheManager ehCacheManager = new EhCacheManager();
        ehCacheManager.setCacheManagerConfigFile("classpath:ehcache.xml");
        defaultWebSecurityManager.setCacheManager(examCacheManager);
        return defaultWebSecurityManager;
    }

2)配置SessionDAO

@Bean(name = "sessionDao")
    public EnterpriseCacheSessionDAO sessionDao(){
        EnterpriseCacheSessionDAO sessionDao = new EnterpriseCacheSessionDAO();
        sessionDao.setActiveSessionsCacheName("shiro-activeSessionCache");
        return sessionDao;
    }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Shiro-Redis 是一个用于在 Shiro实现 Session 共享的插件,它使用 Redis 作为数据存储和缓存,以实现分布式环境下的 Session 共享。 要实现 Shiro-Redis 的 Session 共享,你需要进行以下步骤: 1. 引入 Shiro-Redis 插件依赖:在项目的 Maven 或 Gradle 配置文件中添加 Shiro-Redis 依赖。 2. 配置 Redis 连接信息:在项目的配置文件中配置 Redis 的连接信息,包括主机名、端口号、密码等。 3. 配置 RedisSessionDAO:在 Shiro 的配置文件中配置 RedisSessionDAO,指定使用 Redis 作为 Session 存储和缓存的实现。可以设置过期时间、前缀等参数。 4. 配置 Session Manager:在 Shiro 的配置文件中配置 Session Manager,指定使用自定义的 RedisSessionManager 作为 Session 的管理器。同时,需要将之前配置的 RedisSessionDAO 设置给 RedisSessionManager。 5. 配置 SecurityManager:在 Shiro 的配置文件中配置 SecurityManager,指定使用自定义的 RedisSessionManager 作为 Session 管理器。同时,需要将之前配置的 RedisSessionDAO 设置给 RedisSessionManager。 6. 配置 Filter Chain:在 Shiro 的配置文件中配置 Filter Chain,将自定义的 RedisSessionManager 添加到 Filter Chain 中,以便对请求进行 Session 管理。 通过以上步骤配置完成后,Shiro 将会使用 Redis 进行 Session 的存储和缓存,从而实现 Session共享。在分布式环境中,不同应用节点之间可以通过 Redis 共享 Session 数据,从而实现用户的登录状态和会话信息的共享

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值