How to limit number of concurrent session in a Java web application using Spring Security?

If you don't know, Spring security can limit the number of sessions a user can have in a Java web application. If you are developing a web application especially asecure web application in Java JEE then you must have come up with the requirement similar to many online banking portals have likeonly one session per user at a time or no concurrent session per user. If the user tries to open a new session then either an alert is shown or his previous session is closed. Even though you can also implement this functionality without using spring security but with Spring security, its just piece of cake with coffee :).  You just need to add a couple of lines of XML in your spring security configuration file and you are done. In order to implement this functionality, you can use the <concurrency-control>tag.

You can configure a maximum number of the session your application support and then Spring security will automatically detect if user breach that limits and direct them to invalid session url you have specified with this tag e.g. to a logout page.

Similar to this, Spring Security provides lots of Out of Box functionality a secure enterprise or web application needed for 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 an Active directory using spring security and in this spring security example we will see how to limit the 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 J2EE As I said it’s simple and easy when you use spring security framework or library. In fact, is all declarative and no code is required to enable the concurrent session to disable the functionality.

You will need to include following xml snippet in your Spring Security Configuration file mostly named as applicaContext-security.xml. You can name the file whatever you want but just make sure you use the same name in all relevant places. If you are not sure how to enable Spring Security in Java web application, check that article first.

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, a most secure system like online banking portals allows just one authenticated session per user.

The Max-session specifies how many concurrent authenticated session is allowed and if error-if-maximum-exceeded set to true it will flag an error if a user tries to login into another session.

For example, if you try to log in twice from your browser to this spring security application then you will receive an error saying "Maximum Sessions of 1 for this principal exceeded" as shown below:

Spring security concurrent session control example

You can even specify a URL where the user will be taken if they submit an invalid session identifier can be used to detect session timeout. The session-management element is used to capture the session related stuff.

This is just an example of what Spring security can add to your Java web application. It provides many such advanced and necessary features which can be enabled using some XML tag or annotations.

If you are interested to learn more about advanced Spring security features, I suggest you go through Learn Spring Securitycourse by Eugen Paraschiv, which the most up-to-date online course on Spring Security and covers new security features from Spring Security 5 release.

Spring Security Concurrent User Session example

Dependency

This code has a dependency on the 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 the power of spring security, a small piece of xml snippet can add very useful and handy security feature in your Java web application.

I strongly recommend using spring security for your new or existing Java web application created using Servlet JSP.

That’s all on how to limit the 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 Spring Security Tutorials and Resources you may like to explore

  • Spring Framework 5: Beginner to Guru
  • Spring Security Fundamentals by Bryan Hassen
  • Top 5 Course to Learn Spring Boot in 2019
  • Top 10 Spring question and answer asked in Interview
  • What is SecurityContext and SecurityContextHolder in Spring Security?
  • Top 5 Courses to learn Spring Framework in depth
  • How to implement Role-based Access Control in Spring Security?
  • 10 Spring MVC Annotations Java Developer should learn
  • How to enable Http Basic Authentication in Spring Security?
  • Top 5 course to learn Microservice with Spring Boot and Cloud
  • How HttpBasicAuthentication works in Spring Security?
  • Learn Spring Security by Hands on Examples

Thanks for reading this article so far. If you find this Spring Security tutorial use then please share with your friends and colleagues. If you have any questions or feedback then please drop a note.

P.S - If you like to learn from a book, then Pro Spring Security by Carlo Scarioni is a good starting point. The content is not advanced enough for senior developers but for the junior and intermediate programmer, it's a great book.

P.S.S - Also, If you are an experienced Java/JEE Program and want to learn Spring Security end-to-end, I recommend Learn Spring Security  course by Eugen Paraschiv, The definitive guide to secure your Java application. It's useful for both junior and experienced Java Web developers.