1. 程式人生 > >CAS單點登入原理簡單介紹

CAS單點登入原理簡單介紹

1. SSO簡介

1.1 單點登入定義

單點登入(Single sign on),英文名稱縮寫SSO,SSO的意思就是在多系統的環境中,登入單方系統,就可以在不用再次登入的情況下訪問相關受信任的系統。也就是說只要登入一次單體系統就可以。計劃在專案中加入單點登入,開發中,專案連結

1.2 單點登入角色

單點登入一般包括下面三種角色:

①使用者(多個);

②認證中心(一個);

③Web應用(多個)。

PS:這裡所說的web應用可以理解為SSO Client,認證中心可以說是SSO Server。

2. CAS簡介

2.1 CAS簡單定義

CAS(Center Authentication Service)是耶魯大學研究的一款開源的單點登入專案,主要為web專案提供單點登入實現,屬於Web SSO

2.2 CAS體系結構

CAS體系結構分為CAS Server和CAS Client。

這裡寫圖片描述 PS:圖來自官網

2.3 CAS原理

下面給出一張來自CAS官方的圖片 這裡寫圖片描述 CAS登入等系統分為CAS Server和CAS Client,下面,我根據我的理解稍微解釋一下:

1、使用者訪問CAS Client請求資源

2、客戶端程式做了重定向,重定向到CAS Server

3、CAS Server會對請求做認證

4、認證通過後會生成一個Ticket返回Cas Client

5、然後Cas Client就帶著Ticket再次訪問Cas Server,CAS Server進行Ticket驗證

6、CAS Server對Ticket進行再次驗證,然後通過就返回使用者資訊,使用者拿到資訊後就可以登入

看到這個過程,我們大概就能理解CAS是怎麼實現的,看起來過程挺多的,不過這些過程都是CAS在後臺做的

CAS單點登入

現在部落格簡單介紹一下,CAS Server簡單部署實現,CAS是一款開源框架,目前應用比較廣泛。下面簡單介紹一下: cas開源到github上,不過只有幾個版本有cas release服務端,其它大部分版本都只有source原始碼而已,所以其它版本都需要自己編譯,不想自己編譯的可以下載V4.0.0版本的。 https://github.com/apereo/cas/releases/tag/v4.0.0

下載cas server之後,我們就可以簡單部署一下,中介軟體可以用Tomcat cas的安全機制是依靠SSL實現的,所以一般的http非安全連結不支援的,雖然是這麼說,不過學習練習的話,也可以去掉https要求,下面介紹說一下:

可以先將cas-server-4.0.0-release.zip解壓到Tomcat的webapp目錄下面,然後需要修改一個配置檔案 (1)、先修改一下cas-server-4.0.0-release的WEB-INF下面的deployerConfigContext.xml 修改前:

<!-- Required for proxy ticket mechanism. -->
    <bean id="proxyAuthenticationHandler"
          class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"
          p:httpClient-ref="httpClient"  />

修改後,PS:加上p:requireSecure=“false”

<bean id="proxyAuthenticationHandler"  
          class="org.jasig.cas.authentication.handler.support.HttpBasedServiceCredentialsAuthenticationHandler"  
          p:httpClient-ref="httpClient" p:requireSecure="false"/></code>  

(2)、修改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" />

修改後,PS:改為p:cookieSecure=“false”

<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 修改前:

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

修改後,PS:改為p:cookieSecure=“false”


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

(4)、修改CAS預設登入jsp頁面 可以註釋WEB-INF\view\jsp\default\ui\casLoginView.jsp頁面如下程式碼

<c:if test="${not pageContext.request.secure}">
  <div id="msg" class="errors">
    <h2>Non-secure Connection</h2>
    <p>You are currently accessing CAS over a non-secure connection.  Single Sign On WILL NOT WORK.  In order to have single sign on work, you MUST log in over HTTPS.</p>
  </div>
</c:if>

去掉Https支援要求後,就可以通過http的連結登入cas server了,使用者名稱是casuser,密碼是Mellon PS:可以在deployerConfigContext.xml裡看到配置,正規專案是實現jdbc支援

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

登入成功 這裡寫圖片描述

待續…,PS:找時間繼續寫…

附錄

PS:參考學習教程