在Spring的Session Expiry之前执行自定义事件 [英] Perform custom event before Session Expiry in Spring
问题描述
在我的情况下,Session可以按照以下方式到期:
- >成功注销显式注销)
- >会话超时(隐式注销)
我有DML (记录插入),无论何时用户登录,我都希望在数据库中执行DML(记录删除),无论用户会话超时(隐式注销)如何。
My问题在于,Spring会在会话结束之前告诉我们什么。
因此,我可以在会话过期前执行我的自定义事件。
预先致谢
是的,您可以使用 SessionDestroyedEvent 。
@Component
public class SessionEndedListener实现ApplicationListener< SessionDestroyedEvent> {
@Override
public void onApplicationEvent(SessionDestroyedEvent event)
{
for(SecurityContext securityContext:event.getSecurityContexts())
{
身份验证身份验证= securityContext.getAuthentication();
YourPrincipalClass user =(YourPrincipalClass)authentication.getPrincipal();
//做某事
$ b $ $ $ $
在web.xml中:
< listener>
< listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
< / listener-class>
< / listener>
常规注销以及会话超时都会触发此事件。
I am beginner in Spring framework.
In my case Session can be expire by following way
--> Success Log-out (Explicit Log-out )
--> Session Timeout (Implicit Log-out )
I have do DML(record insertion) in database whenever some user log in and I want to perform DML(record deletion) in database whenever user session timeout (Implicit Log-out).
My Question is that is there any way in Spring that tell us before the expiry of session.
So I can do perform my custom event before session expiry.
Thanks in advance
解决方案 Yes, you can do that with SessionDestroyedEvent.
@Component
public class SessionEndedListener implements ApplicationListener<SessionDestroyedEvent> {
@Override
public void onApplicationEvent(SessionDestroyedEvent event)
{
for (SecurityContext securityContext : event.getSecurityContexts())
{
Authentication authentication = securityContext.getAuthentication();
YourPrincipalClass user = (YourPrincipalClass) authentication.getPrincipal();
// do something
}
}
}
And in web.xml:
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
This event will be fired for both the regular logout as well as the session timeout.
这篇关于在Spring的Session Expiry之前执行自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!