CAS 單點登錄(代碼部分)


資料下載地址:https://download.csdn.net/download/u011072258/11964484

一、CAS簡介

CAS是一個單點登錄框架,開始是由耶魯大學的一個組織開發,后來歸到apereo管理。同時CAS也是開源,遵循着apache 2.0協議,代碼目前是在github上管理。

此次使用的CAS版本:cas-server: 4.0.0cas-client: 3.2.1

 

二、資料清單

1、casDemo.zip》:包含3Tomcat,其中apache-tomcat-7.0.75 - service用於部署cas 服務端程序,另外2Tomcat個模擬cas 客戶端程序。

2、hosts》: 將文件中的映射添加到C:\Windows\System32\drivers\etc 目錄hosts文件中,用於模擬跨域請求下的CAS單點登錄。

127.0.0.1  cas.server.com   

127.0.0.1  cas.client1.com

127.0.0.1  cas.client2.com

3、《cas.sql》: 新建mysql 數據庫 cas ,導入cas.sql生成user 表,用於服務端用戶名密碼驗證(adminadmin)。說明:密碼MD5加密。

 

4、cas-server-4.0.0.zip》:cas-service-4.0.0 的源碼,可以根據此工程做定制開發。

5、《cas-client 所需jar.zip》:包含cas客戶端所需jar包,共3個。

6、cas.war》:cas-server-4.0.0服務端生成的 war包,放在Tomcat中即可訪問(apache-tomcat-7.0.75 - service\webapps\cas.war)。

三、本地配置文件說明

  1. 服務端配置說明
  • deployerConfigContext.xmlapache-tomcat-7.0.75 - service\webapps\cas.war\WEB-INF)
  •  另外cas默認是HTTPS方式訪問,我們可以改為http訪問。需要修改三個文件四處地方的配置(針對4.0.0版本,其他版本的話第一處必改,其他的看看還有沒有帶有cookie的文件名),詳細配置修改如下:
  • (1)、WEB-INF/deployerConfigContext.xml

  • 第一處:增加參數 p:requireSecure=”false” ,是否需要安全驗證,即 HTTPS , false 為不采用,添加以后如下:
  • <!-- Required for proxy ticket mechanism. -->
    <bean id="proxyAuthenticationHandler"
    class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
    p:httpClient-ref="httpClient" p:requireSecure="false"/>

    該文件還有第二處需要修改,否則會出現來回從客戶端到服務端來回跳轉的問題,修改完成以后如下:

    <util:list id="registeredServicesList">
    <bean class="org.jasig.cas.services.RegexRegisteredService"
    p:id="0" p:name="HTTP and IMAP" p:description="Allows HTTP(S) and IMAP(S) protocols"
    p:serviceId="^(https?|imaps?|http?)://.*" p:evaluationOrder="10000001" p:enabled="true" p:allowedToProxy="true" p:ssoEnabled="true" />

    (2) WEB-INF/spring-configuration/ticketGrantingTicketCookieGenerator.xml
    第三處:修改 p:cookieSecure=”true” 為 p:cookieSecure=” false ” , 即不需要安全 cookie,如下部分:

    <bean id="ticketGrantingTicketCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
    p:cookieSecure="false"
    p:cookieMaxAge="-1"
    p:cookieName="CASTGC"
    p:cookiePath="/cas" />

    (3) WEB-INF\spring-configuration\warnCookieGenerator.xml
    第四處:修改 p:cookieSecure=”true” 為 p:cookieSecure=” false ” ,即不需要安全 cookie,結果如下:

    <bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
    p:cookieSecure="false"
    p:cookieMaxAge="-1"
    p:cookieName="CASPRIVACY"
    p:cookiePath="/cas" />

  1. 客戶端配置說明
  • 解壓《cas-client 所需jar.zip》,將解壓所得jar包放入客戶端工程(Tomcat 自帶的 examples工程)\WEB-INF\lib下。
  • 配置web.xmlapache-tomcat-7.0.75 - client1\webapps\examples\WEB-INF\web.xml

 

四、Demo演示

請確保已經創建mysql 數據庫cas,並導入cas.sql,已修改deployerConfigContext.xml文件中的數據庫配置,並配置hosts文件。

1、解壓casDemo.zip,進入apache-tomcat-7.0.75 - service\bin,點擊startup.bat啟動cas 服務端程序。端口:8080,訪問路徑:http://cas.server.com:8080/cas/login

   

2、再分別啟動apache-tomcat-7.0.75 - client1apache-tomcat-7.0.75 - client1,端口分別為80818082

訪問路徑:

http://cas.client1.com:8081/examples

http://cas.client2.com:8082/examples

3、分別訪問http://cas.client1.com:8081/exampleshttp://cas.client2.com:8082/examples,因為未登錄,都重定向到了cas-service的登錄頁面。

 4、在第一個窗口輸入用戶名、密碼(adminadmin),點擊登錄,跳轉到了cas.client1.com的首頁。

此時,直接刷新第二個頁面,因為cas.client1.com已經登錄,無需再次登錄,可直接跳轉到cas.client2.com 的首頁。

至此,cas單點登錄功能已經實現。

 

參考文章:https://blog.csdn.net/u011872945/article/details/81045608

                  http://www.pianshen.com/article/2882278229/


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM