SSO单点登录系列6:cas单点登录防止登出退出后刷新后退ticket失效报500错

这个问题之前就发现过,最近有几个哥们一直在问我这个怎么搞,我手上在做另一个项目,cas就暂时搁浅了几周。现在我们来一起改一下你的应用(client2/3)的web.xml来解决这个2b问题,首先看下错误描述:

问题: 我登录了client2,又登录了client3,现在我把client2退出了,在client3里面我F5刷新了一下,结果页面报错:

未能够识别出目标 'ST-41-2VcnVMguCDWJX5zHaaaD-cas01.example.org'票根

[html]  view plain copy
 
  1. <span style="font-family:Microsoft YaHei;font-size:12px;">type Exception report  
  2.   
  3. message org.jasig.cas.client.validation.TicketValidationException:  
  4.   
  5. description The server encountered an internal error that prevented it from fulfilling this request.  
  6.   
  7. exception  
  8.   
  9. javax.servlet.ServletException: org.jasig.cas.client.validation.TicketValidationException:   
  10.         鏈兘澶熻瘑鍒嚭鐩爣 'ST-41-2VcnVMguCDWJX5zHaaaD-cas01.example.org'绁ㄦ牴  
  11.       
  12.     org.jasig.cas.client.validation.AbstractTicketValidationFilter.doFilter(AbstractTicketValidationFilter.java:155)  
  13.     org.jasig.cas.client.session.SingleSignOutFilter.doFilter(SingleSignOutFilter.java:99)  
  14.     org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)  
  15.     org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)  
  16. root cause  
  17.   
  18. org.jasig.cas.client.validation.TicketValidationException:   
  19.         鏈兘澶熻瘑鍒嚭鐩爣 'ST-41-2VcnVMguCDWJX5zHaaaD-cas01.example.org'绁ㄦ牴  
  20.       
  21.     org.jasig.cas.client.validation.Cas20ServiceTicketValidator.parseResponseFromServer(Cas20ServiceTicketValidator.java:73)  
  22.     org.jasig.cas.client.validation.AbstractUrlBasedTicketValidator.validate(AbstractUrlBasedTicketValidator.java:188)  
  23.     org.jasig.cas.client.validation.AbstractTicketValidationFilter.doFilter(AbstractTicketValidationFilter.java:132)  
  24.     org.jasig.cas.client.session.SingleSignOutFilter.doFilter(SingleSignOutFilter.java:99)  
  25.     org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)  
  26.     org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)  
  27. note The full stack trace of the root cause is available in the Apache Tomcat/7.0.37 logs.</span>  


 

猜都能猜出来,我注销了,ticket已经失效了,现在我又发回到server端,它就报错了。(客户端发过去就报错了),以下就是cas ticket失效处理的一个很简单的解决办法,复杂的话,需要修改client源码进行异常处理。

 

1.所以针对这个情况,我只能在web.xml中下手了,(你也可以修改客户端的jar包中的一些java类,自己去做这个异常处理,接收所有在cas使用过程中会出错的处理,全部跳转到错误页面中,让掉线的人重新登录。在这里,我们采用web.xml配置一下)

 

2.这是官网解释:https://wiki.jasig.org/display/CASC/Configuring+the+Jasig+CAS+Client+for+Java+in+the+web.xml 它的解释:

 

The correct order of the filters in web.xml is necessary:

  1. AuthenticationFilter
  2. TicketValidationFilter (whichever one is chosen)
  3. HttpServletRequestWrapperFilter
  4. AssertionThreadLocalFilter

 

意思是说,过滤器链不要错,我之前的那篇教程里cas客户端配置web.xml没有使用这几个过滤器,现在我们重新使用它。

 

3.这是一个哥们之前解释的:我贴出来。

单点登出,客户端配置。我尝试使用SAML作为认证和Ticket校验,但是调试时发现单点登出取标识的方式只能识别CAS的认证和校验。
认证:org.jasig.cas.client.authentication.AuthenticationFilter
校验:org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter
过滤器顺序:
1. CAS Single Sign Out Filter
2. CAS Validation Filter
3. CAS Authentication Filter
4. CAS HttpServletRequest Wrapper Filter
5. CAS Assertion Thread Local Filter
特别注意Validation在Authentication之前,因为我使用的是Cas20ProxyReceivingTicketValidationFilter。根据CAS文档描述:If you are using proxy validation, you should map the validation filter before the authentication filter.

 

4.ok,放上我的web.xml文件,废掉之前的cas验证过滤器(CAS Filter)。使用另一个过滤器(CAS Authentication Filter),并且增加另外三个过滤器(CAS Validation Filter,CAS HttpServletRequest Wrapper Filter,CAS Assertion Thread Local Filter),注意过滤器的顺序.

 

[html]  view plain copy
 
  1.   

 

 

[html]  view plain copy
 
  1. <span style="font-family:Microsoft YaHei;font-size:12px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app xmlns="http://java.sun.com/xml/ns/javaee"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  5.   
  6.     <!-- 解决中文乱码问题 -->  
  7.     <filter>  
  8.         <filter-name>spring filter</filter-name>  
  9.         <filter-class>  
  10.             org.springframework.web.filter.CharacterEncodingFilter  
  11.         </filter-class>  
  12.         <init-param>  
  13.             <param-name>encoding</param-name>  
  14.             <param-value>UTF-8</param-value>  
  15.         </init-param>  
  16.     </filter>  
  17.     <filter-mapping>  
  18.         <filter-name>spring filter</filter-name>  
  19.         <url-pattern>/*</url-pattern>  
  20.     </filter-mapping>  
  21.     <!-- 解决中文乱码问题 -->  
  22.   
  23.   
  24.     <!--1.用于单点退出 -->  
  25.     <listener>  
  26.         <listener-class>  
  27.             org.jasig.cas.client.session.SingleSignOutHttpSessionListener  
  28.         </listener-class>  
  29.     </listener>  
  30.   
  31.     <filter>  
  32.         <filter-name>CAS Single Sign Out Filter</filter-name>  
  33.         <filter-class>  
  34.             org.jasig.cas.client.session.SingleSignOutFilter  
  35.         </filter-class>  
  36.     </filter>  
  37.   
  38.     <filter-mapping>  
  39.         <filter-name>CAS Single Sign Out Filter</filter-name>  
  40.         <url-pattern>/*</url-pattern>  
  41.     </filter-mapping>  
  42.   
  43.   
  44.   
  45.     <!--2.负责Ticket校验-->  
  46.     <filter>  
  47.         <filter-name>CAS Validation Filter</filter-name>  
  48.         <filter-class>  
  49.             org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter  
  50.         </filter-class>  
  51.         <init-param>  
  52.             <param-name>casServerUrlPrefix</param-name>  
  53.             <param-value>  
  54.                 http://192.168.168.141:8080/casServer  
  55.             </param-value>  
  56.         </init-param>  
  57.         <init-param>  
  58.             <param-name>serverName</param-name>  
  59.             <param-value>192.168.168.141:8080</param-value>  
  60.         </init-param>  
  61.         <init-param>  
  62.             <param-name>useSession</param-name>  
  63.             <param-value>true</param-value>  
  64.         </init-param>  
  65.   
  66.         <init-param>  
  67.             <param-name>exceptionOnValidationFailure</param-name>  
  68.             <param-value>false</param-value>  
  69.         </init-param>  
  70.   
  71.         <init-param>  
  72.             <param-name>redirectAfterValidation</param-name>  
  73.             <param-value>true</param-value>  
  74.         </init-param>  
  75.     </filter>  
  76.   
  77.     <filter-mapping>  
  78.         <filter-name>CAS Validation Filter</filter-name>  
  79.         <url-pattern>/*</url-pattern>  
  80.     </filter-mapping>  
  81.   
  82.     <!-- 3. 单点登录验证 -->  
  83.   
  84.     <filter>  
  85.         <filter-name>CAS Authentication Filter</filter-name>  
  86.         <filter-class>  
  87.             org.jasig.cas.client.authentication.AuthenticationFilter  
  88.         </filter-class>  
  89.         <init-param>  
  90.             <param-name>casServerLoginUrl</param-name>  
  91.             <param-value>  
  92.                 http://192.168.168.141:8080/casServer/login  
  93.             </param-value>  
  94.         </init-param>  
  95.         <init-param>  
  96.             <param-name>serverName</param-name>  
  97.             <param-value>http://192.168.168.141:8080</param-value>  
  98.         </init-param>  
  99.     </filter>  
  100.     <filter-mapping>  
  101.         <filter-name>CAS Authentication Filter</filter-name>  
  102.         <url-pattern>/*</url-pattern>  
  103.     </filter-mapping>  
  104.   
  105.   
  106.   
  107.     <!-- 3.用于单点登录 去服务器端认证(之前使用的这种)  
  108.             <filter>  
  109.             <filter-name>CAS Filter</filter-name>  
  110.             <filter-class>  
  111.             edu.yale.its.tp.cas.client.filter.CASFilter  
  112.             </filter-class>  
  113.             <init-param>  
  114.             <param-name>  
  115.             edu.yale.its.tp.cas.client.filter.loginUrl  
  116.             </param-name>  
  117.             <param-value>  
  118.             http://192.168.168.141:8080/casServer/login  
  119.             </param-value>  
  120.             </init-param>  
  121.             <init-param>  
  122.             <param-name>  
  123.             edu.yale.its.tp.cas.client.filter.validateUrl  
  124.             </param-name>  
  125.             <param-value>  
  126.             http://192.168.168.141:8080/casServer/serviceValidate  
  127.             </param-value>  
  128.             </init-param>  
  129.             <init-param>  
  130.             <param-name>  
  131.             edu.yale.its.tp.cas.client.filter.serverName  
  132.             </param-name>  
  133.             <param-value>192.168.168.141:8080</param-value>  
  134.             </init-param>  
  135.             </filter>  
  136.         -->  
  137.   
  138.   
  139.     <!--4.  CAS HttpServletRequest Wrapper Filter 这个是HttpServletRequet的包裹类,让他支持getUserPrincipal,getRemoteUser方法来取得用户信息-->  
  140.   
  141.     <filter>  
  142.         <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
  143.         <filter-class>  
  144.             org.jasig.cas.client.util.HttpServletRequestWrapperFilter  
  145.         </filter-class>  
  146.     </filter>  
  147.   
  148.     <filter-mapping>  
  149.         <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
  150.         <url-pattern>/*</url-pattern>  
  151.     </filter-mapping>  
  152.   
  153.   
  154.     <!--5. CAS Assertion Thread Local Filter  这个类把Assertion信息放在ThreadLocal变量中,这样应用程序不在web层也能够获取到当前登录信息-->  
  155.   
  156.     <filter>  
  157.         <filter-name>CAS Assertion Thread Local Filter</filter-name>  
  158.         <filter-class>  
  159.             org.jasig.cas.client.util.AssertionThreadLocalFilter  
  160.         </filter-class>  
  161.     </filter>  
  162.   
  163.     <filter-mapping>  
  164.         <filter-name>CAS Assertion Thread Local Filter</filter-name>  
  165.         <url-pattern>/*</url-pattern>  
  166.     </filter-mapping>  
  167.   
  168.   
  169.     <servlet>  
  170.         <servlet-name>Query</servlet-name>  
  171.         <servlet-class>servlet.Query</servlet-class>  
  172.     </servlet>  
  173.   
  174.     <servlet-mapping>  
  175.         <servlet-name>Query</servlet-name>  
  176.         <url-pattern>/query</url-pattern>  
  177.     </servlet-mapping>  
  178.   
  179.     <welcome-file-list>  
  180.         <welcome-file>index.jsp</welcome-file>  
  181.     </welcome-file-list>  
  182. </web-app>  
  183. </span>  

5.如果这样做了,你还需要一件事情,就是前台获取用户信息的方式改了,我的index.jsp改成了这个:

 

 

 

[java]  view plain copy
 
  1. <span style="font-family:Microsoft YaHei;font-size:12px;"><%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%@page import="edu.yale.its.tp.cas.client.filter.CASFilter"%>  
  3. <%@page import="org.jasig.cas.client.util.AssertionThreadLocalFilter"%>  
  4. <%@page import="org.jasig.cas.client.util.HttpServletRequestWrapperFilter"%>  
  5. <%@page import="org.jasig.cas.client.authentication.AttributePrincipal"%>  
  6. <%@page import="org.jasig.cas.client.util.AbstractCasFilter"%>  
  7. <%@page import="org.jasig.cas.client.validation.Assertion"%>  
  8.   
  9.   
  10. <body>  
  11.   
  12.         <h1>  
  13.             登录成功,这是客户端2  
  14.         </h1>  
  15.         <br />  
  16.   
  17.         欢迎您:  
  18.   
  19.         <%  
  20.             //String username = (String) session.getAttribute(CASFilter.CAS_FILTER_USER);  
  21.             //String username2 = (String)AssertionHolder.getAssertion().getPrincipal().getName();  
  22.             String username = "";  
  23.              AttributePrincipal principal = (AttributePrincipal) request.getUserPrincipal();  
  24.              if(principal != null){  
  25.                username = principal.getName();//获取用户名  
  26.              }  
  27.               
  28.            
  29.               
  30.         %>  
  31.         用户名:<%=username%></span>  

ok,我的应用之间如果一个退出,另一个就算带ticket参数也不不再报错了,就算是测试组的兄弟拿到那段ticket复制粘贴到另一个浏览器中进行访问,也不会报错。

 

ps:

也有兄弟说可以通过修改C:\tomcat7\webapps\casServer\WEB-INF\spring-configuration\ticketExpirationPolicies.xml这个文件中的

 

 

[html]  view plain copy
 
  1.   <!-- Expiration policies -->  
  2.     <util:constant id="SECONDS" static-field="java.util.concurrent.TimeUnit.SECONDS"/>  
  3.     <bean id="serviceTicketExpirationPolicy" class="org.jasig.cas.ticket.support.MultiTimeUseOrTimeoutExpirationPolicy"  
  4.           c:numberOfUses="1" c:timeToKill="${st.timeToKillInSeconds:10}" c:timeUnit-ref="SECONDS"/>  
其中那个

 

 c:numberOfUses="1" //使用ticket多少次

 c:timeToKill="${st.timeToKillInSeconds:10}" //多少秒过期,默认10秒,你把这个改成10分钟玩玩。

这个方法我没有尝试,所以希望想尝试想折腾和想玩的兄弟狠狠的点击这个链接:http://bbs.csdn.net/topics/390111112

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值