https://bbs.csdn.net/topics/391967054?page=1
核心代码:
RedisHttpSessionConfiguration-》RedisOperationsSessionRepository-》onMessage这里就可以很清楚看到SessionListener是如何起作用的。
然后可以添加其他监听
###########################
SpringMVC+Redis的项目;用户登录后大量的信息缓存在Redis中;用户退出或者Session超时时;清除Redis中缓存的数据。 原本使用自定义Listener implements HttpSessionListener来监听sessionDestroyed事件 并清除Redis中的记录;现由于加了分布式部署 改用Spring Session Redis 来共享Session;Session共享是成功了,但是现在没办法监听sessionDestroyed事件了;看了网上有些说用Spring Security 来管理Session ;extends HttpSessionEventPublisher;搞了几天 没搞懂;求大神些帮帮忙 是否有其他方式可以监听 sessionDestroyed 或者Spring Security 来管理Session 该如何配置??? |
为什么要通过sessionDestroyed事件去清除Redis缓存?Redis有“多长时间未访问则删除缓存”功能啊,和Session的机制一样。 | |
引用 1 楼 zhangjihao 的回复: 为什么要通过sessionDestroyed事件去清除Redis缓存?Redis有“多长时间未访问则删除缓存”功能啊,和Session的机制一样。对的,可以设置redis的超时自动清理session信息 | |
引用 2 楼 freeworkman 的回复: Quote: 引用 1 楼 zhangjihao 的回复:为什么要通过sessionDestroyed事件去清除Redis缓存?Redis有“多长时间未访问则删除缓存”功能啊,和Session的机制一样。对的,可以设置redis的超时自动清理session信息 如果自己控制,设置超时的同时,可能还需要考虑,每次访问时间顺延的问题吧? | |
引用 3 楼 lbq613613 的回复: 如果自己控制,设置超时的同时,可能还需要考虑,每次访问时间顺延的问题吧? 是的 | |
监听 sessionDestroyed 并非只是为了 清除Redis中的数据; 还有其他操作 比如 统计在线用户数 等功能;如果只是纯粹的用户数据 我可以放到 Session中 或者redis超时策略 | |
引用 1 楼 zhangjihao 的回复: 为什么要通过sessionDestroyed事件去清除Redis缓存?Redis有“多长时间未访问则删除缓存”功能啊,和Session的机制一样。监听 sessionDestroyed 并非只是为了 清除Redis中的数据; 还有其他操作 比如 统计在线用户数 等功能;如果只是纯粹的用户数据 我可以放到 Session中 或者redis超时策略 | |
我也遇到同样问题,楼主解决没? | |
还加多一个问题:<property name="maxInactiveIntervalInSeconds" value="60"/>value不管怎样设置都不管用,redis还是同个时间过期(例如:spring:session:sessions:7050f6cb-51a9-454c-ad27-25ffc054fe6a key 是300秒的样子) | |
你是怎么使用Spring Session的?Spring Session和HttpSession是无缝集成的。 | |
只需要注册一个 SessionEventHttpSessionListenerAdapter ,然后在构造参数中引入自己的处理类,实现 HttpSessionListener 接口即可。
| |||
引用 10 楼 qq_35574615 的回复: 只需要注册一个 SessionEventHttpSessionListenerAdapter ,然后在构造参数中引入自己的处理类,实现 HttpSessionListener 接口即可。 嗯嗯,这种配置果然可行;在org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration 中配置 httpSessionListeners 属性,只能监听到 create 不能监听到 destory | |||
我用10 楼的代码实现不了监听啊 | |
<bean class="org.springframework.session.web.http.SessionEventHttpSessionListenerAdapter"> <constructor-arg name="listeners"> <list> <bean class="com.cccis.foundation.authentication.CCCSessionCounter"></bean> </list> </constructor-arg> </bean> <listener> <listener-class>com.cccis.foundation.authentication.CCCSessionCounter</listener-class> </listener> | |
public class CCCSessionCounter implements HttpSessionListener { public void sessionCreated(HttpSessionEvent se) { System.out.println("------"); } public void sessionDestroyed(HttpSessionEvent se) { System.out.println("---====---"); } } | |
引用 13 楼 qq_19972217 的回复: <bean class="org.springframework.session.web.http.SessionEventHttpSessionListenerAdapter">喊你写配置里面,不是web.xml里面 | |
请问怎么通过springSession 对redis 的key 进行管理,比如我登陆以后生成一个key 我在退出在登陆他会有两个key 而我只要一个最新登陆的key ,怎么设置唯一key 或者 通过springSession 对redis 的key进行清除 | |
引用 15 楼 qq_21235239 的回复: Quote: 引用 13 楼 qq_19972217 的回复:请求大神的qq 或者加我的qq 591287843<bean class="org.springframework.session.web.http.SessionEventHttpSessionListenerAdapter">喊你写配置里面,不是web.xml里面 | |
引用 11 楼 cq_tangwei 的回复: Quote: 引用 10 楼 qq_35574615 的回复:只需要注册一个 SessionEventHttpSessionListenerAdapter ,然后在构造参数中引入自己的处理类,实现 HttpSessionListener 接口即可。 同问,只能监听create,不能监听到destroy,怎么解决的? | |||
public class SessionListener implements HttpSessionListener { public void sessionCreated(HttpSessionEvent httpSessionEvent) { SessionContext.AddSession(httpSessionEvent.getSession()); System.out.println("--session 创建了----"+httpSessionEvent.getSession().getId()); } public void sessionDestroyed(HttpSessionEvent httpSessionEvent) { HttpSession session = httpSessionEvent.getSession(); SessionContext.DelSession(session); System.out.println("--session 销毁了----"+httpSessionEvent.getSession().getId()); } } -------------------------------可以销毁------------- | |
mark 还要凑够6个字 | |
楼主,我这边怎么还不能监听 | |
最终是怎么搞?怎么监听销毁session的事件呢,怎么没有一个结论了。 | |
另外使用HttpSessionAttributeListener监听器似乎无效,求大神告知在登录的时候怎么去监听登录信息然后保存起来? | |
搞定了,原因是配置了: <util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/> 这一行, 找了2天原因。被坑了,网上的文章都把这一行加上的。 官网文章: https://docs.spring.io/spring-session/docs/1.1.0.M1/reference/html5/#httpsession-httpsessionlistener 看到这样的一段: If you are using @EnableRedisHttpSession the SessionMessageListener and enabling the necessary Redis Keyspace events is done automatically. However, in a secured Redis enviornment the config command is disabled. This means that Spring Session cannot configure Redis Keyspace events for you. To disable the automatic configuration add ConfigureRedisAction.NO_OP as a bean. 艹。一万只草泥马!!! | |
把 <util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>这一行去掉就好了,如果去掉之后有报错 需要开启redis的key失效事件监听 http://blog.csdn.net/aeroleo/article/details/77011839 | |
https://blog.csdn.net/zcl111/article/details/51700925 在开发过程中,我们可能会在session的创建或销毁时要处理额外的业务,这个时候我们就应该添加相应的监听器,用于监听处理session创建、销毁事件。不过首先要确保配置的SessionRepository是支持session事件触发的。 还是使用redis为列: 在SpringHttpSessionConfiguration中提供了HttpSessionListener监听器的注入方式。 首先,继承HttpSessionListener,创建session事件的触发器。 意思是不是没配置一个支持session事件的SessionRepository | |
果然是这回事啊!!! 这个时候就要打开“键值管理”功能。 config方式时: 在RedisHttpSessionConfig中添加 @Bean public HttpSessionListener httpSessionListener() { return new HttpSessionMonitorListener(); } 同时去掉ConfigureRedisAction的实例化。 //@Bean public ConfigureRedisAction configureRedisAction() { return ConfigureRedisAction.NO_OP; } xml配置方式时: 实例化监听器。 <bean id="httpSessionMonitorListener" class="com.zcl.listener.HttpSessionMonitorListener" /> 同时注入RedisHttpSessionConfiguration实例中 <bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"> <property name="redisNamespace" value="web_01" /> <property name="maxInactiveIntervalInSeconds" value="600" /> <property name="httpSessionListeners"> <list> <ref bean="httpSessionMonitorListener"/> </list> </property> </bean> |






