How to stop an already signed-in user to enter another browser

I have an application login function where I can store the user in a session and I can also stop the user signIn if he is already signed in in the same browser. But if the signedIn user tries to log in again from a DIFFERENT browser, I cannot stop him.

here is the code.

I use this

session=getThreadLocalRequest().getSession(true); User loggedInUser = (User) session.getAttribute("user"); 

Now this loggedInUser has a user object if loggedInUser tries to enter the application from the SAME browser on another tab (SO works for me)

BUT this loggedInUser is null if loggedInUser is trying to enter the application from the DIFFERENT browser (so it does not work for me)

here is the code.

  public User signIn(String userid, String password) { String result = ""; ApplicationContext ctx = new ClassPathXmlApplicationContext( "applicationContext.xml"); MySQLRdbHelper rdbHelper = (MySQLRdbHelper) ctx.getBean("ManagerTie"); User user = (User) rdbHelper.getAuthentication(userid, password); if(user!=null) { session=getThreadLocalRequest().getSession(true); User loggedInUser = (User) session.getAttribute("user"); if(loggedInUser != null && user.getId() == loggedInUser.getId()){ user.setId(0); }else{ session=getThreadLocalRequest().getSession(true); session.setAttribute("user", user); } } return user; 

I use JAVA, GWT

+4
source share
1 answer

Yes, keeping a static map on the server side that stores User Id as a key and Session as value .

Here is the working code from my bag directly.

 class SessionObject implements HttpSessionBindingListener { User loggedInUser; Logger log = Logger.getLogger(SessionObject.class); public SessionObject(User loggedInUser) { this.loggedInUser=loggedInUser; } public void valueBound(HttpSessionBindingEvent event) { LoggedInUserSessionUtil.getLogggedUserMap() .put(loggedInUser, event.getSession()); return; } public void valueUnbound(HttpSessionBindingEvent event) { try { LoggedInUserSessionUtil.removeLoggedUser(loggedInUser); return; } catch (IllegalStateException e) { e.printStackTrace(); } } } 

The Java tip I followed and the Java2s link while I developed it.

+2
source

Source: https://habr.com/ru/post/1499929/


All Articles