Java How to share a session between two or more web applications?

I have two web applications. I will log into one web application and navigate to another via links or redirects from the first application. Finally, after following some steps in the second application, I will be redirected to the One application. How to implement this?

Here, two applications are common, and I will have three instances of the One application that will interact with Appendix 2.

Please suggest a better approach. I have to use spring and spring Webflow to implement it.

+7
source share
2 answers

Technically, a session between two web applications (two different WARs) cannot be shared. It is designed and made for safety reasons . I would like to make the following comments and suggestions on your issue,

  • A session is usually tracked using a session identifier , which is usually stored as a cookie in the browser and a Session object on the server side.
  • This cookie is sent by the server side each time a request is sent.
  • The cookie will be sent ONLY to the server on which it appeared.
  • Therefore, the cookie set at www.mysite.com/app1 will only be sent to www.mysite.com/app1 and not to www.mysite.com/app2.
  • For you, in order for user sessions to be valid in two applications, the browser will need two cookies to install from applications 1 and 2.
  • One way: when entering application 1 using a java script, also send a request to enter application2. When both of these requests return successfully, your browser will have a session for both applications (app1 and app2)
  • Now logging out will have its problems, when you log out of application 1, you also need to log out of application2. Technically, this means that you need to clear the cookies set from both of these applications. With a little help from a java script you can do this.
+4
source

When you need to make a call to app2 from application1, pass all the necessary information through the request object (as request parameters), then application2 can read and create a session there (perhaps a servlet / filter can be used for this).

You can share a session between the same application (app1 and app1) on computers using clustering.

+5
source

All Articles