Search code examples
javaspringjakarta-eeweb-applicationssession-management

Browser specific session management


Let me explain my scenario,

I have created a spring web application and deployed on tomcat. Then I opened a chrome browser and launched the application. It run successfully. Then again I have opened a new Browser lets say IE/Firefox. Then again I relaunched my application on it. Then the session available at Chrome browser should be invalidated or redirected to Login page. In simple way, I should be able access my web page in one browser at a time.

Is there any way to achieve this using spring ?? or any other way ??

Thanks Naveen


Solution

  • You did not say how you do your authentication. But as you are allready using Spring, I would advice you to use also Spring security that has configurable session management out of the box.

    Using html config, you can ask that a new session invalidate a previous from same user with (extract from Spring Security Reference Manual 3.2.x / Security Namespace Configuration / Advanced Web Features / Session Management) :

    <http>
      ...
      <session-management>
         <concurrency-control max-sessions="1" />
      </session-management>
    </http>
    

    or that a new session will fail with :

    <http>
      ...
      <session-management>
         <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
      </session-management>
    </http>