I have a web application implemented in Spring MVC, JSP.
Default session timeout is defined in web.xml is 30 min.
if use
In order to notify the user about the timeouts on the frontend, you need to pass this information from your backend. To accomplish this, I used the info in https://www.javaworld.com/article/2073234/tracking-session-expiration-in-browser.html as a baseline.
In each Response method in your Controller you need to include cookies containing the session timeout time, as well as the current server time. Since it sounds like you've already set the session-timeout in your web.xml, you can add the cookies to your response with something similar to:
@RequestMapping(value="/example", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public ResponseEntity
Then, on the frontend, you need to retrieve the values of these cookies and compare them against the client current time to determine when to notify the user about the upcoming session timeout. Here's a simple code block to do this, based on the other answers here and the article at the link:
var currTimeCookieName = 'serverTime';
var expireTimeCookieName = 'sessionExpiry';
var offset;
var warningTime = 5 * 60 * 1000;
var timeoutWarningMessage = 'Your session is going to be end by 5 min, Please click OK to continue';
function getCookie(name)
{
var name = name + "=";
var ca = document.cookie.split(';');
for(var i=0; i 0) {
checkForTimeout();
} else {
if(confirm(timeoutWarningMessage)) {
//TODO do a backend call or other action to extend the session here
setTimeout(function() {
checkForTimeout();
}, 1 * 60 * 1000);
}
}
}, timeUntilWarning);
}
checkForTimeout();