实现CAS单点登出

2023-11-20 14:08
文章标签 实现 单点 登出 cas

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

由于项目需求要实现单点登出需要在网上找了N久终于实现单点登出。

使用cas-server-core-3.3.3.jar(CAS Server 3.3.3)

使用cas-client-core-3.1.3.jar(CAS Client 3.1.3)

 

项目结合CAS SpringSecurity SSH

 

普通项目(没有结合Spring Security)的可以在web.xml中加入如下代码

[xhtml]  view plain copy
  1. <filter>  
  2.    <filter-name>CAS Single Sign Out Filter</filter-name>  
  3.    <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>  
  4. </filter>  
  5.   
  6. <filter-mapping>  
  7.    <filter-name>CAS Single Sign Out Filter</filter-name>  
  8.    <url-pattern>/*</url-pattern>  
  9. </filter-mapping>  
  10.   
  11. <listener>  
  12.     <listener-class>  
  13.        org.jasig.cas.client.session.SingleSignOutHttpSessionListener  
  14.     </listener-class>  
  15. </listener>  

 

 

在我们的项目中由于结合了SpringSecurity 可以将filter加入到spring Security过滤链中,也可以直接向上面的一样加入web.xml中

首先在web.xml中加入监听器。

[xhtml]  view plain copy
  1. <!-- single sign out -->  
  2. <listener>  
  3.       <listener-class>  
  4.           org.jasig.cas.client.session.SingleSignOutHttpSessionListener  
  5.       </listener-class>  
  6. </listener>  
  7. <!-- single sign out -->  

 

然后把filter加入到spring Security过滤链中

 

[xhtml]  view plain copy
  1. <!-- single sign out -->  
  2. <b:bean id="casSingleSignOutFilter" class="check.SingleSignOutFilter">  
  3.     <custom-filter before="CAS_PROCESSING_FILTER"/>  
  4. </b:bean>  
  5. <!-- single sign out -->  

 

注意上面的class="check.SingleSignOutFilter"是我自定义的filter(由于CAS3.1.3定义的SingleSignOutFilter在某种意思上没有起到作用)详情请见http://www.javaeye.com/topic/546785

自己定义一个类

[java]  view plain copy
  1. package check;  
  2.   
  3. import java.io.IOException;  
  4. import java.util.Enumeration;  
  5.   
  6. import javax.servlet.FilterChain;  
  7. import javax.servlet.FilterConfig;  
  8. import javax.servlet.ServletException;  
  9. import javax.servlet.ServletRequest;  
  10. import javax.servlet.ServletResponse;  
  11. import javax.servlet.http.HttpServletRequest;  
  12. import javax.servlet.http.HttpSession;  
  13.   
  14. import org.apache.commons.logging.Log;  
  15. import org.apache.commons.logging.LogFactory;  
  16. import org.jasig.cas.client.session.HashMapBackedSessionMappingStorage;  
  17. import org.jasig.cas.client.session.SessionMappingStorage;  
  18. import org.jasig.cas.client.util.AbstractConfigurationFilter;  
  19. import org.jasig.cas.client.util.CommonUtils;  
  20. import org.jasig.cas.client.util.XmlUtils;  
  21.   
  22. public final class SingleSignOutFilter extends AbstractConfigurationFilter  
  23. {  
  24.   private String artifactParameterName;  
  25.   private static SessionMappingStorage SESSION_MAPPING_STORAGE = new HashMapBackedSessionMappingStorage();  
  26.   private static Log log = LogFactory.getLog(SingleSignOutFilter.class);  
  27.   
  28.   public SingleSignOutFilter()  
  29.   {  
  30.     this.artifactParameterName = "ticket";  
  31.   }  
  32.   
  33.   public void init(FilterConfig filterConfig)  
  34.     throws ServletException  
  35.   {  
  36.     setArtifactParameterName(getPropertyFromInitParams(filterConfig, "artifactParameterName""ticket"));  
  37.     init();  
  38.   }  
  39.   
  40.   public void init() {  
  41.     CommonUtils.assertNotNull(this.artifactParameterName, "artifactParameterName cannot be null.");  
  42.     CommonUtils.assertNotNull(SESSION_MAPPING_STORAGE, "sessionMappingStorage cannote be null.");  
  43.   }  
  44.   
  45.   public void setArtifactParameterName(String artifactParameterName) {  
  46.     this.artifactParameterName = artifactParameterName;  
  47.   }  
  48.   
  49.   public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {  
  50.     final HttpServletRequest request = (HttpServletRequest) servletRequest;     
  51.     final String logoutRequest = CommonUtils.safeGetParameter(request, "logoutRequest");     
  52.     Enumeration ff = request.getParameterNames();     
  53.     String a = request.getQueryString();     
  54.     if (CommonUtils.isNotBlank(logoutRequest)) {     
  55.          final String sessionIdentifier = XmlUtils.getTextForElement(logoutRequest, "SessionIndex");     
  56.   
  57.          if (CommonUtils.isNotBlank(sessionIdentifier)) {     
  58.             final HttpSession session = SESSION_MAPPING_STORAGE.removeSessionByMappingId(sessionIdentifier);     
  59.   
  60.             if (session != null) {     
  61.                  String sessionID = session.getId();                        
  62.                  try {     
  63.                     session.invalidate();     
  64.                  } catch (final IllegalStateException e) {     
  65.                          
  66.                  }     
  67.             }     
  68.          }     
  69.      }     
  70.          
  71.     else{     
  72.         final String artifact = CommonUtils.safeGetParameter(request, this.artifactParameterName);     
  73.         final HttpSession session = request.getSession(false);     
  74.              
  75.         if (CommonUtils.isNotBlank(artifact) && session!=null) {     
  76.             try {     
  77.                 SESSION_MAPPING_STORAGE.removeBySessionById(session.getId());     
  78.             } catch (final Exception e) {     
  79.                      
  80.             }     
  81.             SESSION_MAPPING_STORAGE.addSessionById(artifact, session);     
  82.         }     
  83.     }     
  84.   
  85.     filterChain.doFilter(servletRequest, servletResponse);     
  86.   }  
  87.   
  88.   public void setSessionMappingStorage(SessionMappingStorage storage) {  
  89.     SESSION_MAPPING_STORAGE = storage;  
  90.   }  
  91.   
  92.   public static SessionMappingStorage getSessionMappingStorage() {  
  93.     return SESSION_MAPPING_STORAGE;  
  94.   }  
  95.   
  96.   public void destroy()  
  97.   {  
  98.   }  
  99. }  

完成。

这样即可实现单点登出。(所有java应用的单点退出)

1)这样实现的效果是在登出的时候CAS Server 分发给各个客户端让各个客户端都登出,这个得让FIlter来获取,例子:一个index页面有两个链接一个指向java应用,一个指向php应用在java应用加filter 后能做出相应的动作退出动作,而对于php自己没加任何filter就没有退出。所以也得写个filter。

 

 

2)由于我们点击退出的时候请求CAS Server 而后Server分发任务让每个应用退出的消息,java程序通过filter来执行退出。PHP提供了一个phpCAS::handleLogoutRequests()来检验服务器发来的信息,

具体我们可以把这个代码放在phpbb3/include/function.php中的点击事件里面代码如下:

[php]  view plain copy
  1. if(!$admin && CAS_ENABLE){  
  2.     // initialize phpCAS   
  3.     phpCAS::client(CAS_VERSION_2_0, CAS_SERVER_HOSTNAME, CAS_SERVER_PORT, CAS_SERVER_APP_NAME);   
  4.     phpCAS::setNoCasServerValidation();   
  5.     // force CAS authentication   
  6.     phpCAS::handleLogoutRequests();//加的去看看有没有服务器端发出注销消息。  
  7.     phpCAS::forceAuthentication();   

这篇关于实现CAS单点登出的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

MySQL中On duplicate key update的实现示例

《MySQL中Onduplicatekeyupdate的实现示例》ONDUPLICATEKEYUPDATE是一种MySQL的语法,它在插入新数据时,如果遇到唯一键冲突,则会执行更新操作,而不是抛... 目录1/ ON DUPLICATE KEY UPDATE的简介2/ ON DUPLICATE KEY UP

Python中Json和其他类型相互转换的实现示例

《Python中Json和其他类型相互转换的实现示例》本文介绍了在Python中使用json模块实现json数据与dict、object之间的高效转换,包括loads(),load(),dumps()... 项目中经常会用到json格式转为object对象、dict字典格式等。在此做个记录,方便后续用到该方

JWT + 拦截器实现无状态登录系统

《JWT+拦截器实现无状态登录系统》JWT(JSONWebToken)提供了一种无状态的解决方案:用户登录后,服务器返回一个Token,后续请求携带该Token即可完成身份验证,无需服务器存储会话... 目录✅ 引言 一、JWT 是什么? 二、技术选型 三、项目结构 四、核心代码实现4.1 添加依赖(pom

SpringBoot路径映射配置的实现步骤

《SpringBoot路径映射配置的实现步骤》本文介绍了如何在SpringBoot项目中配置路径映射,使得除static目录外的资源可被访问,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一... 目录SpringBoot路径映射补:springboot 配置虚拟路径映射 @RequestMapp

Python与MySQL实现数据库实时同步的详细步骤

《Python与MySQL实现数据库实时同步的详细步骤》在日常开发中,数据同步是一项常见的需求,本篇文章将使用Python和MySQL来实现数据库实时同步,我们将围绕数据变更捕获、数据处理和数据写入这... 目录前言摘要概述:数据同步方案1. 基本思路2. mysql Binlog 简介实现步骤与代码示例1

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

基于C#实现PDF转图片的详细教程

《基于C#实现PDF转图片的详细教程》在数字化办公场景中,PDF文件的可视化处理需求日益增长,本文将围绕Spire.PDFfor.NET这一工具,详解如何通过C#将PDF转换为JPG、PNG等主流图片... 目录引言一、组件部署二、快速入门:PDF 转图片的核心 C# 代码三、分辨率设置 - 清晰度的决定因

Java Kafka消费者实现过程

《JavaKafka消费者实现过程》Kafka消费者通过KafkaConsumer类实现,核心机制包括偏移量管理、消费者组协调、批量拉取消息及多线程处理,手动提交offset确保数据可靠性,自动提交... 目录基础KafkaConsumer类分析关键代码与核心算法2.1 订阅与分区分配2.2 拉取消息2.3

SpringBoot集成XXL-JOB实现任务管理全流程

《SpringBoot集成XXL-JOB实现任务管理全流程》XXL-JOB是一款轻量级分布式任务调度平台,功能丰富、界面简洁、易于扩展,本文介绍如何通过SpringBoot项目,使用RestTempl... 目录一、前言二、项目结构简述三、Maven 依赖四、Controller 代码详解五、Service

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python