有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在会话超时侦听器中区分超时破坏的会话和手动破坏的会话

我想实现一个在会话过期时调用的侦听器,我发现我可以创建一个实现接口HttpSessionListener的侦听器,并重写方法sessionDestroyed

public class SessionListener implements HttpSessionListener {

   @Override
   public void sessionCreated(HttpSessionEvent sessionEvent) {
      // TODO Auto-generated method stub
   }

   @Override
   public void sessionDestroyed(HttpSessionEvent sessionEvent) {
         // TODO Auto-generated method stub         
   }
}

但问题是每次会话被销毁时(例如登录和注销)都会调用此方法,因此如何知道会话在会话过期后被销毁,或者是否存在除HttpSessionListener之外的其他解决方案

PS:我在应用程序中使用Spring框架


共 (1) 个答案

  1. # 1 楼答案

    也许你可以试着这样猜:

    可能的超时=(CurrentTime-LastAccessedTime)>;=MaxVeInterval

    // HttpServlet
    
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    
        System.out.println("Inside doGet(HttpServletRequest,HttpServletResponse)");
    
        HttpSession session = request.getSession(true);
        System.out.println("Session Id : " +session.getId() );
        System.out.println( "Is New Session : " + session.isNew() );
    
        int timeout = 10;
        session.setMaxInactiveInterval(timeout);
    
        System.out.println( "Max Inactive Interval : " + session.getMaxInactiveInterval() );
    
        System.out.println("Exiting doGet(HttpServletRequest,HttpServletResponse)");
        System.out.println();
    
    }
    
    
    // SessionEventListener
    
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        System.out.println("In sessionCreated(HttpSessionEvent) ");
        HttpSession httpSession = httpSessionEvent.getSession();
        System.out.println("Session Id :"+httpSession.getId() );
        System.out.println("Exiting sessionCreated(HttpSessionEvent) ");
        System.out.println();
    }
    
    
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
    
        System.out.println("In sessionDestroyed(HttpSessionEvent) ");
    
        HttpSession httpSession = httpSessionEvent.getSession();
        long createdTime = httpSession.getCreationTime();
        long lastAccessedTime = httpSession.getLastAccessedTime();
        int maxInactiveTime = httpSession.getMaxInactiveInterval();
        long currentTime = System.currentTimeMillis();
    
        System.out.println("Session Id :"+httpSession.getId() );
        System.out.println("Created Time : " + createdTime);
        System.out.println("Last Accessed Time : " + lastAccessedTime);
        System.out.println("Current Time : " + currentTime);
        boolean possibleSessionTimeout = (currentTime-lastAccessedTime) >= (maxInactiveTime*1000);
    
        System.out.println("Possbile Timeout : " + possibleSessionTimeout);
        System.out.println("Exiting sessionDestroyed(HttpSessionEvent)");
        System.out.println();
    }
    

    输出如下:

    Inside doGet(HttpServletRequest,HttpServletResponse)
    In sessionCreated(HttpSessionEvent) 
    Session Id :39F84968757E85ED89E7565639322F1F
    Exiting sessionCreated(HttpSessionEvent) 
    
    Session Id : 39F84968757E85ED89E7565639322F1F
    Is New Session : true
    Max Inactive Interval : 10
    Exiting doGet(HttpServletRequest,HttpServletResponse)
    
    In sessionDestroyed(HttpSessionEvent) 
    Session Id :39F84968757E85ED89E7565639322F1F
    Created Time : 1383729761582
    Last Accessed Time : 1383729761587
    Current Time : 1383729815839
    Possbile Timeout : true
    Exiting sessionDestroyed(HttpSessionEvent)
    

    我观察到,不一定会立即检测到超时。似乎有一个线程定期检查会话超时。此外,每个请求都有一个检查