Notify user about session timeout in Java EE

后端 未结 3 1436
北恋
北恋 2020-12-28 22:27

My requirement is to notify the user with a popup saying that the user session is about to time out in x seconds in case user does not perform any activity on the web page.<

3条回答
  •  一整个雨季
    2020-12-28 23:06

    Either it may be simple servlet, spring-mvc or spring-security auto logout is not possible without perfect client side logic.
    Considering application will have both type of request

    • AJAX and
    • form submission/page reload

    Auto logout needs very calculated logic. Presenting my autologout functionality implementation with following

    Advantages.


    1. No extra call/request is used to achieve this. considering performance impact if more than 10k active users and extra calls to achieve auto logout.
    2. One line configuration using tag.
    3. Works flawlessly even if user opens multiple tab or multiple window.
    4. It intimates you before 30 seconds of session invalidation, so if you have filled form and not submitted, you can keep session alive(extend session by one click). So user less likely to loose unsaved data.

    Usage


    1. Include auto logout script in required JSP pages as given below.

        ....
        
        
    
    

    2. Create a JSP page, autologout-script.jsp and add below code. Note: No editing/configuring is required

    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    
    
    

    3. Configure session attributes to configuring timeout setting Note: Configure this after session creation. You can implement HttpSessionListener sessionCreated method and set the following configuration as per your requirement.

    session.setMaxInactiveInterval(300);
    
    session.setAttribute("timeOutTimeInSeconds", 300);
    session.setAttribute("showTimerTimeInSeconds", 30);
    
    

    4. Add below html for displaying timer.
    Note: it can be moved to autologout-script template page if you are good at CSS. Hence you can avoid to add this in each and every page.
    Include bootstrap or add your custom css.

    
    

    That is all about a simple auto logout implementation. You can download working example from my github repository
    Autologout using simple servlet example
    Autologout using spring-security java configuration example
    Autologout using spring-security xml configuration example

    Logic Explained


    Case 1: On Page load
    Here logic is simple, on page load set timer of interval equlas to maxInactiveInterval. after timeout redirect to login page.
    Case 2: Keep track AJAX calls
    Now considering AJAX requests, you can use .ajaxStart() or .ajaxComplete() callbacks of jquery so that if any ajax request is fired you can reset the interval.
    Case 3: Tracking multi tab/window activity
    Intertab communication is done to synchronize state of each tab. Used localStorage on change event.

    Limitations/Improvements required
    1. If maximum allowed session is one, if session is taken from another system, AJAX request will fail. It needs to be handled to redirect to login page.
    2. Use ajaxStart() instead of ajaxComplete() to have exact sync of idleTime values between server and browser.

    Requirements
    1. Jquery

    Alternatives to current implementation compared


    1. Setting Refresh header in http response. (Not works for AJAX requests)

    response.setHeader("Refresh", "60; URL=login.jsp");
    
    1. Setting meta refresh tag in HTML (Not works for AJAX requests)
    
    
    1. Configuring Activity checker Keeps session alive by repeated AJAX request. Tracks idle time and makes logout request after timeout.
      No doubt it is a good one with simple logic. But i want to just ink my observations.
      • Performance impact if 2 requests are made per minute to keep session alive and 50k active users. 100k requests per minute.
      • Intertab communication If two tabs are open, one tab is receiving activity but other tab is not receiving activity, that tab fires logout request and invalidate session even though activity is present in other tab. (But can be handled)
      • Force logout approach It is a client is dominated over server to invalidate session.

提交回复
热议问题