当同一帐户再次登录时自动踢出用户?

  • 本文关键字:用户 登录 java sql-server swing
  • 更新时间 :
  • 英文 :


感谢您的所有回复!这是我的代码!请给出您的意见! 我创建了一个列来保存用户的当前状态

ShareHelper.USER = nhanVien;//use static variable to save USER
if (ShareHelper.USER.getStatus() == 0) {//first login -> change status from 0 to 1
DialogHelper.alert(this, "Đăng nhập thành công!");
nvd.updateStatus(1, nhanVien.getMaNV());
this.dispose();
EduSys edu = new EduSys();
edu.setVisible(true);
} else if (ShareHelper.USER.getStatus() == 1) { //if someone logged before -> allow to login again
DialogHelper.alert(this, "Đăng nhập thành công!");
EduSys edu = new EduSys();
edu.setVisible(true);
nvd.updateStatus(2, nhanVien.getMaNV());//then update status to 2
this.dispose();
}

在大型机中,我使用线程连续更新状态

Thread t1 = new Thread(new Runnable() {//use Thread to check if status change to 2-> someone logs in with same account
@Override
public void run() {
while (true) {
model.NhanVien nhanVien = nvd.findById(ShareHelper.USER.getMaNV());
ShareHelper.USER = nhanVien;
if (ShareHelper.USER.getStatus() == 2) {
DialogHelper.alert(EduSys.this, "Có người đã đăng nhập tài khoản này!");
ShareHelper.logoff();//log out the present session
nvd.updateStatus(1, ShareHelper.USER.getMaNV());//change status to 1 again
EduSys.this.dispose();
dn.setVisible(true);
}
}
}
});
t1.start();

但是我遇到了麻烦!当我第二次登录时,我在上一个会话和当前会话中都被踢了,因为上一个会话将状态更改为 2,然后当前会话也会收到该值。有人可以帮我解决这个问题吗!提前谢谢你!

你可以有一个简单的方法。首先创建应用程序级别的简单缓存(一个简单的有映射将起作用(,这将维护用户的状态。如果用户首次登录,则为具有登录值的用户创建条目(即 USER -> 1(.并在数据库中相应地更新字段。 现在,如果用户尝试再次登录,则签入映射中的条目(如果已登录(,请更改映射和数据库中的值并终止该用户的所有会话

谢谢 TG

最新更新