如何在会话即将到期时收到通知? Spring-开机

How to get notified when session is about to expire? Spring-boot

我试过使用

public void onApplicationEvent(ApplicationEvent event)
{


  if(event instanceof SessionDestroyedEvent){

 @WebListener
 public class SessionListener implements HttpSessionListener {

      @Override                                                                                                                                                                                                   
     public void sessionDestroyed(HttpSessionEvent se) {

第一个,我根本没有得到 SessionDestoryedEvent 事件。

似乎spring可能会在会话过期后通知我们。
有没有可靠的方法来通知 before 会话已过期?

最好我想要没有 spring-session 包的解决方案。

使用以下代码我没有得到 sessionDestroyed 也没有 sessionCreated..

 import javax.servlet.http.HttpSessionEvent;
 import javax.servlet.http.HttpSessionListener;
 import javax.servlet.annotation.WebListener;
 import org.springframework.stereotype.Component;

 @WebListener
 public class MySessionListener implements HttpSessionListener {

   private static int totalActiveSessions;

   public static int getTotalActiveSession(){
     return totalActiveSessions;
   }

   public MySessionListener()
   {
     System.out.println("MySessionListener -------------");

   }
   @Override                                                                                                                                                                         
 public void sessionCreated(HttpSessionEvent arg0) {
   totalActiveSessions++;                                                                                                                                                            
 System.out.println("sessionCreated - add one session into counter");                                                                                                              

}

@Override                                                                                                                                                                         
 public void sessionDestroyed(HttpSessionEvent arg0) {
   totalActiveSessions--;
   System.out.println("sessionDestroyed - deduct one session from counter");
   }
 }

Spring Session JDBC 不支持会话事件的发布,因为底层数据存储在这方面存在明显的限制。关系数据库本身没有 pub-sub 可用于将事件传播到集群中所有节点的机制。

这在 reference manual and the JdbcOperationsSessionRepository javadoc 中都有记录。

关于你问题的第二部分,使用支持事件发布的会话存储(例如Redis和Hazelcast)Spring会话将它发布的所有事件转换为标准ServletAPI的HttpSessionEvent 实例。虽然你可以听 Spring Session's event hierarchy 建议通过标准 Servlet API 机制保持所有与会话相关的交互。

与 expiration/deletion 相关的会话事件根据 HttpSession and HttpSessionListener#sessionDestroyed 在会话失效时发布。我不确定在 before 会话过期时收到通知到底是什么意思,因为它是一个模糊的术语,取决于您对 before[=27] 的期望=].