使用Spring Session Redis 共享Session 后如何监听Session的sessionDestroyed事件

老衲老啦 2016-06-14 12:13:33
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 该如何配置???
...全文
7452 31 打赏 收藏 转发到动态 举报
写回复
用AI写文章
31 条回复
切换为时间正序
请发表友善的回复…
发表回复
CODE_F0T1 2019-09-20
  • 打赏
  • 举报
回复
我能说加了一下代码就可以了吗?真的是诡异 @Bean public HttpSessionListener httpSessionListener() { return new CustomSessionListener(); }
柯小牧 2018-06-07
  • 打赏
  • 举报
回复
自己写了点心得 如有不正之处还望指出。https://www.cnblogs.com/even247/articles/9134895.html
柯小牧 2018-05-31
  • 打赏
  • 举报
回复
@viaco2love 按照你的配置 果然生效了 厉害 。。。。
viaco2love 2018-04-04
  • 打赏
  • 举报
回复
发现注解跟xml是两种不同的配置模式的,而且注解还要配置spring扫描包<context:component-scan ....>
viaco2love 2018-04-02
  • 打赏
  • 举报
回复
果然是这回事啊!!! 这个时候就要打开“键值管理”功能。 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>
viaco2love 2018-04-02
  • 打赏
  • 举报
回复
https://blog.csdn.net/zcl111/article/details/51700925 在开发过程中,我们可能会在session的创建或销毁时要处理额外的业务,这个时候我们就应该添加相应的监听器,用于监听处理session创建、销毁事件。不过首先要确保配置的SessionRepository是支持session事件触发的。 还是使用redis为列: 在SpringHttpSessionConfiguration中提供了HttpSessionListener监听器的注入方式。 首先,继承HttpSessionListener,创建session事件的触发器。 意思是不是没配置一个支持session事件的SessionRepository
666-999 2018-03-02
  • 打赏
  • 举报
回复
把 <util:constant static-field="org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP"/>这一行去掉就好了,如果去掉之后有报错 需要开启redis的key失效事件监听 http://blog.csdn.net/aeroleo/article/details/77011839
666-999 2018-03-02
  • 打赏
  • 举报
回复
搞定了,原因是配置了: <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. 艹。一万只草泥马!!!
大街上哼调调 2018-02-23
  • 打赏
  • 举报
回复
另外使用HttpSessionAttributeListener监听器似乎无效,求大神告知在登录的时候怎么去监听登录信息然后保存起来?
666-999 2018-02-09
  • 打赏
  • 举报
回复
最终是怎么搞?怎么监听销毁session的事件呢,怎么没有一个结论了。
大街上哼调调 2018-02-07
  • 打赏
  • 举报
回复
楼主,我这边怎么还不能监听
fangdengfu123 2017-11-29
  • 打赏
  • 举报
回复
mark 还要凑够6个字
remzhang 2017-11-29
  • 打赏
  • 举报
回复
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()); } } -------------------------------可以销毁-------------
Morphy 2017-11-17
  • 打赏
  • 举报
回复
引用 11 楼 cq_tangwei 的回复:
[quote=引用 10 楼 qq_35574615 的回复:] 只需要注册一个 SessionEventHttpSessionListenerAdapter ,然后在构造参数中引入自己的处理类,实现 HttpSessionListener 接口即可。

<bean class="org.springframework.session.web.http.SessionEventHttpSessionListenerAdapter">  
    <constructor-arg name="listeners">
        <list>
            <bean class="com.stark.jarvis.listener.SessionListener" />
        </list>
    </constructor-arg>
</bean>
嗯嗯,这种配置果然可行;在org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration 中配置 httpSessionListeners 属性,只能监听到 create 不能监听到 destory[/quote] 同问,只能监听create,不能监听到destroy,怎么解决的?
  • 打赏
  • 举报
回复
引用 15 楼 qq_21235239 的回复:
[quote=引用 13 楼 qq_19972217 的回复:] <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>
喊你写配置里面,不是web.xml里面[/quote] 请求大神的qq 或者加我的qq 591287843
  • 打赏
  • 举报
回复
请问怎么通过springSession 对redis 的key 进行管理,比如我登陆以后生成一个key 我在退出在登陆他会有两个key 而我只要一个最新登陆的key ,怎么设置唯一key 或者 通过springSession 对redis 的key进行清除
成都java小生 2017-10-27
  • 打赏
  • 举报
回复
引用 13 楼 qq_19972217 的回复:
<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>
喊你写配置里面,不是web.xml里面
qq_19972217 2017-09-07
  • 打赏
  • 举报
回复
public class CCCSessionCounter implements HttpSessionListener {
public void sessionCreated(HttpSessionEvent se) {
System.out.println("------");
}
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("---====---");
}
}
qq_19972217 2017-09-07
  • 打赏
  • 举报
回复
<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>
qq_19972217 2017-09-07
  • 打赏
  • 举报
回复
我用10 楼的代码实现不了监听啊
加载更多回复(11)

67,512

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧