账号在其他地方登录后,当前登录账号被迫下线
//session map,用以实现挤掉相同账号
//key:登录账号;value:session对象
public static Map
//登录用户被挤掉后存放提示消息
//key:sessionId;value:提示消息
public static Map
public void repeatOperate(){
Object uObj = getSession().getAttribute(Environment.GLOBAL_USER_OBJECT);
if(uObj == null)
return;
User user = (User)uObj;
HttpSession session = Environment.SESSION_MAP.remove(user.getLogin());
if(session == null)
return;
Environment.MSG_MAP.put(session.getId(), "账号在其他地方登录,您已被迫下线!");
logoutSession(session);
}
public void checkUserOnline() {
JSONObject json = new JSONObject();
String sessionId = getSession().getId();
json.put("msg", Environment.MSG_MAP.remove(sessionId));
response.setContentType("text/html;charset=utf-8");
try {
response.getWriter().print(json);
} catch (IOException e) {
e.printStackTrace();
}
}
登录操作完成后:
repeatOperate();
Environment.SESSION_MAP.put(user.getLogin(), getSession());
在公共js中添加:
var check;
function checkUserOnline(){
$.ajax({
type:"POST",
url:"/checkUserOnlineLogin.action",
data:{},
dataType: 'json',
success: function(data){
var msg = data.msg;
if(msg != null && msg != "" && msg != undefined){
clearInterval(check);
alert(msg);
//注销登录
logOut();
}
}
});
}
$(document).ready(function(){
//账号重复登录检查
check = setInterval("checkUserOnline()",5000);
}
package com.regaltec.esa.common.listener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
import com.regaltec.esa.common.cfg.Environment;
import com.regaltec.esa.sys.entity.User;
public class SessionListener implements HttpSessionListener{
@Override
public void sessionCreated(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
}
/**
* session超时后,删除SESSION_MAP和MSG_MAP中的冗余信息
*/
@Override
public void sessionDestroyed(HttpSessionEvent event) {
HttpSession session = event.getSession();
Object o = session.getAttribute(Environment.GLOBAL_USER_OBJECT);
if(o == null)
return;
User user = (User)o;
Environment.SESSION_MAP.remove(user.getLogin());
Environment.MSG_MAP.remove(session.getId());
}
}
web.xml配置
<listener>
<listener-class>com.regaltec.esa.common.listener.SpringContextLoaderListenerlistener-class>
listener>
<listener>
<listener-class>com.regaltec.esa.common.listener.SessionListenerlistener-class>
listener>