java web 用户单点登录的方案的基本实现

2024-09-02 11:32

本文主要是介绍java web 用户单点登录的方案的基本实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

     首先,考虑不能重复登录的问题。在项目中,我使用session来存储用户的信息,用户登录时,创建一个session,将用户名,用户逻辑Id,登录时间等属性存放到该session中。考虑使用Application来实现禁止重复登录。定义一个Map<Long,String>类型的变量loginUserMap。其每条记录存储登录用户的逻辑Id和对应session的sessionId。这样,每次用户登录的时候遍历loginUserMap,如果没有对应的userlogicId或sessionId则允许登录,否则提示已在别处登录。

[java]  view plain copy
  1. // 判断是否重复登录  
  2. isloginexists = false;  
  3. ifsessioninvalidate = false;  
  4. loginUserMap = (Map<Long, String>) acx.getApplication().get(WebConstant.LOGIN_USER_MAP);  
  5. if (loginUserMap == null) {  
  6.     loginUserMap = new HashMap<Long, String>();  
  7. }  
  8. HttpServletRequest request = ServletActionContext.getRequest();  
  9. String sessionId = request.getSession(false).getId();  
  10. System.out.println("sessionId" + sessionId);  
  11. for (Long userlogicId2 : loginUserMap.keySet()) {  
  12.     if (!userlogicId2.equals(userlogicId) && !loginUserMap.containsValue(sessionId)) { // 不同浏览器不允许相同用户重复登录  
  13.         continue;  
  14.     }  
  15.       
  16.     if(userlogicId2.equals(userlogicId)&&!loginUserMap.containsValue(sessionId)){  
  17.         setIfsessioninvalidate(true);  
  18.     }  
  19.     isloginexists = true;  
  20.     break;  
  21. }  
  22. if (isloginexists) {  
  23.     setTip("loginexists");  
  24.     if(ifsessioninvalidate==true){  
  25.         request.getSession(false).invalidate();  
  26. }  
  27.   
  28. else {  
  29.   
  30.     loginUserMap.put(userlogicId, sessionId);  
  31.     acx.getApplication().put(WebConstant.LOGIN_USER_MAP,loginUserMap);  
  32.     acx.getSession().put(WebConstant.USER_ID, getUsername());  
  33.     acx.getSession().put(WebConstant.USER_LOGICID,userManageService.findbyUsername(getUsername()).getLogicId());  
  34.     acx.getSession().put(WebConstant.LOGIN_TIME, new Date());  
  35.        }  


在用户退出的操作中,将loginUserMap中对应的用户logicId和sessionId清除,同时清除session中的用户信息。

[java]  view plain copy
  1.   Map<Long, String> loginUserMap = (Map<Long, String>)acx.getApplication().get(WebConstant.LOGIN_USER_MAP);  
  2.   String username=userManageService.findByLogicId(userlogicId).getUserName();  
  3.   if(loginUserMap.containsKey(userlogicId)){  
  4.    loginUserMap.remove(userlogicId);      
  5.   }  
  6.   session.getServletContext().setAttribute("loginUserMap", loginUserMap);  
  7.     
  8.   Long id=(Long) session.getAttribute(WebConstant.USER_LOGICID);  
  9.   if(id!=null)  
  10. this.userManageService.userLogout(id);  
  11.     
  12.   session.removeAttribute(WebConstant.USER_ID);  
  13.   session.removeAttribute(WebConstant.USER_LOGICID);  
  14.   session.removeAttribute(WebConstant.LOGIN_TIME);  
  15.   
  16.   //使Session失效  
  17.   session.invalidate();  
  18.   
  19.   response.setHeader("Cache-Control","no-cache");  
  20.   response.setHeader("Cache-Control","no-store");  
  21.   response.setDateHeader("Expires"0);  
  22.     
    在session失效的监听器处理中,也做相同的操作,保证登录session超时时从loginUserMap中删除该用户,以保证后继账号能够正常登录。
   
[java]  view plain copy
  1. public class SessionListener implements HttpSessionListener{  
  2.        
  3.     @Override  
  4.     public void sessionCreated(HttpSessionEvent event) {  
  5.            
  6.     }  
  7.   
  8.       
  9.     @Override  
  10.     public void sessionDestroyed(HttpSessionEvent event) {  
  11.         //监听session的失效和销毁  
  12.         HttpSession session=event.getSession();  
  13.         ServletContext application=session.getServletContext();  
  14.       
  15.         try{  
  16.              String username=(String) session.getAttribute(WebConstant.USER_ID);  
  17.              Long userlogicId=(Long)session.getAttribute(WebConstant.USER_LOGICID);  
  18.              Map<Long, String> loginUserMap = (Map<Long, String>)application.getAttribute(WebConstant.LOGIN_USER_MAP);   
  19.              if(loginUserMap.containsKey(userlogicId))  
  20.                  loginUserMap.remove(userlogicId);  
  21.              application.setAttribute(WebConstant.LOGIN_USER_MAP, loginUserMap);  
  22.               System.out.println("session:"+session.getId()+"已失效");  
  23.          }  
  24.          catch(Exception e){  
  25.                 System.out.println(e.getMessage());  
  26.          }  
  27.     }  
  28.   
  29.   
  30. }  

     到这里,基本能做到限制用户的重复登录了,但是靠这些异常情况依然无法处理,如用户使用过程中关闭浏览器,再次登录时,由于信息记录在服务器端的application中且未按照正常安全退出流程执行,则执行登录操作会提示"已在别处登录"。要解决这个问题,如下:

二、

1.在mainframe页面绑定jQuery的beforeunload事件,在关闭浏览器时执行退出程序

[javascript]  view plain copy
  1. $(function() {  
  2.         console.log("daemon");  
  3.         $(window).bind('beforeunload'function() {  
  4.             //关闭浏览器则执行退出程序  
  5.             $.ajax({  
  6.                 url : "logout.action",  
  7.                 type : "post",  
  8.                 success : function() {  
  9.                     messagebox.alert("退出","您已成功登出!");  
  10.                 }  
  11.             });  
  12.         });  
  13.     });  

这种做法确实能实现关闭浏览器实现用户退出的功能,不过通过查阅jquery手册,发现beforeunload事件监听的不仅仅是浏览器的关闭,同时,页面的刷新操作以及页面的重定向均可导致此事件的发生,显然这些操作我们并不希望执行退出操作,我做了一些尝试,在页面中判断beforeunload监听的是哪一种操作,以达到只监听浏览器关闭的事件,不过经过多次尝试,发现各个浏览器的兼容性差异太大,只得作罢。

   2.设置"心跳信号"。也就是在页面中设置一个隐藏的iframe,定时地发送请求到服务器(如每隔10s),如果一定时间内服务器没有响应(如30s),则判定用户已退出,执行登出程序。这种做法能有效监听各种异常事件,并在短时间内做出相应的处理。但是由于页面会一直不停的发送请求到服务器,如果用户量变大的时候,则服务器的压力也随之增大,这时候这种方法则不是一个比较好的解决方案。

    3.由于上述两种方案均有着较大的缺陷,想到第三种方法。这种方法和第一种方法从大体思路上是类似的,即用beforeunload事件监听,但是判断是哪种操作放在服务器端。考虑到beforeunload事件监听的是离开页面的事件,关闭页面或浏览器操作为直接离开,而重定向和刷新是由离开当前页面和重新加载页面两步组成,于是我们可以考虑用session的getLastAccessedTime()方法来加以区分,getLastAccessedTime()能获取session的最后访问时间,对于关闭浏览器操作,一旦监听到执行logout 程序则最后访问事件就是关闭浏览器时的时间,而刷新和重定向操作的最后访问时间为重新加载当前页面的时间,这样便可以加以区分这两种操作。

   Code:

[java]  view plain copy
  1. <pre name="code" class="java">public String logOut()  throws Exception  
  2. {  
  3.     HttpSession session = request.getSession(true);  
  4.     ActionContext acx=ActionContext.getContext();  
  5.     try{  
  6.         long lastAccessedTime1=session.getLastAccessedTime();   //离开当前页面的时间  
  7.         Thread.sleep(2000);  
  8.         long lastAccessedTime2=session.getLastAccessedTime();   //重新加载当前页面的时间(刷新和重定向)  
  9.         System.out.println(lastAccessedTime1);  
  10.         System.out.println(lastAccessedTime2);  
  11.         if(lastAccessedTime1!=lastAccessedTime2)  
  12.             return NONE;  
  13.     }catch(IllegalStateException e){  
  14.         return NONE;  
  15.     }  
  16.    
  17.    
  18.     //把loginUserMap中保存的键值对清除  
  19.    try{  
  20.        Map<String, String> loginUserMap = (Map<String, String>)acx.getApplication().get(WebConstant.LOGIN_USER_MAP);  
  21.        String username=(String)session.getAttribute(WebConstant.USER_ID);  
  22.    
  23.        if(loginUserMap.containsKey(username)){  
  24.            loginUserMap.remove(username);                            
  25.        }  
  26.        session.getServletContext().setAttribute("loginUserMap", loginUserMap);  
  27.    
  28.        //处理用户退出  
  29.        Long id=(Long) session.getAttribute(WebConstant.USER_LOGICID);  
  30.        if(id!=null)  
  31.         this.userManageService.userLogout(id);  
  32.    
  33.     session.removeAttribute(WebConstant.USER_ID);  
  34.     session.removeAttribute(WebConstant.USER_LOGICID);  
  35.     session.removeAttribute(WebConstant.LOGIN_TIME);  
  36.     // 使Session失效  
  37.      session.invalidate();  
  38.     //缓存设置  
  39.     response.setHeader("Cache-Control","no-cache");  
  40.     response.setHeader("Cache-Control","no-store");  
  41.     response.setDateHeader("Expires"0);  
  42.    
  43.    }catch(NullPointerException e){  
  44.    
  45.    }  
  46.    return NONE;  
  47. }  


 

 而对于浏览器崩溃,断网等异常情况,只能通过session失效时的监听器进行处理 。这里监听器的作用在于当session超时的时候,删除loginUserMap中的用户信息,这样当达到session失效的时间以后同时清除已登录的用户的信息。

Code:

[java]  view plain copy
  1. public class SessionListener implements HttpSessionListener{  
  2.    
  3.     @Override  
  4.     public void sessionCreated(HttpSessionEvent event) {  
  5.    
  6.     }  
  7.    
  8.    
  9.     @Override  
  10.     public void sessionDestroyed(HttpSessionEvent event) {  
  11.         //监听session的失效和销毁  
  12.         HttpSession session=event.getSession();  
  13.          ServletContext application=session.getServletContext();  
  14.          try{  
  15.              String username=(String) session.getAttribute(WebConstant.USER_ID);  
  16.              Map<String, String> loginUserMap = (Map<String, String>)application.getAttribute(WebConstant.LOGIN_USER_MAP);  
  17.              if(loginUserMap.containsKey(username))  
  18.                  loginUserMap.remove(username);  
  19.              application.setAttribute(WebConstant.LOGIN_USER_MAP, loginUserMap);  
  20.          }  
  21.          catch(Exception e){  
  22.                 System.out.println(e.getMessage());  
  23.          }  
  24.     }  
  25.    
  26. }  

这篇关于java web 用户单点登录的方案的基本实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1129827

相关文章

java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)

《java中pdf模版填充表单踩坑实战记录(itextPdf、openPdf、pdfbox)》:本文主要介绍java中pdf模版填充表单踩坑的相关资料,OpenPDF、iText、PDFBox是三... 目录准备Pdf模版方法1:itextpdf7填充表单(1)加入依赖(2)代码(3)遇到的问题方法2:pd

Java Stream流之GroupBy的用法及应用场景

《JavaStream流之GroupBy的用法及应用场景》本教程将详细介绍如何在Java中使用Stream流的groupby方法,包括基本用法和一些常见的实际应用场景,感兴趣的朋友一起看看吧... 目录Java Stream流之GroupBy的用法1. 前言2. 基础概念什么是 GroupBy?Stream

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

SpringBoot监控API请求耗时的6中解决解决方案

《SpringBoot监控API请求耗时的6中解决解决方案》本文介绍SpringBoot中记录API请求耗时的6种方案,包括手动埋点、AOP切面、拦截器、Filter、事件监听、Micrometer+... 目录1. 简介2.实战案例2.1 手动记录2.2 自定义AOP记录2.3 拦截器技术2.4 使用Fi

最新Spring Security的基于内存用户认证方式

《最新SpringSecurity的基于内存用户认证方式》本文讲解SpringSecurity内存认证配置,适用于开发、测试等场景,通过代码创建用户及权限管理,支持密码加密,虽简单但不持久化,生产环... 目录1. 前言2. 因何选择内存认证?3. 基础配置实战❶ 创建Spring Security配置文件

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

Java Thread中join方法使用举例详解

《JavaThread中join方法使用举例详解》JavaThread中join()方法主要是让调用改方法的thread完成run方法里面的东西后,在执行join()方法后面的代码,这篇文章主要介绍... 目录前言1.join()方法的定义和作用2.join()方法的三个重载版本3.join()方法的工作原