views:

114

answers:

1

I hope my question on the title make sense, if not, let say: machine A, via the web browser I log in as admin, I go to machine B, and log in as admin, the web browser in machine A should force a logout on user admin. I gave this some thought, and I think it will be ugly if I try to manual implement this. I have a feeling that this can be done in Glassfish.

I use Java EE 6 + Glassfish v3.0.1. Authentication and authorization are implemented via jdbcRealm set up in Glassfish

+4  A: 
  • create and map (using <listener>..</listener> in web.xml) a HttpSessionListener
  • on sessionCreated(..) store a reference to the session in the ServletContext, in a Map<String, Session>
  • when the user logs-in, get the Map from the ServletContext and see if any session there has the same user / userId as a session attribute.
  • if there is, session.invalidate() it.
  • if you want to use this in a cluster, you can either use a database to store the information so that it is accessible from everywhere, or use a distributed cache (JBoss Cache, Ehcache)
Bozho
Maybe a Map<String, HttpSession> rather than a set, to allow easy lookups by userid rather than having to iterate.
Tom Anderson
good idea. updating
Bozho
Unfortunately, this won't work in a cluster.
Vineet Reynolds
@Vineet, what _would_ work in a cluster?
Thorbjørn Ravn Andersen
@Thorbjørn Ravn Andersen, if the solution has to involve Java, the map must be shared across all JVMs (so possibly Terracotta or Coherence might help). But such a solution can be considered "heavy" if an object caching solution is used for only one feature. It might in fact, turn out to be easier to store the list in a database, and lazily-expire sessions.
Vineet Reynolds
Can you give me some sample codes? The above explanation even though are extremely good, but I am a bit new to this business, so lots of them dont even make much sense to me. Thank you and sorry for the trouble
Harry Pham
@Harry: basically, you need to replace `Map<String, Session>` (as per Bozho's example) by a (shared) database table and fire SQL queries instead of `Map#get()`, `Map#remove()` and so on.
BalusC
Or a distributed cache. I added these options to the answer
Bozho