Thursday, March 22, 2012

Spring Security Example Tutorial - How to limit number of User Session in Java J2EE

Spring security can limit number of session a user can have. If you are developing web application specially secure web application in Java J2EE then you must have come up with requirement similar to online banking portals have e.g. only one session per user at a time or no concurrent session per user. You can also implement this functionality without using spring security but with Spring security its just piece of cake with coffee :). Spring Security provides lots of Out of Box functionality a secure enterprise or web application needed like authentication, authorization, session management, password encoding, secure access, session timeout etc. In our spring security example we have seen how to do LDAP Authentication in Active directory using spring security and in this spring security example we will see how to limit number of session user can have in Java web application or restricting concurrent user session.

 

Spring Security Example: Limit Number of User Session

spring security example - limit number of session in java J2EEAs I said it’s simple and easy when you use spring security framework or library. In fact is all declarative and no code is require to enable concurrent session disable functionality. You will need to include following xml snippet in your Spring Security Configuration file mostly named as applicaContext-security.xml. Here is sample spring security Example of limiting user session in Java web application:

<session-management invalid-session-url="/logout.html">
    <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
</session-management>

As you see you can specify how many concurrent session per user is allowed, most secure system like online banking portals allow just one authenticate session per user. You can even specify a URL where user will be taken if they submit an invalid session identifier can be used to detect session timeout. Session-management element is used to capture session related stuff. Max-session specify how many concurrent authenticated session is allowed and if error-if-maximum-exceeded set to true it will flag error if user tries to login into another session.

Dependency

This code has dependency on spring-security framework. You need to download spring security jar like spring-security-web-3.1.0.jar and add into application classpath.

This simple example of spring security shows power of spring security, a small piece of xml snippet can add very useful and handy security feature in your Java web application. I recommend using spring security for your new or existing Java web application created using Servlet JSP.

That’s all on how to limit number of user session using spring security in Java web application. Let me know if you face any issue while implementing this security feature in your project.

Other Java tutorials you may like

6 comments :

John Turner said...

How does this work in a clustered environment?

danish said...

Cool Example, just few lines of code. Indeed looks like Spring Security is full of such great feature which just need configuration to make them active. I am loving Security Security :)

Sashika said...

Does this handle browser close or browser crash scenarios? If we set the max-sessions=1 and if we close the browser without logging off, can the user login again immediately?

Sindhuraj said...

@Sashika, it doesn't handle browser close scenario. If user closes its browser without logging off from application, his user session will be active on Server and all subsequent login from same user will be denied as "maximum active session is 1".

By the way this feature is called Spring Security Concurrent Session Control and available from Spring security 3.0 in declarative format as mentioned in this tutorial.

I agreed with Writer that this is the easiest way to implement Concurrent Session Control on any Java web application but this feature requires a central Session Repository and if you are running on Two cluster where Session replication is not available and both Cluster have there own session repository, you will end up with Concurrent multiple User session. I am not sure if you can customize this behavior by implementing your own Session Repository, If you have any idea please jump in.

Sourabh Ghose said...

@Sinduraj, The concurrentsessionfilter indeed does not work in a clustered environment with multiple web servers. In order to make this work you would have to write a custom SessionRegistry as described here:
http://scalejava.blogspot.in/2012/12/clustered-spring-sessionregistry.html

Anonymous said...

how to enable Spring Security in case of browser close or browser crash scenario....??

Post a Comment