title: Destroy spring session after tab or browser close
body: <p>I have a requrement to destroy session after 30 seconds of tab/browser close and logout user. If new tab or new window access my website URL, after 30 second it should redirect to login page other wise dashboard should be shown.</p>
  
  <p>There are many threads to similar problem but not getting perfect solution for spring mvc based application.</p>
  
  <p>Suggestions from other threads:</p>
  
  <p>"Session objects live in the server and are controlled by the server. Only the server can create or destroy sessions. So if the client is closed the session lives until it expires. The client can only suggest to the server that it can destroy some session. This request must be explicit somehow. Hence, when you close a browser window, there's no implicit request to the server informing that it must destroy a given session."
  "If you need the session to be destroyed when a browser window/tab is closed you might attach a JavaScript handler to the onunload event that makes some sort of AJAX call to a resource that call kill the session."</p>
  
  <p>"Note that the onunload event does not always fire so it's not totally trustworthy. (Does not work with new chrome versions) One trusty way might be to use a "heartbeat" system."</p>
  
  <p><a href="https://stackoverflow.com/questions/706224/javawhy-http-session-is-not-destroyed-when-tab-or-browser-is-closed#">Java:Why http session is not destroyed when tab or browser is closed?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/11008177/stop-people-having-my-website-loaded-on-multiple-tabs">Stop people having my website loaded on multiple tabs</a></p>
  
  <p><a href="https://stackoverflow.com/questions/28841505/spring-4-stomp-websockets-heartbeat">Spring 4 STOMP Websockets Heartbeat</a></p>
  
  <p>Can somebody suggest correct way for spring 4 based application?</p>