CAS SSO研究一:抛弃Https让Cas以Http协议提供单点登录服务

来源:互联网 发布:最新全国省市区数据库 编辑:程序博客网 时间:2024/06/07 03:51

转自: http://blog.csdn.net/ycyk_168/article/details/18668951

本文环境:

1、apache-tomcat-7.0.50-windows-x86

2、cas-server-3.4.11

3、cas-client-3.2.1

将cas-server-webapp-3.4.11.war放入tomcat的webapps下,改名ROOT.war,启动tomcat,待自动解压后,进行如下修改:

1、修改WEB-INF\deployerConfigContext.xml,加入

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. p:requireSecure="false"  
[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <property name="authenticationHandlers">  
  2.             <list>  
  3.                 <!--  
  4.                     | This is the authentication handler that authenticates services by means of callback via SSL, thereby validating  
  5.                     | a server side SSL certificate.  
  6.                     +-->  
  7.                 <bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"  
  8.                     p:httpClient-ref="httpClient" p:requireSecure="false"/>  
  9.                 <!--  
  10.                     | This is the authentication handler declaration that every CAS deployer will need to change before deploying CAS   
  11.                     | into production.  The default SimpleTestUsernamePasswordAuthenticationHandler authenticates UsernamePasswordCredentials  
  12.                     | where the username equals the password.  You will need to replace this with an AuthenticationHandler that implements your  
  13.                     | local authentication strategy.  You might accomplish this by coding a new such handler and declaring  
  14.                     | edu.someschool.its.cas.MySpecialHandler here, or you might use one of the handlers provided in the adaptors modules.  
  15.                     +-->  
  16.                 <bean  
  17.                     class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" />  
  18.             </list>  
  19.         </property>  
2、修改WEB-INF\spring-configuration\ticketGrantingTicketCookieGenerator.xml,修改p:cookieSecure="false"
[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"  
  2.     p:cookieSecure="false"  
  3.     p:cookieMaxAge="-1"  
  4.     p:cookieName="CASTGC"  
  5.     p:cookiePath="/cas" />  

3、修改修改WEB-INF\spring-configuration\warnCookieGenerator.xml,修改p:cookieSecure="false"
[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"  
  2.     p:cookieSecure="false"  
  3.     p:cookieMaxAge="-1"  
  4.     p:cookieName="CASPRIVACY"  
  5.     p:cookiePath="/cas" />  

经过以上三步,cas server端修改完毕

客户端操作我习惯进行一下域名/IP映射,修改:C:\Windows\System32\drivers\etc\hosts 添加如下映射

[java] view plain copy 在CODE上查看代码片派生到我的代码片
  1. 127.0.0.1 cas.jkkl1314.com  
  2. 127.0.0.1 c1.jkkl1314.com  
  3. 127.0.0.1 c2.jkkl1314.com  

在客户端项目中加入cas-client-core-3.2.1.jar、commons-logging.jar,并在web.xml中加入:

[html] view plain copy 在CODE上查看代码片派生到我的代码片
  1. <!-- ======================== 单点登录开始 ======================== -->  
  2.         <!-- 用于单点退出,该过滤器用于实现单点登出功能,可选配置-->  
  3.         <listener>  
  4.             <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>  
  5.         </listener>  
  6.   
  7.         <!-- 该过滤器用于实现单点登出功能,可选配置。 -->  
  8.         <filter>  
  9.             <filter-name>CAS Single Sign Out Filter</filter-name>  
  10.             <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>  
  11.         </filter>  
  12.         <filter-mapping>  
  13.             <filter-name>CAS Single Sign Out Filter</filter-name>  
  14.             <url-pattern>/*</url-pattern>  
  15.         </filter-mapping>  
  16.   
  17.         <filter>  
  18.             <filter-name>CAS Filter</filter-name>  
  19.             <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>  
  20.             <init-param>  
  21.                 <param-name>casServerLoginUrl</param-name>  
  22.                 <param-value>http://cas.jkkl1314.com:10000</param-value>  
  23.             </init-param>  
  24.             <init-param>  
  25.                 <param-name>serverName</param-name>  
  26.                 <param-value>http://c1.jkkl1314.com:8080</param-value>  
  27.             </init-param>  
  28.         </filter>  
  29.         <filter-mapping>  
  30.             <filter-name>CAS Filter</filter-name>  
  31.             <url-pattern>/*</url-pattern>  
  32.         </filter-mapping>  
  33.         <!-- 该过滤器负责对Ticket的校验工作,必须启用它 -->  
  34.         <filter>  
  35.             <filter-name>CAS Validation Filter</filter-name>  
  36.             <filter-class>  
  37.                 org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>  
  38.             <init-param>  
  39.                 <param-name>casServerUrlPrefix</param-name>  
  40.                 <param-value>http://cas.jkkl1314.com:10000</param-value>  
  41.             </init-param>  
  42.             <init-param>  
  43.                 <param-name>serverName</param-name>  
  44.                 <param-value>http://c1.jkkl1314.com:8080</param-value>  
  45.             </init-param>  
  46.         </filter>  
  47.         <filter-mapping>  
  48.             <filter-name>CAS Validation Filter</filter-name>  
  49.             <url-pattern>/*</url-pattern>  
  50.         </filter-mapping>  
  51.   
  52.         <!--  
  53.             该过滤器负责实现HttpServletRequest请求的包裹,  
  54.             比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。  
  55.         -->  
  56.         <filter>  
  57.             <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
  58.             <filter-class>  
  59.                 org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>  
  60.         </filter>  
  61.         <filter-mapping>  
  62.             <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
  63.             <url-pattern>/*</url-pattern>  
  64.         </filter-mapping>  
  65.   
  66.     <!--  
  67.         该过滤器使得开发者可以通过org.jasig.cas.client.util.AssertionHolder来获取用户的登录名。  
  68.         比如AssertionHolder.getAssertion().getPrincipal().getName()。  
  69.         -->  
  70.         <filter>  
  71.             <filter-name>CAS Assertion Thread Local Filter</filter-name>  
  72.             <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>  
  73.         </filter>  
  74.         <filter-mapping>  
  75.             <filter-name>CAS Assertion Thread Local Filter</filter-name>  
  76.             <url-pattern>/*</url-pattern>  
  77.         </filter-mapping>  
  78.   
  79.         <!-- ======================== 单点登录结束 ======================== -->  

第二个客户端项目只是修改了一下域名,在web.xml中加入的配置是一样的!运行后即可实现单点登录!

以下两边文章对我帮助很大,特此感谢:

http://www.micmiu.com/enterprise-app/sso/sso-cas-sample/

http://blog.csdn.net/designlife/article/details/2956814
0 0
原创粉丝点击