重复登录

有几天没写了,所以...

--Java重复登录的实现:

sessionlinstener

public class SessionListener implements HttpSessionAttributeListener {
	Map<String, HttpSession> map = new HashMap<String, HttpSession>();
	// 属性增加
	public void attributeAdded(HttpSessionBindingEvent arg0) {
		String name = arg0.getName();
		if ("user".equals(name)) {
			UserBean userbean = (UserBean) arg0.getValue();
			if (map.get(userbean.getUid()) != null) {
				System.out.println("--------------------");
				HttpSession session = map.get(userbean.getUid());
				UserBean oldub = (UserBean) session.getAttribute("user");
				session.removeAttribute("user");
			}
			map.put(userbean.getUid(), arg0.getSession());
		}
	}

	// 属性删除
	public void attributeRemoved(HttpSessionBindingEvent arg0) {
		String name = arg0.getName();
		if (name.equals("user")) {
			UserBean ub = (UserBean) arg0.getValue();
			map.remove(ub.getUid());
		}
	}

	// 属性替换
	public void attributeReplaced(HttpSessionBindingEvent arg0) {
		String name = arg0.getName();
		if (name.equals("user")) {
			UserBean oldub = (UserBean) arg0.getValue();
			map.remove(oldub.getUid());
			UserBean ub = (UserBean) arg0.getSession().getAttribute("user");
			if (map.get(ub.getUid()) != null) {
				// map中有记录,表明该帐号在其他机器上登录过,将以前的登录失效
				HttpSession session = map.get(ub.getUid());
				session.removeAttribute("user");
			}
			map.put(ub.getUid(), arg0.getSession());
		}
	}
}

 

你可能感兴趣的:(登录)