I'm using grails 1.3.5 and I need to automatically log out users from my app when they close their browser or all tabs in which my app is opened.
-
2There's no reliable way to do this. – Gregg Aug 23 '12 at 15:33
-
1See also: http://stackoverflow.com/questions/3986430/how-we-call-logout-servlet-on-browser-close-event – ataylor Aug 23 '12 at 15:51
1 Answers
While there is no particularly reliable way to do this (in any web framework, not just Grails), there are some rather hacky ways you can get close to this, though there are some massive tradeoffs.
In general, since you have a default session timeout, the user will be logged out (in general) when their session expires due to not receiving a request associated with their session. This behavior can be changed depending on your security environment, but we'll assume you are using (sensible) defaults.
This session expiration logout can be abused to mimic logging them out when they have no windows/tabs with your application open in them. To do this, you could have a small piece of JavaScript that continually "pings" your server at whatever interval you specify to keep the session "alive" and keep them logged in. How tight you set these pings is a tradeoff between the load on your server and the window of time where they could close their browser and re-open it and still stay logged in.
Like I said, this is very hacky, but it's functional.

- 30,869
- 25
- 117
- 173