亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Spring啟動會話超時事件監聽器

Spring啟動會話超時事件監聽器

呼啦一陣風 2021-10-28 14:04:18
我想在用戶從會話超時中注銷時執行自定義事件。用戶在我的 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 容器完成,您的應用程序正在從中獲取通知。并且無法在會話到期的確切時間收到通知。


查看完整回答
反對 回復 2021-10-28
  • 2 回答
  • 0 關注
  • 180 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號