【SSO單點系列】(1):CAS4.0 環境的搭建



一、概述

   今天開始寫CAS相關的第一篇文章,這篇文章主要是關於CAS環境的搭配,提供給剛剛接觸CAS的一個入門指南,並演示一個CAS的最簡單的實例

 

二、環境要求

博主的環境如下:

  1. win8.1 64 bitJDK1.7  下載地址點我Tomcat-8.0.15  下載地址點我cas-server-4.0.0 、cas-client-3.3.3  下載地址點我  (官網速度比較慢,提供百度網盤)

tomcat服務器需要部署三個,我分別命名為 apache-tomcat-8.0.15-app1、apache-tomcat-8.0.15-app2、apache-tomcat-8.0.15-cas

分別的用途如下:
序號
服務器名稱
用途
1
tomcat-app1
客戶端服務器1:用戶部署應用app1
2
tomcat-app2
客戶端服務器2:用戶部署應用app2
3
tomcat-cas
cas服務器:用來部署CAS server

 

 三、特別說明

  CAS 默認認證方式使用的是HTTPS協議,一般對安全性不高的話建議取消改成HTTP方式。因為,開啟的話會經常提示證書過期、需要用戶確認等,對客戶的感知不好,當前有需要的可以開啟。

 如果需要HTTPS協議的話,有關證書的生成可以參考這篇文章 : CAS單點登錄證書導入 

 取消HTTPS協議的方法,第四點會具體介紹,大家可以接着往下看!

 

四、實例講解

第一步、Tomcat修改

  1. 解壓下載的 Tomcat-8.0.15.zip,並按照第二點的約定復制三個並命名

修改tomcat的相關啟動等端口,使機器可以運行多個tomcat。我的訪問端口對應如下:

序號 服務器名稱 訪問端口
1 tomcat-app1 8081
2 tomcat-app2 8082
3 tomcat-cas 18080

端口的修改方法:打開 x:\tomcat-app1\conf\server.xml 文件, 找到

復制代碼
 
           
第一個:修改Shutdown端口(默認為8005端口)
<
Server port="8005" shutdown="SHUTDOWN">
第二個: 修改http訪問端口(默認為8080端口)
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

第三個:修改8009端口
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />

      修改成自己需要的端口即可。

        

第二步、CAS-SERVER 服務端的部署

    1. 解壓下載的 cas-server-4.0.0-release.zip 壓縮包找到 X:\cas-server-4.0.0\modules\cas-server-webapp-4.0.0.war 文件解壓到 tomcat-cas\webapps\ 下取消HTTPS協議:

       1)打開 cas-server\WEB-INF\deployerConfigContext.xml 文件 ,找到如下配置:

 

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

 

     2)打開 cas-server\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" 為 p:cookieSecure="false"
即不開啟https驗證

 

    3) 打開 cas-server\WEB-INF\spring-configuration\warnCookieGenerator.xml ,找到如下配置:

 

<bean id="warnCookieGenerator" class="org.jasig.cas.web.support.CookieRetrievingCookieGenerator"
        p:cookieSecure="true"
        p:cookieMaxAge="-1"
        p:cookieName="CASPRIVACY"
        p:cookiePath="/cas" />
修改  p:cookieSecure="true" 為 p:cookieSecure="false"
即不開啟https驗證

 

 

 

5.啟動 tomcat-cas ,訪問  http://localhost:18080/cas-server ,可以看到如下界面

  

      

  注意:cas-server4.0之前的默認驗證規則:只要用戶名和密碼相同就認證通過

  4.0 之后規則改了,默認是配置在 deployerConfigContext.xml 配置文件中,可以看到用戶名密碼為 casuser/Mellon。

      

<bean id="primaryAuthenticationHandler"
          class="org.jasig.cas.authentication.AcceptUsersAuthenticationHandler">
        <property name="users">
            <map>
                <entry key="casuser" value="Mellon"/>
            </map>
        </property>
    </bean>

 

 

 第三步、客戶端(cas-client)的配置

注:我們直接用tomcat自帶的examples工程作為客戶端例子

  1. 解壓我們下載的cas-client-3.3.3-release.zip 包,復制cas-client-3.3.3\modules\cas-client-core-3.3.3.jar 包放到 tomcat-app1\webapps\examples\WEB-INF\lib 下(兩個client tomcat都需要放,這邊只列出其中一個)修改examples\WEB-INF\web.xml 文件,增加下面的內容:

 

 

<!-- ========================單點登錄開始 ======================== -->
    <!--用於單點退出,該過濾器用於實現單點登出功能,可選配置 -->
    <listener>
        <listener-class>org.jasig.cas.client.session.SingleSignOutHttpSessionListener</listener-class>
    </listener>
    <!--該過濾器用於實現單點登出功能,可選配置。 -->
    <filter>
        <filter-name>CASSingle Sign OutFilter</filter-name>
        <filter-class>org.jasig.cas.client.session.SingleSignOutFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CASSingle Sign OutFilter</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://localhost:18080/cas-server/login</param-value>
        </init-param>
        <init-param>
            <param-name>serverName</param-name>
            <param-value>http://localhost:8081</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CASFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--該過濾器負責對Ticket的校驗工作,必須啟用它 -->
    <filter>
        <filter-name>CASValidationFilter</filter-name>
        <filter-class>
            org.jasig.cas.client.validation.Cas20ProxyReceivingTicketValidationFilter
        </filter-class>
        <init-param>
            <param-name>casServerUrlPrefix</param-name>
            <param-value>http://localhost:18080/cas-server</param-value>
        </init-param>
        <init-param>
            <param-name>serverName</param-name>
            <param-value>http://localhost:8081</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CASValidationFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 該過濾器負責實現HttpServletRequest請求的包裹, 比如允許開發者通過HttpServletRequest的getRemoteUser()方法獲得SSO登錄用戶的登錄名,可選配置。 -->
    <filter>
        <filter-name>CASHttpServletRequest WrapperFilter</filter-name>
        <filter-class>
            org.jasig.cas.client.util.HttpServletRequestWrapperFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CASHttpServletRequest WrapperFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 該過濾器使得開發者可以通過org.jasig.cas.client.util.AssertionHolder來獲取用戶的登錄名。 比如AssertionHolder.getAssertion().getPrincipal().getName()。 -->
    <filter>
        <filter-name>CASAssertion Thread LocalFilter</filter-name>
        <filter-class>org.jasig.cas.client.util.AssertionThreadLocalFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CASAssertion Thread LocalFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- ========================單點登錄結束 ======================== -->

 

 4.啟動 tomcat-app1 ,然后訪問 http://localhost:8081/examples ,頁面會跳到

http://localhost:18080/cas-server/login?service=http%3A%2F%2Flocalhost%3A8081%2Fexamples%2F

說明單點成功

 

 

同樣的  tomcat-app2 我這邊就不演示了,

我把修改完的兩個web.xml 上傳上來 

tomcat-app1 : web.xml

tomcat-app2 : web.xml

 

 第四步、 單點流程演示

我們首先依次把三個tomcat都啟動后 ,我們先單獨訪問兩個客戶端看看效果

1. 訪問 http://localhost:8081/examples  ==》 跳到 http://localhost:18080/cas-server/login?service=http%3A%2F%2Flocalhost%3A8081%2Fexamples%2F

2. 訪問 http://localhost:8082/examples  ==》 跳到  http://localhost:18080/cas-server/login?service=http%3A%2F%2Flocalhost%3A8082%2Fexamples%2F

說明兩個客戶端第一次訪問時都需要跳轉到cas-server進行認證

 

接下來:我們登錄其中一個客戶端 http://localhost:8081/examples ,賬號密碼  casuser/Mellon

登錄成功后 顯示下面的界面

 

然后我們在打開一個新選項卡 ,直接訪問 http://localhost:8082/examples

可以看到 不會跳轉到cas-server登錄界面 直接顯示下面的界面

 

說明 兩個客戶端單點登錄成功,登錄了其中一個,另一個不需要登陸即可進行訪問。

 

 

總結、

  單點登錄(Single Sign On , 簡稱 SSO )是目前比較流行的服務於企業業務整合的解決方案之一, SSO 使得在多個應用系統中,用戶只需要登錄一次就可以訪問所有相互信任的應用系統。 

上文只是介紹了最簡單的單點登錄例子,在實際開發中是不能使用的。如果需要應用到項目中,還需要許多個性化的訂制,比如登陸頁的美化、通過數據庫認證、服務端與客戶端用戶信息的交互等。這些都會在后面的文章中介紹到。

 

好了,第一篇大概就這樣了, 沒有什么代碼,就不上傳源碼了,大家湊合的看吧!

下一篇會介紹 【登錄頁的個性化定制】。

 

打完收工。。。

!

!

 

posted @ 2014-12-17 23:20 _輝 閱讀(2022) 評論(13) 編輯 收藏


免責聲明!

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



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