第十一节 CAS服务端RememberMe

关于启用RememberMe功能所需做的修改描述

  1. /** 
  2.  * @see CAS服务端RememberMe 
  3.  * @see ------------------------------------------------------------------------------------------------------------------------ 
  4.  * @see 关于RememberMe,可参考官方文档,网址如下(下面两个网址描述的RememberMe实现都是一样的,只是第二个还有其它描述) 
  5.  * @see http://jasig.github.io/cas/development/installation/Configuring-LongTerm-Authentication.html 
  6.  * @see http://jasig.github.io/cas/4.0.x/installation/Configuring-Authentication-Components.html#long-term-authentication 
  7.  * @see RememberMe也就是平时所说的记住密码的功能,可以让用户登录成功后,关闭浏览器再重新打开浏览器访问应用时不需要再次登录 
  8.  * @see RememberMe与上面的Session超时配置tgt.timeToKillInSeconds是两回事,Session超时是针对一次会话而言,RememberMe则更广 
  9.  * @see 另外本文的CAS-4.0.3服务端源码修改,是在我的以下三篇博文基础上修改的,最终我会在CSDN上提供整体源码下载 
  10.  * @see http://blog.csdn.net/jadyer/article/details/46875393 
  11.  * @see http://blog.csdn.net/jadyer/article/details/46914661 
  12.  * @see http://blog.csdn.net/jadyer/article/details/46916169 
  13.  * @see 具体修改步骤如下 
  14.  * @see 1.cas.properties中新增配置项rememberMeDuration=1209600 
  15.  * @see 2.ticketExpirationPolicies.xml中新增RememberMe过期策略的配置 
  16.  * @see 3.ticketGrantingTicketCookieGenerator.xml中新增属性项p:rememberMeMaxAge="${rememberMeDuration:1209600}" 
  17.  * @see 4.deployerConfigContext.xml 
  18.  * @see 5.casLoginView.jsp表单中增加rememberMe字段 
  19.  * @see 6.login-webflow.xml增加接收表单rememberMe字段的配置 
  20.  * @see 7.UsernamePasswordCaptchaCredential.java集成RememberMeUsernamePasswordCredential使得可以接收表单的rememberMe字段 
  21.  * @see ------------------------------------------------------------------------------------------------------------------------ 
  22.  * @create  @create 2016-6-6 下午08:34:18
  23.  * @author 玄玉<http://blog.csdn.net/jadyer> 
  24.  */  

1.ticketExpirationPolicies.xml的修改

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!--  
  3.   
  4.     Licensed to Jasig under one or more contributor license  
  5.     agreements. See the NOTICE file distributed with this work  
  6.     for additional information regarding copyright ownership.  
  7.     Jasig licenses this file to you under the Apache License,  
  8.     Version 2.0 (the "License"); you may not use this file  
  9.     except in compliance with the License.  You may obtain a  
  10.     copy of the License at the following location:  
  11.   
  12.       http://www.apache.org/licenses/LICENSE-2.0  
  13.   
  14.     Unless required by applicable law or agreed to in writing,  
  15.     software distributed under the License is distributed on an  
  16.     "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY  
  17.     KIND, either express or implied.  See the License for the  
  18.     specific language governing permissions and limitations  
  19.     under the License.  
  20.   
  21. -->  
  22. <beans xmlns="http://www.springframework.org/schema/beans"  
  23.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  24.        xmlns:p="http://www.springframework.org/schema/p"  
  25.        xmlns:c="http://www.springframework.org/schema/c" xmlns:util="http://www.springframework.org/schema/util"  
  26.        xsi:schemaLocation="http://www.springframework.org/schema/beans  
  27.                            http://www.springframework.org/schema/beans/spring-beans.xsd  
  28.                            http://www.springframework.org/schema/util  
  29.                            http://www.springframework.org/schema/util/spring-util.xsd">  
  30.     <description>  
  31.         Assignment of expiration policies for the different tickets generated by CAS including ticket granting ticket  
  32.         (TGT), service ticket (ST), proxy granting ticket (PGT), and proxy ticket (PT).  
  33.         These expiration policies determine how long the ticket they are assigned to can be used and even how often they  
  34.         can be used before becoming expired / invalid.  
  35.     </description>  
  36.   
  37.     <!-- Expiration policies -->  
  38.     <util:constant id="SECONDS" static-field="java.util.concurrent.TimeUnit.SECONDS"/>  
  39.     <bean id="serviceTicketExpirationPolicy" class="org.jasig.cas.ticket.support.MultiTimeUseOrTimeoutExpirationPolicy"  
  40.           c:numberOfUses="1" c:timeToKill="${st.timeToKillInSeconds:10}" c:timeUnit-ref="SECONDS"/>  
  41.   
  42.     <!-- TicketGrantingTicketExpirationPolicy: Default as of 3.5 -->  
  43.     <!-- Provides both idle and hard timeouts, for instance 2 hour sliding window with an 8 hour max lifetime -->  
  44.     <!--   
  45.     <bean id="grantingTicketExpirationPolicy" class="org.jasig.cas.ticket.support.TicketGrantingTicketExpirationPolicy"  
  46.           p:maxTimeToLiveInSeconds="${tgt.maxTimeToLiveInSeconds:28800}"  
  47.           p:timeToKillInSeconds="${tgt.timeToKillInSeconds:7200}"/>  
  48.      -->  
  49.   
  50.     <!-- 以下为RememberMe所需配置 -->  
  51.     <!-- 这里要先把原有的<bean id="grantingTicketExpirationPolicy">注释掉,如上所示 -->  
  52.     <!-- 之所以注释是因为applicationContext.xml的第117行要用到<bean id="grantingTicketExpirationPolicy"> -->  
  53.     <!-- 而我们实现RememberMe需要用到的是RememberMeDelegatingExpirationPolicy,而非默认的TicketGrantingTicketExpirationPolicy -->  
  54.     <!-- 看看下面的配置就一目了然了 -->  
  55.     <!--  
  56.         | The following policy applies to standard CAS SSO sessions.  
  57.         | Default 2h (7200s) sliding expiration with default 8h (28800s) maximum lifetime.  
  58.     -->  
  59.     <bean id="standardSessionTGTExpirationPolicy" class="org.jasig.cas.ticket.support.TicketGrantingTicketExpirationPolicy"  
  60.           p:maxTimeToLiveInSeconds="${tgt.maxTimeToLiveInSeconds:28800}"  
  61.           p:timeToKillInSeconds="${tgt.timeToKillInSeconds:7200}"/>  
  62.     <!--  
  63.         | The following policy applies to long term CAS SSO sessions.  
  64.         | Default duration is two weeks (1209600s).  
  65.     -->  
  66.     <bean id="longTermSessionTGTExpirationPolicy" class="org.jasig.cas.ticket.support.TimeoutExpirationPolicy"  
  67.           c:timeToKillInMilliSeconds="#{ ${rememberMeDuration:1209600} * 1000 }"/>  
  68.     <bean id="grantingTicketExpirationPolicy" class="org.jasig.cas.ticket.support.RememberMeDelegatingExpirationPolicy"  
  69.           p:sessionExpirationPolicy-ref="standardSessionTGTExpirationPolicy"  
  70.           p:rememberMeExpirationPolicy-ref="longTermSessionTGTExpirationPolicy"/>  
  71. </beans>  

2.ticketGrantingTicketCookieGenerator.xml的修改

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:p="http://www.springframework.org/schema/p"  
  5.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">  
  6.     <description>  
  7.         Defines the cookie that stores the TicketGrantingTicket.  You most likely should never modify these (especially the "secure" property).  
  8.         You can change the name if you want to make it harder for people to guess.  
  9.     </description>  
  10.       
  11.     <!-- 针对RememberMe需增加p:rememberMeMaxAge属性配置 -->  
  12.     <bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"  
  13.         p:cookieSecure="false"  
  14.         p:cookieMaxAge="-1"  
  15.         p:rememberMeMaxAge="${rememberMeDuration:1209600}"  
  16.         p:cookieName="CASTGC"  
  17.         p:cookiePath="/cas" />  
  18. </beans>  

3.deployerConfigContext.xml修改的部分

  1. <bean id="authenticationManager" class="org.jasig.cas.authentication.PolicyBasedAuthenticationManager">  
  2.     <constructor-arg>  
  3.         <map>  
  4.             <entry key-ref="proxyAuthenticationHandler" value-ref="proxyPrincipalResolver" />  
  5.             <entry key-ref="primaryAuthenticationHandler" value-ref="primaryPrincipalResolver" />  
  6.         </map>  
  7.     </constructor-arg>  
  8.   
  9.     <property name="authenticationPolicy">  
  10.         <bean class="org.jasig.cas.authentication.AnyAuthenticationPolicy" />  
  11.     </property>  
  12.       
  13.     <!-- 针对RememberMe需增加的属性配置 -->  
  14.     <property name="authenticationMetaDataPopulators">  
  15.         <list>  
  16.             <bean class="org.jasig.cas.authentication.SuccessfulHandlerMetaDataPopulator"/>  
  17.             <bean class="org.jasig.cas.authentication.principal.RememberMeAuthenticationMetaDataPopulator"/>  
  18.         </list>  
  19.     </property>  
  20. </bean>  

4.login-webflow.xml修改的部分

  1. <view-state id="viewLoginForm" view="casLoginView" model="credential">  
  2.        <binder>  
  3.            <binding property="username"/>  
  4.            <binding property="password"/>  
  5.            <!-- 前台表单添加验证码字段captcha -->  
  6.            <binding property="captcha"/>  
  7.            <!-- 前台表单添加RememberMe字段 -->  
  8.            <binding property="rememberMe"/>  
  9.        </binder>  
  10.        <on-entry>  
  11.            <set name="viewScope.commandName" value="'credential'" />  
  12.        </on-entry>  
  13.     <transition on="submit" bind="true" validate="true" to="validateCaptcha">  
  14.            <evaluate expression="authenticationViaCaptchaFormAction.doBind(flowRequestContext, flowScope.credential)" />  
  15.        </transition>  
  16. </view-state>  

5.//WEB-INF//view//jsp//star//ui//casLoginView.jsp

  1. <tr>  
  2.             <td>  
  3.                 <input type="checkbox" tabindex="4" name="rememberMe" value="true"/>  
  4.                 <label for="warn">RememberMe</label>  
  5.             </td>  
  6.         </tr>

6.最后是cas.properties中增加的rememberMeDuration配置

  1. #服务端RememberMe的有效期,默认为1209600s,即两周  
  2. rememberMeDuration=120960

猜你喜欢

转载自starbhhc.iteye.com/blog/2303753