CAS单点登录

来源:互联网 发布:qq群关系数据库 种子 编辑:程序博客网 时间:2024/06/06 20:52

原文:http://blog.csdn.net/fei1502816/article/details/6695117

----------------------

最近项目中需要做单点登录,客户端包含java和php,java有几个应用程序,php是discuz+supesite+ucenter,需

 

要这几个客户端都要能单点登录和登出,在网上找了许多相关资料,今天终于配置成功,步骤如下:

 

1、cas服务端:下载地址:http://downloads.jasig.org/cas/cas的服务端和客户端有许多版本,最新版本和老版本

 

有很大的区别,目前服务端最新版本为:cas-server-3.4.4-release.zip

 

解压cas-server-3.4.4-release.zip将modules目录下的cas-server-webapp-3.4.4.war改名称为cas.war复制到

 

tomcat的webapps下,启动tomcat,访问:http://localhost:8080/cas/login 就可以看到登录界面了:

(如下:图一)

cas服务端默认采用的是 用户名=密码的验证,并且采用的是https验证,需要给tomact配置证书,本系统没有采用https验证,若采用https验证可参考:

 

http://blog.csdn.net/haydenwang8287/archive/2010/07/26/5765941.aspx

 

1.1、若不采用https验证,服务器端需要配置

  1)、

  1. cas\WEB-INF\deployerConfigContext.xml  
  2.   
  3. <bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"  
  4. p:httpClient-ref="httpClient"/>  


 增加参数p:requireSecure="false",是否需要安全验证,即HTTPS,false为不采用,加上去之后如下:

  1. <bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"  
  2.  p:httpClient-ref="httpClient"  p:requireSecure="false"/>  


 2)、

  1. cas\WEB-INF\spring-configuration\  
  2.   
  3. ticketGrantingTicketCookieGenerator.xml  
  4.   
  5. <bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"  
  6.   
  7.       p:cookieSecure="true"  
  8.   
  9.       p:cookieMaxAge="-1"  
  10.   
  11.       p:cookieName="CASTGC"  
  12.   
  13.       p:cookiePath="/cas" />  


 

参数p:cookieSecure="true",同理为HTTPS验证相关,TRUE为采用HTTPS验证,FALSE为不采用https验证。

参数p:cookieMaxAge="-1",简单说是COOKIE的最大生命周期,-1为无生命周期,即只在当前打开的IE窗口有效,IE关闭或重新打开其它窗口,仍会要求验证。可以根据需要修改为大于0的数字,比如3600等,意思是在3600秒内,打开任意IE窗口,都不需要验证。

1.2、服务器端退出访问:http://localhost:8080/cas/logout,

(如下图二)

若希望退出后能返回则需要配置

服务端cas-servlet.xml配置

<bean id="logoutController" class="org.jasig.cas.web.LogoutController" ... .../>

增加属性 p:followServiceRedirects="true"

 

退出链接为:http://localhost:8080/cas/logout?service=http://localhost:8080/Casclient/index.jsp

1.3、更改服务器端验证方式,采用数据库验证:

修改配置文件deployerConfigContext.xml,加dbcp连接池:(以oracle为例)

  1. <bean id="casDataSource" class="org.apache.commons.dbcp.BasicDataSource">  
  2.      <property name="driverClassName">  
  3.           <value>oracle.jdbc.driver.OracleDriver</value>  
  4.      </property>  
  5.      <property name="url">  
  6.           <value>jdbc:oracle:thin:@192.168.18.26:1521:orcl</value>  
  7.      </property>  
  8.      <property name="username">  
  9.           <value>test</value>  
  10.      </property>  
  11.      <property name="password">  
  12.           <value>test</value>  
  13.      </property>  
  14.    </bean>  

需要的jar包有:(见附件:cas-server-support-jdbc-3.4.4.jar,commons-dbcp-1.2.1.jar,commons-pool-1.3.jar,ojdbc14_g.jar)

 

配置加密方式,cas内置的有MD5加密,也可以写自己的加密类,实现org.jasig.cas.authentication.handler.PasswordEncoder接口即可:

  1. <bean id="passwordEncoder"    
  2.     class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">        
  3.     <constructor-arg value="MD5"/>    
  4.    </bean>   

注释掉默认的验证方式,采用数据库查询验证:

  1. <property name="authenticationHandlers">  
  2.      <list>  
  3.      <!----注释掉这里的默认验证方式,采用以下验证QueryDatabaseAuthenticationHandler-->  
  4.     <!--  
  5.     <bean  
  6.      class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" /> -->  
  7.   
  8.    
  9.   
  10.      <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">  
  11.       <property name="dataSource" ref="casDataSource" />  
  12.       <property name="sql"   
  13.          value="select password from userinfo where lower(username) = lower(?)" />  
  14.       <property  name="passwordEncoder"  ref="passwordEncoder"/>  
  15.      </bean>  
  16.    </list>  
  17.   </property>  

---------------到这里cas服务端的配置就完成了。

2、java客户端配置,下载客户端:http://downloads.jasig.org/cas-clients/,目前最新版本为:cas-client-3.2.0

 

将modules下的jar复制到java客户端Casclient1的lib下,在web.xml中配置过滤器,配置如下(详情见附件):

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.4"   
  3.  xmlns="http://java.sun.com/xml/ns/j2ee"   
  4.  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   
  6.  http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">  
  7.    
  8.    
  9.  <!-- 用于单点退出,该过滤器用于实现单点登出功能,通知其他应用单点登出-->  
  10.   
  11.  <listener>  
  12.          <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>  
  13.  </listener>  
  14.   
  15.  <!-- 该过滤器用于实现单点登出功能,可选配置。 -->  
  16.   
  17.  <filter>  
  18.          <filter-name>CAS Single Sign Out Filter</filter-name>  
  19.          <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>  
  20.  </filter>  
  21.  <filter-mapping>  
  22.          <filter-name>CAS Single Sign Out Filter</filter-name>  
  23.          <url-pattern>/*</url-pattern>  
  24.  </filter-mapping>  
  25.   
  26.    
  27.  <!-- 该过滤器负责用户的认证工作,必须启用它 -->  
  28.  <filter>  
  29.          <filter-name>CASFilter</filter-name>  
  30.          <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>  
  31.          <init-param>  
  32.                  <param-name>casServerLoginUrl</param-name>  
  33.                  <param-value>http://192.168.18.8:8080/cas/login</param-value>  
  34.                  <!--这里的server是服务端的IP-->  
  35.          </init-param>  
  36.          <init-param>  
  37.                  <param-name>serverName</param-name>  
  38.                  <param-value>http://192.168.18.8:8989</param-value>  
  39.          </init-param>  
  40.  </filter>  
  41.  <filter-mapping>  
  42.          <filter-name>CASFilter</filter-name>  
  43.          <url-pattern>/*</url-pattern>  
  44.  </filter-mapping>  
  45.    
  46.  <!-- 该过滤器负责对Ticket的校验工作,必须启用它 -->  
  47.  <filter>  
  48.          <filter-name>CAS Validation Filter</filter-name>  
  49.          <filter-class>  
  50.                  org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>  
  51.          <init-param>  
  52.                  <param-name>casServerUrlPrefix</param-name>  
  53.                  <param-value>http://192.168.18.8:8080/cas</param-value>  
  54.          </init-param>  
  55.          <init-param>  
  56.                  <param-name>serverName</param-name>  
  57.                  <param-value>http://192.168.18.8:8989</param-value>  
  58.          </init-param>  
  59.  </filter>  
  60.  <filter-mapping>  
  61.          <filter-name>CAS Validation Filter</filter-name>  
  62.          <url-pattern>/*</url-pattern>  
  63.  </filter-mapping>  
  64.    
  65.  <!--  
  66.          该过滤器负责实现HttpServletRequest请求的包裹,  
  67.          比如允许开发者通过HttpServletRequest的getRemoteUser()方法获得SSO登录用户的登录名,可选配置。  
  68.  -->  
  69.  <filter>  
  70.          <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
  71.          <filter-class>  
  72.                  org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>  
  73.  </filter>  
  74.  <filter-mapping>  
  75.          <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>  
  76.          <url-pattern>/*</url-pattern>  
  77.  </filter-mapping>  
  78.   
  79. <filter>  
  80.         <filter-name>CAS Assertion Thread Local Filter</filter-name>  
  81.         <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>  
  82. </filter>  
  83. <filter-mapping>  
  84.         <filter-name>CAS Assertion Thread Local Filter</filter-name>  
  85.         <url-pattern>/*</url-pattern>  
  86. </filter-mapping>  
  87.    
  88.    
  89.   <welcome-file-list>  
  90.     <welcome-file>index.jsp</welcome-file>  
  91.   </welcome-file-list>  
  92. </web-app>  


 

页面为:

  1. <%  
  2. AttributePrincipal principal = (AttributePrincipal)request.getUserPrincipal();      
  3. String username = principal.getName();   
  4. %>  
  5. <br/>----------------------------------------------------------<br/>  
  6. <h1>登录成功,这是客户端1啊</h1><br/>  
  7. 用户名:<%=username %><br/>  
  8. <a href="http://localhost:8989/Casclient2/index.jsp">进入客户端2</a><br/>  
  9.   
  10. <a href="http://localhost:8080/cas/logout?service=http://localhost:8989/Casclient1/index.jsp">退出</a><br/>  


 

-----------到这里java客户端配置成功,发布到tomcat,复制Casclient1改名为Casclient2,启动tomcat,

 

访问Casclient1,跳转到登录页面,登录成功后成功转向登录成功页面,这时访问Casclient2发现不需要登录即显示登录成功页面,java单点登录成功。

 

 

3、配置php客户端,下载php客户端:http://downloads.jasig.org/cas-clients/php/ ,目前最新版本为:CAS-1.2.0RC2

 

新建php工程:Phpcasclient1,将CAS文件夹和CAS.php复制到工程中,修改CAS/client.php,将其中的https改为http,将docs/examples/example_simple.php

 

复制到工程中,修改如下:

  1. <?php  
  2. //  
  3. // phpCAS simple client  
  4. //  
  5. // import phpCAS lib  
  6. include_once('CAS.php');  
  7. phpCAS::setDebug();  
  8. // initialize phpCAS  
  9. phpCAS::client(CAS_VERSION_2_0,'192.168.18.8',8080,'cas');  
  10. // no SSL validation for the CAS server  
  11. phpCAS::setNoCasServerValidation();  
  12. // force CAS authentication  
  13. phpCAS::forceAuthentication();  
  14. // at this step, the user has been authenticated by the CAS server  
  15. // and the user's login name can be read with phpCAS::getUser().  
  16. // logout if desired  
  17. if (isset($_REQUEST['logout'])) {  
  18.    
  19.  $param=array("service"=>"http://localhost/Phpcasclient1/example_simple.php");//退出登录后返回  
  20.  phpCAS::logout($param);  
  21.   
  22. }  
  23. // for this test, simply print that the authentication was successfull  
  24. ?>  

  1. <html>  
  2.   <head>  
  3.     <title>phpCAS simple client</title>  
  4.   </head>  
  5.   <body>  
  6.     <h1>Successfull Authentication!这是客户端1</h1>  
  7.     <p>the user's login is <b><?php echo phpCAS::getUser(); ?></b>.</p>  
  8.     <p>phpCAS version is <b><?php echo phpCAS::getVersion(); ?></b>.</p>  
  9.      <p><a href="http://192.168.18.8:8989/Casclient1/index.jsp">去java客户端1</a></p>  
  10.      <p><a href="?logout=">退出</a></p>  
  11.   </body>  
  12. </html>  

php配置需要开启php_curl,可以复制Phpcasclient1为Phpcasclient2

 

访问:http://localhost/Phpcasclient1/example_simple.php,跳转到登录页面,登录成功后访问Phpcasclient2,不需要登录,

 

php单点登录成功,这时再访问java客户端发现也不需要登录,php和java应用之间单点登录成功。

 

注:php的phpCAS::client(CAS_VERSION_2_0,'192.168.18.8',8080,'cas');地址需要和java的web.xml中的cas服务器地址一致,我开始一个写的ip:192.168.18.8,一个写的localhost,

php和java总是不能同步登录,郁闷了好久

 

----------------到这里java和php的客户端已经配置完成,现在你会发现php和java之间不能单点登出,php端退出java客户端也退出,反之java退出但是php却没有同步退出

 

这里需要做一个配置,在

phpCAS::setNoCasServerValidation();

// force CAS authentication
phpCAS::forceAuthentication();

这里加上

 

phpCAS::setNoCasServerValidation();

// force CAS authentication

phpCAS::handleLogoutRequests();  这里会检测服务器端java退出的通知,就能实现php和java间同步登出了。

phpCAS::forceAuthentication();


至于discuz+supesite的单点登录,再了解了php单点登录的原理后就需要改造discuz+supesite的登录代码了,discuz的为logging.php

 

supersite的为batch.login.php,俺是做java开发的,对php不是很熟悉,所以改造的觉得不是很靠谱,大致是先让discuz单点登录,获取用户名,根据用户

 

获取数据库中的密码再交给discuz系统自己的登录系统登录。discuz是采用cookie验证的,所以在java端退出后,discuz不会退出。

 

若谁有改造很成功的可以交流下。

 

参考网址:

http://blog.csdn.net/DL88250/archive/2008/08/20/2799522.aspx

http://www.wsria.com/archives/1349

http://tonrenyuye.blog.163.com/blog/static/30012576200922925820471/

http://www.discuz.net/thread-1416206-1-1.html


0 0
原创粉丝点击