shiro分布式session共享

article/2025/9/17 10:29:26

当我们开发的程序需要跑在多个tomcat容器或者多台机器上时,shiro的默认session存储就不能满足我们的需求了,其中shiro默认的session是存储在运行jvm内存中的,使用的AbstractSessionDAO抽象类的一个子类MemorySessionDAO,当我们需要做分布式session,可以将session存储在redis上,即只要重写AbstractSessionDAO抽象类,重写里面的方法。

将上一篇的博客添加如下几个类(SerializeUtils、RedisManager、RedisSessionDao),添加的一个配置文件(applicationContext-redis.xml) ,修改了的配置文件(applicationContext-shiro.xml、pom.xml)。

现在工程结构:

这里写图片描述

一个序列化工具类SerializeUtils:

/*** 序列化工具类*/
public class SerializeUtils {private static final JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();/*** 序列化对象** @param obj* @return*/public static <T> byte[] serialize(T obj) {try {return jdkSerializationRedisSerializer.serialize(obj);} catch (Exception e) {throw new RuntimeException("序列化失败!", e);}}/*** 反序列化对象** @param bytes 字节数组* @return*/@SuppressWarnings("unchecked")public static <T> T deserialize(byte[] bytes) {try {return (T) jdkSerializationRedisSerializer.deserialize(bytes);} catch (Exception e) {throw new RuntimeException("反序列化失败!", e);}}
}

redisManager类:

public class RedisManager {@Autowiredprivate RedisTemplate<Serializable, Serializable> redisTemplate;/*** 过期时间*/
//  private Long expire;/*** 添加缓存数据(给定key已存在,进行覆盖)** @param key* @param obj* @throws DataAccessException*/public <T> void set(String key, T obj) throws DataAccessException {final byte[] bkey = key.getBytes();final byte[] bvalue = SerializeUtils.serialize(obj);redisTemplate.execute(new RedisCallback<Void>() {@Overridepublic Void doInRedis(RedisConnection connection) throws DataAccessException {connection.set(bkey, bvalue);return null;}});}/*** 添加缓存数据(给定key已存在,不进行覆盖,直接返回false)** @param key* @param obj* @return 操作成功返回true,否则返回false* @throws DataAccessException*/public <T> boolean setNX(String key, T obj) throws DataAccessException {final byte[] bkey = key.getBytes();final byte[] bvalue = SerializeUtils.serialize(obj);boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {@Overridepublic Boolean doInRedis(RedisConnection connection) throws DataAccessException {return connection.setNX(bkey, bvalue);}});return result;}/*** 添加缓存数据,设定缓存失效时间** @param key* @param obj* @param expireSeconds 过期时间,单位 秒* @throws DataAccessException*/public <T> void setEx(String key, T obj, final long expireSeconds) throws DataAccessException {final byte[] bkey = key.getBytes();final byte[] bvalue = SerializeUtils.serialize(obj);redisTemplate.execute(new RedisCallback<Boolean>() {@Overridepublic Boolean doInRedis(RedisConnection connection) throws DataAccessException {connection.setEx(bkey, expireSeconds, bvalue);return true;}});}/*** 获取key对应value** @param key* @return* @throws DataAccessException*/public <T> T get(final String key) throws DataAccessException {byte[] result = redisTemplate.execute(new RedisCallback<byte[]>() {@Overridepublic byte[] doInRedis(RedisConnection connection) throws DataAccessException {return connection.get(key.getBytes());}});if (result == null) {return null;}return SerializeUtils.deserialize(result);}/*** 删除指定key数据** @param key* @return 返回操作影响记录数*/public Long del(final String key) {if (StringUtils.isEmpty(key)) {return 0l;}Long delNum = redisTemplate.execute(new RedisCallback<Long>() {@Overridepublic Long doInRedis(RedisConnection connection) throws DataAccessException {byte[] keys = key.getBytes();return connection.del(keys);}});return delNum;}public Set<byte[]> keys(final String key) {if (StringUtils.isEmpty(key)) {return null;}Set<byte[]> bytesSet = redisTemplate.execute(new RedisCallback<Set<byte[]>>() {@Overridepublic Set<byte[]> doInRedis(RedisConnection connection) throws DataAccessException {byte[] keys = key.getBytes();return connection.keys(keys);}});return bytesSet;}}

自定义的sessionDao:

public class RedisSessionDao extends AbstractSessionDAO {private Logger logger = LoggerFactory.getLogger(this.getClass());private RedisManager redisManager;/*** The Redis key prefix for the sessions*/private static final String KEY_PREFIX = "shiro_redis_session:";@Overridepublic void update(Session session) throws UnknownSessionException {this.saveSession(session);}@Overridepublic void delete(Session session) {if (session == null || session.getId() == null) {logger.error("session or session id is null");return;}redisManager.del(KEY_PREFIX + session.getId());}@Overridepublic Collection<Session> getActiveSessions() {Set<Session> sessions = new HashSet<Session>();Set<byte[]> keys = redisManager.keys(KEY_PREFIX + "*");if (keys != null && keys.size() > 0) {for (byte[] key : keys) {Session s = (Session) SerializeUtils.deserialize(redisManager.get(SerializeUtils.deserialize(key)));sessions.add(s);}}return sessions;}@Overrideprotected Serializable doCreate(Session session) {Serializable sessionId = this.generateSessionId(session);this.assignSessionId(session, sessionId);this.saveSession(session);return sessionId;}@Overrideprotected Session doReadSession(Serializable sessionId) {if (sessionId == null) {logger.error("session id is null");return null;}Session s = (Session) redisManager.get(KEY_PREFIX + sessionId);return s;}private void saveSession(Session session) throws UnknownSessionException {if (session == null || session.getId() == null) {logger.error("session or session id is null");return;}//设置过期时间long expireTime = 1800000l;session.setTimeout(expireTime);redisManager.setEx(KEY_PREFIX + session.getId(), session, expireTime);}public void setRedisManager(RedisManager redisManager) {this.redisManager = redisManager;}public RedisManager getRedisManager() {return redisManager;}

添加redis的配置 applicationContext-redis.xml:

<beans xmlns="http://www.springframework.org/schema/beans"xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-3.0.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx-3.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.0.xsdhttp://www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-3.0.xsd"><bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"><property name="maxIdle" value="1"/><property name="maxTotal" value="5"/><property name="blockWhenExhausted" value="true"/><property name="maxWaitMillis" value="30000"/><property name="testOnBorrow" value="true"/></bean><bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"><property name="hostName" value="192.168.80.123"/><property name="port" value="6379"/><property name="poolConfig" ref="jedisPoolConfig"/><property name="usePool" value="true"/></bean><bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"><property name="connectionFactory" ref="jedisConnectionFactory"/><property name="keySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/></property><property name="valueSerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/></property><property name="hashKeySerializer"><bean class="org.springframework.data.redis.serializer.StringRedisSerializer"/></property><property name="hashValueSerializer"><bean class="org.springframework.data.redis.serializer.JdkSerializationRedisSerializer"/></property></bean></beans>

pom添加:

        <dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId><version>1.6.2.RELEASE</version></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>2.9.0</version></dependency>

修改applicationContext-shiro.xml文件,在会话管理器注入自定义的redisSessionDao

     <!-- 会话管理器 --><bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"><property name="sessionDAO" ref="redisSessionDao"/><!-- session的失效时长,单位毫秒 --><property name="globalSessionTimeout" value="600000"/><!-- 删除失效的session --><property name="deleteInvalidSessions" value="true"/></bean>

redisSessionDao注入配置:

    <bean id="redisSessionDao" class="com.lijie.shiro.RedisSessionDao"><property name="redisManager" ref="redisManager"/></bean>

以及redisSessionDao依赖的RedisManager注入

<bean id="redisManager" class="com.lijie.redis.RedisManager"></bean>

以后session的序列化和反序列化就不会走shiro默认的单机内存,而是走redis,这样就实现了分布式session共享。


http://chatgpt.dhexx.cn/article/SK50ByoW.shtml

相关文章

tomcat+redis实现session共享

注意&#xff1a;在实际应用中&#xff0c;发现该方案会不定期导致Tomcat假死&#xff08;怀疑Redis连接未释放&#xff09;&#xff0c;慎用。 服务器192.168.14.132和192.168.14.133&#xff0c; 均已经安装tomcat&#xff0c;tomcat安装过程不再赘述。 采用192.168.14.132…

Session共享问题

Session共享及Session保持或者叫做Session⼀致性 1、Session问题原因分析 出现这个问题的原因&#xff0c;从根本上来说是因为Http协议是无状态的协议。客户端和服务端在某次会话中产生的数据不会被保留下来&#xff0c;所以第⼆次请求服务端无法认识到你曾经来过&#xff0c;…

session共享(redis实现)

引言 大厂很多项目都是部署到多台服务器上&#xff0c;这些服务器在各个地区都存在&#xff0c;当我们访问服务时虽然执行的是同一个服务&#xff0c;但是可能是不同服务器运行的&#xff1b; 在我学习项目时遇到这样一个登录情景&#xff0c;假设有如下三台服务器&#xff0…

如何实现session共享的几种解决方案?

先了解一下为什么会出现这种session共享的解决方案&#xff1f; 随着互联网公司的项目在微服务和分布式的环境下进行的搭建&#xff0c;导致一个项目可能分别部署在几个甚至很多的服务器集群下&#xff0c;此时就会出现一个问题当用户进行一个session会话的时候&#xff0c;比…

session共享几种方式

Session共享三种方式 1.nginx基于ip_hash负载均衡。 只需要更改nginx.conf配置文件。添加ip_hash就可以了。 缺点&#xff1a;1.由于ip_hash分配tomcat的时候用的是除法&#xff0c;所以新添加一台服务器会导致分配不到原来程序上&#xff0c;session会丢失。2.同一个公网ip…

session共享学习

一、Session共享 1 什么是Session共享    是指在一个浏览器对应多个Web服务时&#xff0c;服务端的Session数据需要共享。 2 Session共享应用场景 单点登录Web服务器集群等 3 Session共享常见的解决方案 3.1 Session复制       通过对应用服务器的配置开启服务器的Sess…

redis实现session共享

session共享 什么是session&#xff1f; 由于 HTTP 协议是无状态的协议&#xff0c;所以服务端需要记录用户的状态时&#xff0c;就需要用某种机制来识具体的用户。Session 是另一种记录客户状态的机制&#xff0c;不同的是 Cookie 保存在客户端浏览器中&#xff0c;而 Sessi…

UART协议概述与实现

UART协议概述&#xff08;一&#xff09; 协议描述关键代码逻辑写在最后 协议描述 常见的三大低速通信协议之一&#xff0c;UART&#xff0c;通用异步收发协议。非常简单的协议&#xff0c;协议细节不需要多说&#xff0c;只说需要注意的点。空闲位为高位&#xff0c;起始位为…

UART协议简述及编程

UART原理简述 通用异步收发器简称UART&#xff08;universal asynchronous receiver transmitter&#xff09;。他的用途很广泛&#xff0c;一般常用的就是用来输出打印的信息&#xff0c;也可以外接各种模块&#xff0c;例如GPS和蓝牙等。 正是因为这种协议非常的简单可靠&…

UART协议及串口回环

UART协议及串口回环 一、异步通信的分类1、UART&#xff08;通用异步收发器&#xff09;2、RS4223、RS4854、Modbus5、接口标准 二、UART协议要求1、空闲状态2、起始位3、数据位4、校验位5、停止位6、波特率7、比特率 三、汉字发送四、串口回环uart_txuart_rxctrltop 五、参考六…

基于verilog的uart协议实现

目录 1、理论介绍 2、架构设计 3、代码设计 一、发送模块代码 二、接收代码设计 三、顶层模块设计 四、测试代码 4、仿真实验 1、理论介绍 uart&#xff1a;通用异步收发传输器&#xff08;Universal Asynchronous Receiver/Transmitter)&#xff0c;是一种串行的收发方式…

UART协议快速扫盲(图文并茂+超详细)

文章目录 1 UART发展历史1.1 早期的串行通讯设备1.2 早期的芯片级UART1.3 现代UART的发展 2 预备知识3 协议层起始位数据校验位停止位波特率 4 传输过程5 物理层6 优缺点 1 UART发展历史 1.1 早期的串行通讯设备 早期的电报机器使用长度可变的脉冲信号进行数据传输&#xff0…

UART协议学习

通信协议分层 物理层 物理层规定通讯系统具有的机械、电子功能部分的特性&#xff0c;确保原始数据在物理媒体的传输。如RS232、RS485等就是电气协议&#xff0c;规定了数据传输时的电平标准&#xff0c;网络上许多博主将UART看作一个协议族&#xff0c;这些电气协议都是UART…

c语言模拟uart协议的收发

这篇文章注重思想的讲解,理解下来肯定对uart协议有一个更深的认识。 uart协议,通常用在嵌入式设备之间的通信。像下面这样: 问题一:uart是全双工还是半双工? 你完全可以将两个设备想象成两个人,上图中的两条线想象成A和B的对话。A对B说话和B对A说话的一个场景。问大家一个问题…

UART协议详解

通用异步收发传输器&#xff08;Universal Asynchronous Receiver/Transmitter)&#xff0c;通常称作UART。 定义&#xff1a;UART是一种通用串行数据总线&#xff0c;用于异步通信。该总线双向通信&#xff0c;可以实现全双工传输和接收。在嵌入式设计中&#xff0c;UART用于主…

Linux·UART协议

目录 一、什么是UART&#xff1f; 二、UART的帧格式 2.1 为什么UART的传输需要起始位&#xff1f; 2.2 UART基本的数据形式 2.3 为什么UART的数据位可变&#xff1f; 三、UART的波特率 3.1 什么是波特率 3.2 如何换算波特率 3.3 波特率和采样频率是一样的吗&#xff1f…

通信协议(一)——UART协议

1、知识点 基础部分参考&#xff1a;UART串口发送模块设计Verilog_发光中请勿扰的博客-CSDN博客_uart设计verilog &#xff08;1&#xff09;什么是串口&#xff08;UART&#xff09;&#xff1f; 串口作为常用的三大低速总线&#xff08;UART、SPI、IIC&#xff09;之一&#…

uart协议学习,从了解到入门,看这篇文章

uart协议从了解到入门 背景知识介绍&#xff1a;1、并行和串行的意思&#xff1a;2、串转并和并转串传输&#xff1a;3、单工、半双工、全双工区别&#xff1a; uart协议介绍1、uart简介2、uart通信3、uart工作原理 uart的优缺点 背景知识介绍&#xff1a; 1、并行和串行的意思…

FPGA实现uart协议

简介 使用verilog实现uart协议&#xff0c;能够和pc进行通信&#xff0c;实现串口回环功能&#xff0c;各参数设置如下&#xff1a; 波特率&#xff1a;115200数据位&#xff1a;8停止位&#xff1a;任意校验位&#xff1a;无 系统时钟为50M&#xff0c;115200波特率下&…

协议篇之UART协议

协议篇之UART协议 一、写在前面二、UART协议简介三、UART协议数据帧结构3.1 UART发送过程3.2 UART接收过程3.3 UART传输速率 四、UART收发模块设计4.1 UART接收模块设计4.2 UART发送模块设计4.3 UART回环顶层模块4.4 UART回环上板验证 五、写在最后 一、写在前面 由于设计需要&…