关于防止一个账号在不同机器上重复登录
来源:互联网 发布:林志炫浮夸知乎 编辑:程序博客网 时间:2024/05/16 09:17
需求:同一个账号,比如在a电脑登录,b就不能登录,主管理员可以踢掉任何人下线。
1.SessionListener.java 文件
package com.neusoft.core;import java.util.HashMap;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;/** * 项目名称:diablo * 类名称:SessionListener * 创建人:极限李。 * 创建时间:2017年4月21日 下午4:50:12 * @version */public class SessionListener implements HttpSessionListener{public static HashMap<String,String> userMap = new HashMap<String,String>(); public static HashMap<String,HttpSession> sessionMap = new HashMap<String,HttpSession>();@Overridepublic void sessionCreated(HttpSessionEvent httpSessionEvent) {// TODO Auto-generated method stub}@Overridepublic void sessionDestroyed(HttpSessionEvent httpSessionEvent) {// TODO Auto-generated method stubHttpSession session = httpSessionEvent.getSession(); String sessionId = session.getId(); sessionMap.remove(sessionId); for(String key : userMap.keySet()){ if(userMap.get(key).equals(sessionId)){ userMap.remove(key); } } }}
2.web.xml
<listener> <listener-class>com.neusoft.core.SessionListener</listener-class> </listener>
3.登录controller
String userName = user.getUserName();String sessionid = SessionListener.userMap.get(userName); System.out.println("sessionid "+sessionid);if(sessionid != null&&!sessionid.equals("")){ //注销在线用户,如果session id 相同,不销毁。 System.out.println("sessionid不为空");System.out.println("request.getSession().getId() "+request.getSession().getId()); /*if(!sessionid.equals(request.getSession().getId())){ SessionListener.sessionMap.get(sessionid).invalidate(); SessionListener.userMap.put(userName,request.getSession().getId()); SessionListener.sessionMap.put(request.getSession().getId(),request.getSession()); }*/ String message="此账号已经登录";model.addAttribute("message", message);return "redirect:/login.do";}else{ System.out.println("sessionid为空"); if(SessionListener.sessionMap.containsKey(request.getSession().getId())){ System.out.println("执行删除sessionMap"); SessionListener.sessionMap.remove(request.getSession().getId()); for(String key : SessionListener.userMap.keySet()){ if(SessionListener.userMap.get(key).equals(request.getSession().getId())){ System.out.println("执行删除userMap"); SessionListener.userMap.remove(key); } } } SessionListener.userMap.put(userName,request.getSession().getId()); SessionListener.sessionMap.put(request.getSession().getId(),request.getSession()); queryList(); return "redirect:/main.do";
4.查询当前所有用户
public List<String> queryList(){ HashMap<String,HttpSession> sessionMap = SessionListener.sessionMap; System.out.println(sessionMap); System.out.println(sessionMap.size()); System.out.println(sessionMap.toString()); System.out.println(sessionMap.keySet()); List<String> list = new ArrayList<String>(); System.out.println("SessionListener.userMap.keySet()"+SessionListener.userMap.keySet()); for(String key : SessionListener.userMap.keySet()){ System.out.println("key "+key); OnlineVo v = new OnlineVo(); v.setLoginname(key); System.out.println("v.getLoginname() "+v.getLoginname()); list.add(v.getLoginname()); } System.out.println("list"+list); return list; }
5.执行踢出用户和用户自己点退出或者关闭游览器
@RequestMapping("/addUser.do")public ModelAndView addUser(){ModelAndView modelAndView=new ModelAndView("user/UserAdd");queryList();List<String> queryList = queryList();modelAndView.addObject("queryList", queryList);return modelAndView;}@RequestMapping(value="/xiaxian", method = RequestMethod.GET) public String getNewForm(HttpServletRequest request){String user = request.getParameter("user");System.out.println("user "+user);String sessionid = SessionListener.userMap.get(user); System.out.println("sessionid "+sessionid); //注销在线用户,如果session id 相同,不销毁。 System.out.println("开始去除sessionid");System.out.println("request.getSession().getId() "+request.getSession().getId());HttpSession httpSession = SessionListener.sessionMap.get(sessionid);System.out.println("httpSession "+httpSession);SessionListener.userMap.remove(user);httpSession.invalidate();// SessionListener.sessionMap.remove(sessionid); /* SessionListener.userMap.put(user,request.getSession().getId()); SessionListener.sessionMap.put(request.getSession().getId(),request.getSession());*/ String zhiling = request.getParameter("zhiling"); System.out.println("zhiling "+zhiling);if(zhiling.equals("xiaxian")){return "redirect:addUser.do";}else{return "redirect:/login.do";}}
6.jsp 自己点退出,或者关闭浏览器,或者关机,
<button onclick="tuichu()">退出</button>function tuichu(){ alert('进入退出'); var yonghu = $('#yonghu').text(); var zhiling='tuichu'; var stt="user/xiaxian.do?user=" + yonghu +"&zhiling="+zhiling; window.location.href=stt; } /* window.onbeforeunload = onbeforeunload_handler; */ window.onunload = onunload_handler; /* function onbeforeunload_handler(){ var warning="确认退出?"; return warning; } */ function onunload_handler(){ /* var warning="谢谢光临"; */ /* alert(warning); */ var yonghu = $('#yonghu').text(); var zhiling='tuichu'; var stt="user/xiaxian.do?user=" + yonghu +"&zhiling="+zhiling; window.location.href=stt; }
7.踢人下线
<table id="table" class="gridtable" border="1"><tr><th width="5%">用户名</th><th width="5%">操作</th></tr><c:forEach items="${queryList}" var="queryList"><tr> <td>${queryList}</td> <td><a style="color:blue;cursor:pointer;" onclick="updateRow(this); " >修改</a></td> </tr> </c:forEach></table>function updateRow(r){ var zhiling='xiaxian'; var i=r.parentNode.parentNode.rowIndex; alert("行号"+i); var bb= document.getElementById('table').rows[i].cells[0].innerText; alert(bb); var stt="xiaxian.do?user=" + bb +"&zhiling="+zhiling; //alert(stt); window.location.href=stt; }
0 0
- 关于防止一个账号在不同机器上重复登录
- java如何写一个防止同账号不同ip登录的程序?
- 如何写一个防止同账号不同ip登录的程序?
- WEB系统防止同一账号,同时在多个不同设备登录。
- 关于PHP如何用实现防止用户在浏览器上使用后退功能重复提交输入
- 防止用户重复登录
- 防止用户重复登录
- 防止用户重复登录
- 防止用户重复登录
- 问题:asp.net如何防止用户用同一账号重复登录
- asp.net 中关于防止用户重复登录的问题
- 怎么在一个脚本中使用在不同机器上的命令
- 判断账号重复登录 网站
- 如何在不同的电脑上使用同一个git账号
- regsvr32在不同机器上的区别!
- 如何保证同一个账号同时只能在一个设备登录,账号自动登录
- 关于vs2015生成软件在不同机器上运行缺少库(dll)
- 在windows机器上远程登录ubuntu
- python 数组文件读取,数组拼接
- 一个关于java调用顺序的题目
- easyUI(八) --combobox的onchange事件(联动效果实现)
- jQuery里的trim()函数在浏览器上面支持的问题
- Servlet跳转到其他组件②
- 关于防止一个账号在不同机器上重复登录
- 深度学习框架Keras安装指南
- git简单推送到远程仓库流程
- 微信小程序
- Leetcode 22. Generate Parentheses
- IOS开发之HomeKit(四)
- CSU 1895 Apache is late again(根号2加1的n次方)
- 原生JS添加鼠标事件
- 数字的大写字母的乱序整理