如何阻止已登录的用户从其他浏览器登录 [英] how to stop already signedIn user to sign in from other browser

查看:472
本文介绍了如何阻止已登录的用户从其他浏览器登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我有一个登录功能,
,我可以将用户存储在会话
中,并且我也能够阻止用户登录,如果他已经登录同样的浏览器..
但是,如果一个signedIn用户试图再次从一个不同的浏览器登录,我不能阻止他。



这里是代码..



我使用这个

  session = getThreadLocalRequest()。getSession (真正); 
User loggedInUser =(User)session.getAttribute(user);

现在,如果loggedInUser尝试从SAME浏览器中获取应用程序,另一个选项卡(它适用于我)



但是,如果loggedInUser试图从不同的浏览器中获取应用程序,则此loggedInUser为null(所以它不适用于我)



这里是代码..

  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);
}


}
返回用户;

我使用JAVA,GWT

解决方案是通过在服务器端存储静态地图,其中存储用户标识为一个键和 Session 作为



这里是直接从我的包里运行代码。

 类SessionObject实现HttpSessionBindingListener {
User loggedInUser;
Logger log = Logger.getLogger(SessionObject.class);
public SessionObject(User loggedInUser){
this.loggedInUser = loggedInUser;
}
公共无效的valueBound(HttpSessionBindingEvent事件){
LoggedInUserSessionUtil.getLogggedUserMap()
。把(与loggedInUser,event.getSession());
return;


public void valueUnbound(HttpSessionBindingEvent event){
try {
LoggedInUserSessionUtil.removeLoggedUser(loggedInUser);
return;
} catch(IllegalStateException e){
e.printStackTrace();



$ b $ / code $ / pre

Java tips我跟着


I have a login funcitonality in my application , where i am able to store the user in a session and i am also able to stop user to signIn , if he is already Signed in on the same browser .. But if a signedIn user tries to logIn again from a DIFFERENT browser i am not able to stop him .

here is the code..

I am using this

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

Now this loggedInUser have the user object if a loggedInUser tries to get in the application from the SAME browser in another tab (SO it works for me)

BUT this loggedInUser is null if a loggedInUser tries to get in the application from the DIFFERENT browser(SO its not working 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 am using JAVA , GWT

解决方案

Yes by storing static map on server side,Which stores User Id as a key and Session as value.

Here is 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();
            }
             }

    }

Java tip I followed and Java2s link while I developed.

这篇关于如何阻止已登录的用户从其他浏览器登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆