Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allow only one session per user

We have a web-application developed using struts2, spring & hibernate.

The application needs a functionality that one user can login from only one browser.

Say if user x, is logged in on pc-1 browser ff, then he cannot be logged in from any other place.

I tried it by implemention session map and store the sessions in global map, but this fails when user logs off and tries to login again.

Even it fails critically if the user does not logs off and session time-outs, but the map is not cleared.

Any better idea to implement this functionality.

We do not want to obstruct the user to login but do not want users to exploit the application by allowing him to share the creditionals and allow multiple users with same login to happen.

like image 469
Amol Ghotankar Avatar asked May 28 '12 10:05

Amol Ghotankar


People also ask

How to implement single user session?

Use a SessionInterceptor which will check for session validity, if session is valid it will check if user is already logged in to the application (for this you will have to maintain session somewhere for eg database for every successful login), if valid login is found, redirect user again to login page with custom ...

Is session unique for every user?

Besides session hijacking, one session is always tied to just one user.

What is single user session?

In desktop software, a single application instance would normally have only one single user session. This means that such an application would only show one window (or set of windows) to a single user and interaction is done with that single user.

How do I stop multiple logs in laravel?

Step:1 Create a new Laravel 5.7 Project in users, migration table add one extra filed. if you already migrate users table then you need to add one extra field in the user table. After that migrate table in the database using this following command. In your Laravel application folder, LoginController.


3 Answers

Since you are already using Spring, I would recommend you to integrate your application with Spring Security.

Spring security lets you define maximum sessions allowed per user concurrently.

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

If set when user having valid session tries to login again it will inform user that maximum concurrent access is set to 1.

Read more at the reference documentation of Spring Security: v3.2.x, v4.2.x or v5.1.x.

If spring security is not an option for you then:

  1. Use a SessionInterceptor which will check for session validity, if session is valid it will check if user is already logged in to the application (for this you will have to maintain session somewhere for eg database for every successful login), if valid login is found, redirect user again to login page with custom message, or logout already valid session and then redirect him to login again. If you logout earlier session it would mean any successive action in that browser session will have to deal with invalid session.

  2. If case you are also using Servlet in your application then Interceptor wont work for you, in this case you should use a Filter and follow the same steps as detailed above for Interceptor.

like image 83
mprabhat Avatar answered Sep 28 '22 21:09

mprabhat


The best solution is to log-off user from other session when he logs in in new session. It is often that user would not logoff when closing browser and restricting him from logging in other window would be the pitfall.

Automaticly closing any previous user sessions is good, because in normal usage, it is no problem, but when sharing login and password, no two persons can work simultanously with your application.

like image 38
Danubian Sailor Avatar answered Sep 28 '22 23:09

Danubian Sailor


At the login give the user a generated ID/cookie (sessionid suffices) stored with the user data. If a user does a request to the server with an old ID/cookie, say that he logged in elsewhere.

The other way round, forbidding the new login attempt, has its drawbacks - as you've experienced.

like image 28
Joop Eggen Avatar answered Sep 28 '22 22:09

Joop Eggen