Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass session data between two different applications

I have two applications running on Tomcat, JSF 2 Mojarra 2. Both applications are mapped to the same domain but each to a different pattern within this domain. One application is used as a front page while the other is used to access protected resources (don't ask why not all in one app, it's been design deliberately to separate the apps as two different entities, each responsible for it's own thing). Now the question is: is it possible, and if it is, how to pass the session state between those two separate apps. To illustrate here are some common situations:

  1. A user does something on the main application running the website and then logs in and whatever he/she was up to doing is being taken to the new session after login to the new app.

  2. (This one is slightly more complex I think) The user registers in the first app and is automatically logged in upon successful registration into the other app. The app where yo have to log in is using j_security_check form login (this would be the hard part)

like image 632
foofighter Avatar asked Feb 11 '26 09:02

foofighter


1 Answers

Several ways:

  1. Store the data in DB which and identify it by a long, unique, hard-to-guess autogenerated key which you in turn also store in a domain-wide cookie. This way the both applications can get the data from the DB based on the key found in the cookie.

  2. Expose the ServletContext of the both applications to each other. In Tomcat, it's a matter of adding crossContext="true" to the <Context> element of the webapplication's context.xml. This way you can get the each other's ServletContext by ServletContext#getContext(). Finally put some Map<String, SomeData> in there which is keyed by some ID which is shared between the both applications, for example the logged-in user ID (you should only ensure that the same user can't have more than one session).

like image 200
BalusC Avatar answered Feb 15 '26 07:02

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!