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;
}
就这么简单!
Shiro如何使用Ehcache实现Session共享的更多相关文章
- Shiro+Redis实现tomcat集群session共享
一.背景 当我们使用了nginx做项目集群以后,就会出现一个很严重的问题亟待解决,那就是:tomcat集群之间如何实现session共享的问题,如果这个问题不解决,就会出现登陆过后再次请求资源依旧 ...
- JEECG & JEESite Tomcat集群 Session共享
多台tomcat服务的session共享 memcached与redis - JEECG开源社区 - CSDN博客https://blog.csdn.net/zhangdaiscott/article ...
- shiro实现session共享
session共享:在多应用系统中,如果使用了负载均衡,用户的请求会被分发到不同的应用中,A应用中的session数据在B应用中是获取不到的,就会带来共享的问题. 假设:用户第一次访问,连接的A服务器 ...
- Apache shiro集群实现 (六)分布式集群系统下的高可用session解决方案---Session共享
Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...
- Spring boot整合redis实现shiro的分布式session共享
我们知道,shiro是通过SessionManager来管理Session的,而对于Session的操作则是通过SessionDao来实现的,默认的情况下,shiro实现了两种SessionDao,分 ...
- shiro实现session共享(本文转自店蛋蛋)
session共享:在多应用系统中,如果使用了负载均衡,用户的请求会被分发到不同的应用中,A应用中的session数据在B应用中是获取不到的,就会带来共享的问题. 假设:用户第一次访问,连接的A服务器 ...
- shiro源码篇 - shiro的session共享,你值得拥有
前言 开心一刻 老师对小明说:"乳就是小的意思,比如乳猪就是小猪,乳名就是小名,请你用乳字造个句" 小明:"我家很穷,只能住在40平米的乳房" 老师:" ...
- Shiro权限管理框架(二):Shiro结合Redis实现分布式环境下的Session共享
首发地址:https://www.guitu18.com/post/2019/07/28/44.html 本篇是Shiro系列第二篇,使用Shiro基于Redis实现分布式环境下的Session共享. ...
- SpringBoot搭建基于Apache Shiro+Redis的分布式Session共享功能
我们在上一遍文档中已经完成了Shiro验证功能.(http://www.cnblogs.com/nbfujx/p/7773789.html),在此基础上我们将完成分布式Session共享功能. Red ...
随机推荐
- Sql的行列转换
创建表scores 一.传统的行列转换 纵表转横表 我们要转成的横表是这样子的: pivot是sql server 2005 提供的运算符,所以只要数据库在05版本以上的都可以使用.主要用于行和列的转 ...
- MVC错误(一)
目录规范后将相应的文件放到该放的地方后,应修改require_once();中的路径 调试php代码时先打开WampServer,再在浏览器中输入localhost/文件夹名/index.php
- Transferring Data Between ASP.NET Web Pages
14 July 2012 20:24 http://www.mikesdotnetting.com/article/192/transferring-data-between-asp-net-web- ...
- css常用技巧:input提示文字;placeholder字体修改
1 很多网站都需要更改 <input>内部的placeholder 文字颜色属性:下面来介绍下这个技巧. 2 源代码: <!DOCTYPE html><html> ...
- How To Start Building Spatially Aware Apps With Google’s Project Tango
How To Start Building Spatially Aware Apps With Google’s Project Tango “Tango can enable a whole new ...
- RPM管理,计划任务与性能监控
=========== RPM 软件包管理: 相关命令: rpm -ivh 软件包名称 rpm -e 软件包名称 rpm -U 软件名称 软件包信息查询: 相关命令: rpm -q 查询指定软 ...
- Java反射api详解
虽然已经用了很多反射相关的api,但是还没有详细研究官方api文档,下面以1.6为基础研究一下api文档(因为只找到了1.6的中文版...),同时补充1.7和1.8对反射相关api的改变.
- Rabbimq必备基础之对高级消息队列协议AMQP分析及Rabbitmq本质介绍
MQ的一个产品... [消息队列] 1. MSMQ windows自带的一个服务... [petshop],message存放在文件系统中. 最原始的消息队列... [集群,消息确认,内存化,高可用, ...
- Replication--Alwayson+复制发布
场景:主服务器:RepServer1从服务器:RepServer2监听者:RepListener分发库:DisServer发布数据库:RepDB1配置:1>设置DisServer为分发服务器,在 ...
- redis详细说明
# By default Redis does not run as a daemon. Use 'yes' if you need it.# Note that Redis will write a ...