cas實現單點登入,登出(java跟php用戶端) (轉)

來源:互聯網
上載者:User
cas實現單點登入,登出(java和php用戶端) (轉)

最近項目中需要做單點登入,用戶端包含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、cas\WEB-INF\deployerConfigContext.xml

p:httpClient-ref="httpClient"/>

?

?增加參數p:requireSecure="false",是否需要安全驗證,即HTTPS,false為不採用,加上去之後如下:

?

?p:httpClient-ref="httpClient"? p:requireSecure="false"/>

?

2、cas\WEB-INF\spring-configuration\

ticketGrantingTicketCookieGenerator.xml

????? 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.2、伺服器端退出訪問:http://localhost:8080/cas/logout,

?

?

若希望退出後能返回則需要配置

服務端cas-servlet.xml配置

增加屬性 p:followServiceRedirects="true"

?

退出連結為:http://localhost:8080/cas/logout?service=http://localhost:8080/Casclient/index.jsp

?

1.3、更改伺服器端驗證方式,採用資料庫驗證:

修改設定檔deployerConfigContext.xml,加dbcp串連池:(以oracle為例)

?


????
????????? oracle.jdbc.driver.OracleDriver
????
????
????????? jdbc:oracle:thin:@192.168.18.26:1521:orcl
????
????
????????? test
????
????
????????? test
????
??

?

需要的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介面即可:

? ???? class="org.jasig.cas.authentication.handler.DefaultPasswordEncoder" autowire="byName">?????
??? ?
??

?

注釋掉預設的驗證方式,採用資料庫查詢驗證:


?????
?????
????

?

?????
?????
????? ???????? value="select password from userinfo where lower(username) = lower(?)" />
?????
?????
???
??

?

---------------到這裡cas服務端的配置就完成了。

?

?

2、java用戶端配置,下載用戶端:http://downloads.jasig.org/cas-clients/,目前最新版本為:cas-client-3.2.0

?

將modules下的jar複製到java用戶端Casclient1的lib下,在web.xml中配置過濾器,配置如下(詳情見附件):


?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">
?
?
?

?
???????? org.jasig.cas.client.session.SingleSignOutHttpSessionListener
?

?

?
???????? CAS Single Sign Out Filter
???????? org.jasig.cas.client.session.SingleSignOutFilter
?
?
???????? CAS Single Sign Out Filter
???????? /*
?

?
?
?
???????? CASFilter
???????? org.jasig.cas.client.authentication.AuthenticationFilter
????????
???????????????? casServerLoginUrl
???????????????? http://192.168.18.8:8080/cas/login
????????????????
????????
????????
???????????????? serverName
???????????????? http://192.168.18.8:8989
????????
?
?
???????? CASFilter
???????? /*
?
?
?
?
???????? CAS Validation Filter
????????
???????????????? org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter
????????
???????????????? casServerUrlPrefix
???????????????? http://192.168.18.8:8080/cas
????????
????????
???????????????? serverName
???????????????? http://192.168.18.8:8989
????????
?
?
???????? CAS Validation Filter
???????? /*
?
?
?
?
???????? CAS HttpServletRequest Wrapper Filter
????????
???????????????? org.jasig.cas.client.util.HttpServletRequestWrapperFilter
?
?
???????? CAS HttpServletRequest Wrapper Filter
???????? /*
?


??????? CAS Assertion Thread Local Filter
??????? org.jasig.cas.client.util.AssertionThreadLocalFilter


??????? CAS Assertion Thread Local Filter
??????? /*

?
?
?
??? index.jsp
?

?

頁面為:

<%
AttributePrincipal principal = (AttributePrincipal)request.getUserPrincipal();???
String username = principal.getName();
%>

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

登入成功,這是用戶端1啊



使用者名稱:<%=username %>

http://localhost:8989/Casclient2/index.jsp">進入用戶端2

http://localhost:8080/cas/logout?service=http://localhost:8989/Casclient1/index.jsp">退出

?

-----------到這裡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

?

複製到工程中,修改如下:

//
// 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
?>

?
??? phpCAS simple client
?
?
???

Successfull Authentication!這是用戶端1


???

the user's login is .


???

phpCAS version is .


?????

http://192.168.18.8:8989/Casclient1/index.jsp">去java用戶端1


?????

退出


?

?

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

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.