gpt4 book ai didi

java - 如何阻止已经登录的用户从其他浏览器登录

转载 作者:搜寻专家 更新时间:2023-11-01 03:24:25 24 4
gpt4 key购买 nike

我的应用程序中有一个登录功能,我可以将用户存储在 session 中的位置如果他已经在同一个浏览器上登录,我也可以阻止用户登录。但是,如果登录用户尝试从不同的浏览器再次登录,我无法阻止他。

这是代码..

我正在用这个

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

现在,如果 loggedInUser 试图从另一个选项卡中的同一浏览器进入应用程序,则此 loggedInUser 具有用户对象(所以它对我有用)

但是如果 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);
}


}
return user;

我正在使用 JAVA,GWT

最佳答案

是的,通过在服务器端存储static映射,将User Id存储为键,将Session存储为.

这是直接从我的包中获取的工作代码。

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 followedJava2s link在我开发的时候。

关于java - 如何阻止已经登录的用户从其他浏览器登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18548636/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com