CAS 單點登陸(代碼部分)

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

1、CAS簡介git

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

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

 

2、資料清單spring

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

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

127.0.0.1  cas.server.com   apache

127.0.0.1  cas.client1.com跨域

127.0.0.1  cas.client2.comtomcat

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

 

四、cas-server-4.0.0.zip》:cas-service-4.0.0 的源碼,能夠根據此工程作定製開發。

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

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

3、本地配置文件說明

  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

 

4、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/

相關文章
相關標籤/搜索