gpt4 book ai didi

java - 在 Spring 的 Session Expiry 之前执行自定义事件

转载 作者:IT老高 更新时间:2023-10-28 13:56:33 34 4
gpt4 key购买 nike

我是 Spring 框架的初学者。

在我的情况下, session 可以通过以下方式过期
--> 成功登出(显式登出)

--> session 超时(隐式注销)

每当某些用户登录时,我都会在数据库中执行 DML(记录插入),并且每当用户 session 超时(隐式注销)时,我想在数据库中执行 DML(记录删除)。

我的问题是 Spring 有什么方法可以在 session 到期之前告诉我们。所以我可以在 session 到期之前执行我的自定义事件。

提前致谢

最佳答案

是的,您可以使用 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
}
}

}

在 web.xml 中:

<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>

此事件将在常规注销和 session 超时时触发。

关于java - 在 Spring 的 Session Expiry 之前执行自定义事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27753111/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com