cas實現單點登陸、登出(java和PHP客戶端)

近項目中須要作單點登陸,客戶端包含java和php,java有幾個應用程序,php是discuz+supesite+ucenter,需
 
要這幾個客戶端都要能單點登陸和登出,在網上找了許多相關資料,今天終於配置成功,步驟以下:
 
一、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驗證可參考:
 
 
1.一、若不採用https驗證,服務器端須要配置
一、cas\WEB-INF\deployerConfigContext.xml
<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
p:httpClient-ref="httpClient"/>
 
 增長參數p:requireSecure="false",是否須要安全驗證,即HTTPS,false爲不採用,加上去以後以下:
 
<bean class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
 p:httpClient-ref="httpClient"  p:requireSecure="false"/>
 
二、cas\WEB-INF\spring-configuration\
ticketGrantingTicketCookieGenerator.xml
<bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
      p:cookieSecure="true"
      p:cookieMaxAge="-1"
      p:cookieName="CASTGC"
      p:cookiePath="/cas" />
 
參數p:cookieSecure="true",同理爲HTTPS驗證相關,TRUE爲採用HTTPS驗證,FALSE爲不採用https驗證。
參數p:cookieMaxAge="-1",簡單說是COOKIE的最大生命 週期,-1爲無生命週期,即只在當前打開的IE窗口有效,IE關閉或從新打開其它窗口,仍會要求驗證。能夠根據須要修改成大於0的數字,好比3600等, 意思是在3600秒內,打開任意IE窗口,都不須要驗證。
 
1.二、服務器端退出訪問:http://localhost:8080/cas/logout,
 
 

若但願退出後能返回則須要配置
服務端cas-servlet.xml配置
<bean id="logoutController" class="org.jasig.cas.web.LogoutController" ... .../>
增長屬性 p:followServiceRedirects="true"
 
 
1.三、更改服務器端驗證方式,採用數據庫驗證:
修改配置文件deployerConfigContext.xml,加dbcp鏈接池:(以oracle爲例)
 
<bean id="casDataSource" class="org.apache.commons.dbcp.BasicDataSource">
     <property name="driverClassName">
          <value>oracle.jdbc.driver.OracleDriver</value>
     </property>
     <property name="url">
          <value>jdbc:oracle:thin:@192.168.18.26:1521:orcl</value>
     </property>
     <property name="username">
          <value>test</value>
     </property>
     <property name="password">
          <value>test</value>
     </property>
   </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接口便可:
   <bean id="passwordEncoder" 
    class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">     
    <constructor-arg value="MD5"/> 
   </bean>
 
註釋掉默認的驗證方式,採用數據庫查詢驗證:
<property name="authenticationHandlers">
     <list>
     <!----註釋掉這裏的默認驗證方式,採用如下驗證QueryDatabaseAuthenticationHandler-->
    <!--
    <bean
     class="org.jasig.cas.authentication.handler.support.SimpleTestUsernamePasswordAuthenticationHandler" /> -->
 
     <bean class="org.jasig.cas.adaptors.jdbc.QueryDatabaseAuthenticationHandler">
      <property name="dataSource" ref="casDataSource" />
      <property name="sql"
         value="select password from userinfo where lower(username) = lower(?)" />
      <property  name="passwordEncoder"  ref="passwordEncoder"/>
     </bean>
   </list>
  </property>
 
---------------到這裏cas服務端的配置就完成了。
 
 
二、java客戶端配置,下載客戶端: http://downloads.jasig.org/cas-clients/ ,目前最新版本爲:cas-client-3.2.0
 
將modules下的jar複製到java客戶端Casclient1的lib下,在web.xml中配置過濾器,配置以下(詳情見附件):
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
 http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
 
 
 <!-- 用於單點退出,該過濾器用於實現單點登出功能,通知其餘應用單點登出-->
 <listener>
         <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
 </listener>
 <!-- 該過濾器用於實現單點登出功能,可選配置。 -->
 <filter>
         <filter-name>CAS Single Sign Out Filter</filter-name>
         <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
 </filter>
 <filter-mapping>
         <filter-name>CAS Single Sign Out Filter</filter-name>
         <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <!-- 該過濾器負責用戶的認證工做,必須啓用它 -->
 <filter>
         <filter-name>CASFilter</filter-name>
         <filter-class>org.jasig.cas.client.authentication.AuthenticationFilter</filter-class>
         <init-param>
                 <param-name>casServerLoginUrl</param-name>
                 <param-value>http://192.168.18.8:8080/cas/login</param-value>
                 <!--這裏的server是服務端的IP-->
         </init-param>
         <init-param>
                 <param-name>serverName</param-name>
                 <param-value>http://192.168.18.8:8989</param-value>
         </init-param>
 </filter>
 <filter-mapping>
         <filter-name>CASFilter</filter-name>
         <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <!-- 該過濾器負責對Ticket的校驗工做,必須啓用它 -->
 <filter>
         <filter-name>CAS Validation Filter</filter-name>
         <filter-class>
                 org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter</filter-class>
         <init-param>
                 <param-name>casServerUrlPrefix</param-name>
                 <param-value>http://192.168.18.8:8080/cas</param-value>
         </init-param>
         <init-param>
                 <param-name>serverName</param-name>
                 <param-value>http://192.168.18.8:8989</param-value>
         </init-param>
 </filter>
 <filter-mapping>
         <filter-name>CAS Validation Filter</filter-name>
         <url-pattern>/*</url-pattern>
 </filter-mapping>
 
 <!--
         該過濾器負責實現HttpServletRequest請求的包裹,
         好比容許開發者經過HttpServletRequest的getRemoteUser()方法得到SSO登陸用戶的登陸名,可選配置。
 -->
 <filter>
         <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
         <filter-class>
                 org.jasig.cas.client.util.HttpServletRequestWrapperFilter</filter-class>
 </filter>
 <filter-mapping>
         <filter-name>CAS HttpServletRequest Wrapper Filter</filter-name>
         <url-pattern>/*</url-pattern>
 </filter-mapping>
<filter>
        <filter-name>CAS Assertion Thread Local Filter</filter-name>
        <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
</filter>
<filter-mapping>
        <filter-name>CAS Assertion Thread Local Filter</filter-name>
        <url-pattern>/*</url-pattern>
</filter-mapping>
 
 
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

頁面爲:
<%
AttributePrincipal principal = (AttributePrincipal)request.getUserPrincipal();   
String username = principal.getName();
%>
<br/>----------------------------------------------------------<br/>
<h1>登陸成功,這是客戶端1啊</h1><br/>
用戶名:<%=username %><br/>
<a href=" http://localhost:8989/Casclient2/index.jsp">進入客戶端2</a><br/>
<a href=" http://localhost:8080/cas/logout?service=http://localhost:8989/Casclient1/index.jsp">退出</a><br/>
 
-----------到這裏java客戶端配置成功,發佈到tomcat,複製Casclient1更名爲Casclient2,啓動tomcat,
 
訪問Casclient1,跳轉到登陸頁面,登陸成功後成功轉向登陸成功頁面,這時訪問Casclient2發現不須要登陸即顯示登陸成功頁面,java單點登陸成功。
 
 
三、配置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
 
複製到工程中,修改以下:
<?php
//
// phpCAS simple client
//
// import phpCAS lib
include_once('CAS.php');
phpCAS::setDebug();
// initialize phpCAS
phpCAS::client(CAS_VERSION_2_0,'192.168.18.8',8080,'cas');
// no SSL validation for the CAS server
phpCAS::setNoCasServerValidation();
// force CAS authentication
phpCAS::forceAuthentication();
// at this step, the user has been authenticated by the CAS server
// and the user's login name can be read with phpCAS::getUser().
// logout if desired
if (isset($_REQUEST['logout'])) {
 
 $param=array("service"=>"http://localhost/Phpcasclient1/example_simple.php");//退出登陸後返回
 phpCAS::logout($param);

}
// for this test, simply print that the authentication was successfull
?>
<html>
  <head>
    <title>phpCAS simple client</title>
  </head>
  <body>
    <h1>Successfull Authentication!這是客戶端1</h1>
    <p>the user's login is <b><?php echo phpCAS::getUser(); ?></b>.</p>
    <p>phpCAS version is <b><?php echo phpCAS::getVersion(); ?></b>.</p>
   
  <p><a href="
http://192.168.18.8:8989/Casclient1/index.jsp ">去java客戶端1</a></p>
    
 <p><a href="?logout=">退出</a></p>
  </body>
</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://www.walkerjava.com期待您的加入
相關文章
相關標籤/搜索