Java防止用户同一时间重复登录(包括异地登录)

有时候我们在做登录模块过程中难免会遇到这种问题,即使我们给用户进行了多重判断,比如:用户名,管理员,验证码,一系列的判断… 你是否真正考虑到用户的体验,比如不能让用户在同一时间,同一个浏览器重复登录问题,同时也包括不同浏览器登录(异地登录)问题。
这些都是我们应该去考虑的。如果是用Spring 框架的话,Spring security 是可以实现防止用户重复登录的问题的,但我这里并没有用到框架,因为我这里不需要做太高的限制。

我们可以利用 Servlet三大域对象 request、session、application(ServletContext)中的ServletContext,它是一个全局的储存信息的空间,服务器启动后就被创建,服务器停止后才销毁。
request,一个用户可有多个;session,一个用户一个;而servletContext,所有用户共用一个。所以,为了节省空间,提高效率,ServletContext中,要放必须的、重要的、所有用户需要共享的线程又是安全的一些信息。
思路:我们将用户的登录信息保存在context里, 然后利用监听器HttpSessionListener监听每一个登录用户的登录情况,实时监控session。

Servlet:

/*登录方法*/
public void login(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException{String username = request.getParameter("username");//登录名String password = request.getParameter("password");//密码HttpSession session=request.getSession();response.setCharacterEncoding("UTF-8");PrintWriter out=response.getWriter();SystemUsertable usertable = service.login(username);//SystemUsertable 自己封装存放用户信息的类if (usertable != null) {if (usertable.getPassword().equals(password)) {session.setAttribute("userid", usertable.getUserid());session.setAttribute("username", usertable.getUsername());} else {out.write("<html><head><meta charset='utf-8'></head><script>alert('密码错误');window.location.href='../jsp/login.jsp';</script>");}}else {out.write("<html><head><meta charset='utf-8'></head><script>alert('用户不存在');window.location.href='../jsp/login.jsp';</script>");}ServletContext context = session.getServletContext();@SuppressWarnings("unchecked")Map<String, Object> loginMap = (Map<String, Object>) context.getAttribute("loginMap");if (loginMap == null) {loginMap = new HashMap<String, Object>();}for (String key : loginMap.keySet()) {int result=usertable.getUserid();String day = "";day = String.valueOf(result);if (day.equals(key)) {if (session.getId().equals(loginMap.get(key))) {out.write("<html><head><meta charset='utf-8'></head><script>alert('在同一地点重复登录');window.location.href='../jsp/login.jsp';</script>");} else {out.write("<html><head><meta charset='utf-8'></head><script>alert('异地已登录,请先退出登录');window.location.href='../jsp/login.jsp';</script>");}}else{}}loginMap.put(String.valueOf(usertable.getUserid()),session.getId());context.setAttribute("loginMap", loginMap);// 将用户保存在session当中session.setAttribute("usertable", usertable);// session 销毁时间session.setMaxInactiveInterval(10*60);out.write("<html><head><meta charset='utf-8'></head><script>alert('登录成功');window.location.href='../jsp/main.jsp';</script>");}

这时候我们需要写一个监听器,监听每一个登录用户的登录情况。首先去web.xml 配置监听器:

/*监听器存放路径*/
<listener>  <listener-class>  com.day.listener.SessionListener</listener-class>  
</listener>

Listener:

/*监听器*/
public class SessionListener implements HttpSessionListener  {@Overridepublic void sessionCreated(HttpSessionEvent httpSessionEvent) {//创建// TODO Auto-generated method stub}@Overridepublic void sessionDestroyed(HttpSessionEvent httpSessionEvent) {//销毁// TODO Auto-generated method stubHttpSession session = httpSessionEvent.getSession();SystemUsertable user = (SystemUsertable) session.getAttribute("usertable");if (user != null) {ServletContext application = session.getServletContext();@SuppressWarnings("unchecked")Map<String, Object> loginMap = (Map<String, Object>) application.getAttribute("loginMap");loginMap.remove(user.getUserid());application.setAttribute("loginMap", loginMap);session.removeAttribute("usertable");}}
}

最后需要对它的退出登录进行处理,退出时销毁session, 清除用户登录信息。

 /*退出登录*/
public void loginOut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{HttpSession session = request.getSession();String id = session.getId();if (session != null) {try {session.removeAttribute("usertable");String user= id; if (session.getAttribute(user) != null) {session.removeAttribute(user);}ServletContext context = session.getServletContext();@SuppressWarnings("unchecked")Map<String, Object> loginMap = (Map<String, Object>) context.getAttribute("loginMap");if (loginMap != null) {loginMap = new HashMap<String, Object>();}context.setAttribute("loginMap", loginMap);} catch (Exception e) {e.printStackTrace();}}request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
}

这样子就可以把登录的用户信息清除掉,用户可去其他浏览器登录亦或是继续登录。其实,这些大多可以根据自己的需求而定,代码可以是固定的,但思想是活的。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/443367.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

jquery点击弹出播放视频并显示遮罩层

最近在做视频播放的时候&#xff0c;发现用jquery的播放视频可以很好的实现我所需要的效果。在之前请把插件引用进来&#xff0c;废话不多说&#xff0c;代码如下&#xff1a; <!DOCTYPE html> <html><head><meta charset"utf-8"><meta …

JS实现图片循环滚动

之前在前端的时候有遇到这样一个问题&#xff0c;实现 JS 图片的循环滚动&#xff0c;然后鼠标移入的时候停止滚动&#xff0c;鼠标移开继续滚动&#xff0c;这里无非就是设置了一个定时器&#xff0c;鼠标移上时清除定时器达到滚动停止的目的&#xff0c;鼠标移开时重设定时器…

有趣的滚动条

今天在搭前端页面的时候&#xff0c;UI给过来的一个自定义的滚动条&#xff0c;要求必须与设计的一样&#xff0c;觉得有点有趣&#xff0c;现在记录一下。 效果图&#xff1a; 具体代码是这样的&#xff1a; /*字体太长&#xff0c;这里我就减少一些*/ <div class"l…

查询分页的几种sql语句

sql server 中的分页查询sql语句不跟mysql 一样&#xff0c;mysql 用 limit 可以实现查询sql分页。如&#xff1a; select * from news where id>(select id from news limit 250000,1) limit 10; //相对执行效率高 select * from news limit 250000,10;mysql 中 limit总是…

c#解决在数据表格中无法显示秒数问题

在查询日期的时候&#xff08;年月日时分秒&#xff09;&#xff0c;发现丢失了秒&#xff0c;在这里我用的是自己 封装的Vo类里面的时间转换&#xff0c;没有把秒数查询出来&#xff0c;在Vo动不了手&#xff0c;我还不能对Controller 动手了&#xff1f; 查询&#xff1a; 更…

解决 idea 复制jsp 文件过来页面报404

今天在做功能的时候把另外一个jsp文件复制过来&#xff0c;发现页面一直报404&#xff0c;咋搞的&#xff0c;检查路径没有问题&#xff0c;报404这个就奇葩了&#xff0c;后面经过一番“洗脑”&#xff0c;才发现jsp文件复制过来少了一个特别重要的步骤&#xff0c;那就是重新…

解决ckeditor富文本在layui 弹框中大小字体等下拉不显示

css <div class"layui-input-inline" style"width:1100px;"><div class"noticeContent"><textarea id"Introduce1" name"处分结果" class"form-control" style"z-index:19991015">&…

C# DateTime.Compare判断两个DateTime 日期是否相等

DateTime t1 DateTime.Now.Date; //2020/8/4 0:00:00 DateTime t2 Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));//得到2020/8/4 16:24:54 DateTime t3 Convert.ToDateTime(t1.AddHours(16).AddMinutes(24).AddSeconds(t2.Second).AddM…

解决git上传提交的时出现:Please tell me who you are.问题

今天安装好git后&#xff0c;创建新项目&#xff0c;当git上传提交时出现了一个问题&#xff0c;如下&#xff1a; Commit failed - exit code 128 received, with output: *** Please tell me who you are.Rungit config --global user.email "youexample.com"git …

vue解决v-for报错 [vue/valid-v-for]Custom elements in iteration require ‘v-bind:key‘ directives

v-for报错 [vue/valid-v-for]Custom elements in iteration require ‘v-bind:key’ directives.eslint-plugin-vue 原因&#xff1a;这是因为我安装的enlint插件&#xff0c;它会对代码的规范进行检查。我这里用到v-for&#xff0c;但它是需要key值的&#xff0c;当不根据它的…

vscode中如何运行git

在vscode 中运行git 指令&#xff0c;发现报错。 这是因为还没有进行安装git &#xff0c;所以这个指令对于vscode 来说是没有办法识别的&#xff0c;这时&#xff0c;我们就要进行下载安装git。 第一步&#xff1a;下载git 去到git 官网&#xff1a; 点击下载&#xff0…

通过nginx在window下部署项目

1…安装nginx 首先&#xff0c;进入nginx 的官网&#xff0c;下载nginx&#xff0c;安装nginx 通过&#xff0c;https://nginx.org/ 链接进入 Mainline version&#xff1a;开发版&#xff0c;mainline 目前主力在做的版本 Stable version&#xff1a;最新稳定版&#xff0c;生…

C# 字符串逗号分隔存到List 数组(互相转换)

List 数组用逗号分隔成字符串 var ptCancelFileUpload context.PtCancelFileUpload.Where(x > x.Recordstatus ! RecordStatusType.InActive.ToStr()).Select(x > x.Taskcode).ToList();if (ptCancelFileUpload.Count > 0){string resultStr "";foreach …

代码块概述

代码块&#xff1a;在Java中&#xff0c;使用{}括起来的代码被称为代码块。 根据其位置和声明的不同&#xff0c;可以分为&#xff1a; 局部代码块:局部位置,用于限定变量的生命周期。 构造代码块:在类中的成员位置,用{}括起来的代码。每次调用构造方法执行前&#xff0c;都…

Android常见命令

1&#xff09;adb指令 adb kill-server 杀死adb服务 adb start-server 开启adb服务 cd desktop&#xff08;假如安装包在桌面&#xff09; adb install 应用名.apk 安装应用 adb uninstall 应用包名 …

final,finally和finalize的区别

&#xfeff;&#xfeff;final&#xff1a;最终的意思&#xff0c;可以修饰类&#xff0c;成员变量&#xff0c;成员方法 修饰类&#xff0c;类不能被继承 修饰变量&#xff0c;变量是常量 修饰方法&#xff0c;方法不能被重写 finally&…

Collection集合总结

&#xfeff;&#xfeff;Collection|--List 有序,可重复|--ArrayList底层数据结构是数组&#xff0c;查询快&#xff0c;增删慢。线程不安全&#xff0c;效率高|--Vector底层数据结构是数组&#xff0c;查询快&#xff0c;增删慢。线程安全&#xff0c;效率低|--LinkedList底层…

排序算法之冒泡排序,选择排序

public class Sort {public static void main(String[] args) {int[] arr { 24, 69, 80, 57, 13 };bubbleSort(arr);selectSort(arr);for (int s : arr) {System.out.print(s "/");}}//冒泡排序public static void bubbleSort(int[] arr) {for (int i 0; i < a…

java设计模式之装饰模式

装饰模式&#xff1a;又名包装(Wrapper)模式&#xff0c;装饰模式以对客户端透明的方式扩展对象的功能&#xff0c;是继承关系的一个替代方案。 装饰模式可以在不创造更多的子类的模式下&#xff0c;将对象的功能加以扩展。 经典代码&#xff1a; //装饰模式interface Phone…

Hibernate与 MyBatis的比较

第一章 Hibernate与MyBatis Hibernate 是当前最流行的O/R mapping框架&#xff0c;它出身于sf.net&#xff0c;现在已经成为Jboss的一部分。 Mybatis 是另外一种优秀的O/R mapping框架。目前属于apache的一个子项目。 MyBatis 参考资料官网&#xff1a;http://www.mybatis.or…