1. SSO簡介
1.1 單點登錄定義
單點登錄(Single sign on),英文名稱縮寫SSO,SSO的意思就是在多系統的環境中,登錄單方系統,就可以在不用再次登錄的情況下訪問相關受信任的系統。也就是說只要登錄一次單體系統就可以。計划在項目中加入單點登錄,開發中,taoshop
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:參考學習教程
單點登錄實現方式:
單點登錄的三種實現方式
SpringBoot集成Cas
SpringBoot集成CAS
Spring Boot 集成Shiro和CAS
基於Spring Boot的單點登錄
springboot + shiro + cas4.2.7 實戰
CAS單點登錄教程:
Cas專題文章列表
測試認證方式搭建CAS
SSO之單點登錄詳細搭建教程
CAS實現SSO單點登錄原理
使用 CAS 在 Tomcat 中實現單點登錄