It can be Spring-Security, Spring-MVC or a servlet, automatic failure is impossible without perfect logic on the client side.
The application in question will have both types of requests
- Ajax and
- form submission / page reload
Automatically logging out requires very sophisticated logic. Introducing my autologout function implementation with the following
Benefits.
1. No additional calls / requests are used to achieve this. taking into account the impact on productivity, if more than 10,000 active users and additional calls to achieve automatic exit.
2. Single line configuration using a tag.
3. It works flawlessly, even if the user opens several tabs or multiple windows.
4. He informs you that up to 30 seconds of the session is invalid, so if you filled out the form and did not submit it, you can keep the session alive (extend the session with one click of the mouse). Thus, the user is less likely to lose unsaved data.
Usage 1. Turn on the automatic logoff script on the required JSP pages as follows.
.... </body> <jsp:include page="../template/autologout-script.jsp"></jsp:include> </html>
2. Create a JSP page, autologout-script.jsp and add the code below. Note. Editing / customization is not required.
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <script> $(document).ready(function() { var timeOutTimeInSeconds = ${ timeOutTimeInSeconds }; var showTimerTimeInSeconds= ${ showTimerTimeInSeconds }; var sessionCheckIntervalId = setInterval(redirectToLoginPage, timeOutTimeInSeconds * 1000); var timerDisplayIntervalId = setInterval(showTimer, (timeOutTimeInSeconds - showTimerTimeInSeconds) * 1000); var badgeTimerId; window.localStorage.setItem("AjaxRequestFired", new Date()); function redirectToLoginPage(){ </script>
3. Configure session attributes to configure timeouts. Note. Configure this after creating the session. You can implement the sessionCreated HttpSessionListener method and set the following configuration according to your requirements.
session.setMaxInactiveInterval(300); session.setAttribute("timeOutTimeInSeconds", 300); session.setAttribute("showTimerTimeInSeconds", 30);
4. Add the HTML below to display the timer.
Note: you can move it to the autolog script page if you are good at CSS. Therefore, you can avoid adding this to every page.
Turn on the loader or add your own CSS.
<span class="badge badge-primary" title="click to keep session alive" id="sessionTimeRemaining" onclick="ajaxSessionRefresh()" style="display:none;"> <i class="badge badge-danger" id="sessionTimeRemainingBadge" style="float:left">30</i> <small>Refresh</small> <i class="glyphicon glyphicon-refresh"></i> </span>

It's all about the simple implementation of automatic logout. You can download a working example from my github repository
Autologout using a simple servlet example
Autologout using the Spring-Security Java configuration example
Autologout using Spring-Security XML configuration example
Explained Logic
Case 1: when loading a pageHere the logic is simple, when loading the page, set the interval equation timer to maxInactiveInterval. after a timeout redirect to the login page.
Case 2: Track AJAX CallsNow when looking at AJAX requests, you can use .ajaxStart () or .ajaxComplete () jquery callbacks so that when you run any ajax request you can reset the interval.
Case 3: tracking the activity of multiple tabs / windowsIntertab communication is done to synchronize the state of each tab. Used localStorage when changing the event.
Constraints / improvements required
1. If the maximum allowed session is equal to one, if the session is taken from another system, the AJAX request will not be executed. This needs to be processed in order to redirect to the login page.
2. Use ajaxStart () instead of ajaxComplete () to precisely synchronize idleTime values ββbetween the server and browser.
Requirements
1. jquery
Comparison of alternatives to the current implementation
1.
Setting the header update in the http response. (Does not work for AJAX requests)
response.setHeader("Refresh", "60; URL=login.jsp");
- Customizing meta meta tag in HTML (not working for AJAX requests)
<meta http-equiv="refresh" content="60; url=login.jsp">
- Configure Activity Checks Supports a session by re-invoking an AJAX. Monitors downtime and sends an exit request after a timeout.
No doubt it's good with simple logic. But I just want to draw my observations.- Performance impact if 2 requests are executed per minute to maintain session activity and 50,000 active users. 100,000 requests per minute.
- Relationship between tabs If two tabs are open, one tab receives activity, but the other tab does not receive activity, this tab launches a request to exit the system and invalidates the session, even if activity is present on another tab. (But can be processed)
- Force Logout Approach This client dominates the server to terminate the session.