我想在用戶從會話超時中注銷時執行自定義事件。用戶在我的 application.properties 指定的時間長度后成功注銷:server.servlet.session.timeout=10server.servlet.session.cookie.max-age=10我發現了一些涉及 SessionDestroyedEvent 的類似解決方案,例如:@Slf4j@Componentpublic class SessionExpiredListener implements ApplicationListener<SessionDestroyedEvent> { @Override public void onApplicationEvent(SessionDestroyedEvent event) { for (SecurityContext securityContext : event.getSecurityContexts()) { Authentication authentication = securityContext.getAuthentication(); UserPrincipal user = (UserPrincipal) authentication.getPrincipal(); // UserPrincipal is my custom Principal class log.debug("Session expired!" + user.getUsername()); // do custom event handling } }}問題是 SessionDestroyedEvent 未與會話超時同時觸發,在我的測試中,它在會話過期后最多 5 分鐘觸發。我也嘗試過在 HttpSessionListener 中使用 sessionDestroyed 但結果相似。是否有一個事件會在會話到期時觸發,或者有什么方法可以實現這一點?
2 回答

森欄
TA貢獻1810條經驗 獲得超5個贊
sessionDestroyed()
當 Web 容器使會話過期時調用該方法。在 Tomcat 中,會話過期每分鐘都會發生一次,我認為其他 servlet 容器也是如此。因此,即使在會話超時之后,在下一次檢測到到期之前也可能會有延遲。
會話管理由 servlet 容器完成,您的應用程序正在從中獲取通知。并且無法在會話到期的確切時間收到通知。
添加回答
舉報
0/150
提交
取消