Java 如何停止已登录的用户从其他浏览器登录
我的应用程序中有登录功能, 我可以在会话中存储用户 如果用户已经在同一个浏览器上登录,我也可以阻止他登录。。 但是,如果登录的用户试图从其他浏览器再次登录,我无法阻止他 这是密码Java 如何停止已登录的用户从其他浏览器登录,java,gwt,Java,Gwt,我的应用程序中有登录功能, 我可以在会话中存储用户 如果用户已经在同一个浏览器上登录,我也可以阻止他登录。。 但是,如果登录的用户试图从其他浏览器再次登录,我无法阻止他 这是密码 public User signIn(String userid, String password) { String result = ""; ApplicationContext ctx = new ClassPathXmlApplicationContext( "
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;
我在用这个
session=getThreadLocalRequest().getSession(true);
User loggedInUser = (User) session.getAttribute("user");
现在,如果loggedInUser试图从另一个选项卡中的同一浏览器进入应用程序,则此loggedInUser拥有user对象(因此它对我有效)
但是如果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是,它将用户Id
存储为键,将会话
存储为值
这是我包里的工作代码
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();
}
}
}
当我开发时。这是一个包含map及其getter和setter的类。