Spring:在会话超时之前调用方法

时间:2013-06-03 03:34:55

标签: spring session timeout

我目前有一个Spring 3项目,我想要做的是在会话到期时检索我的会话。我一直在做一些研究,显然HttpSessionBindingListener可以处理这个,虽然在Spring项目中,我似乎无法弄清楚如何正确实现它。在我的会话中,我保存了一个包含UserDetailsImpl对象的User对象。我应该在所陈述的对象上实施HttpSessionBindingListener吗?

要清楚,我想要做的是在会话对象到期之前从会话对象中检索用户的id。

编辑:显然HttpSessionBindingListener在Websphere中无法正常工作,但在Tomcat中没问题。还有其他选择吗?

1 个答案:

答案 0 :(得分:1)

您还可以在web.xml注册听众:

<listener>
    <listener-class>com.example.MyHttpSessionListener</listener-class>
</listener>

并使用方法sessionDestroyed()

这个详细描述在这个答案中:https://stackoverflow.com/a/3720512/516167

在此侦听器中注入Spring应用程序上下文,如此问题中所述:

How to inject dependencies into HttpSessionListener, using Spring?

此处描述了其他可能的解决方案:

Logout/Session timeout catching with spring security