
單點登錄( Single Sign-On , 簡稱 SSO )是目前比較流行的服務於企業業務整合的解決方案之一, SSO 使得在多個應用系統中,用戶只需要 登錄一次 就可以訪問所有相互信任的應用系統。
1.2. 什麼是CAS?隨着SSO技術的流行,相關產品也比較多,其中CAS就是一套解決方案,CAS(Central Authentication Service)中文翻譯為統一身份認證服務或中央身份服務,它由服務端和客戶端組成,實現SSO,並且容易進行企業應用的集成。
CAS是Yale大學(耶魯)發起的一個開源項目,旨在為web應用系統提供一種可靠的單點登錄方法,CAS在2004年12月正式成為JA-SIG的一個項目。
官網:https://www.apereo.org/projects/cas

CAS具有以下的特點:
有了CAS,我們的系統架構就演變成下面這樣的:

從架構上可以看出,CAS包含兩個部分:CAS Server和CAS Client.
下面,我們一步步搭建CAS實現SSO.
1.3. 開發環境要求Jdk1.8+ maven3.6 idea tomcat9.0+ windows10
2. CAS Server服務器端2.1. CAS服務器端軟件包下載下載服務器的overlay地址: https://github.com/apereo/cas-overlay-template/tree/5.3
壓縮包:cas-overlay-template-5.3.zip
解壓好後用命令:build.cmd package
然後用編譯的目錄查看war包:

將war包放到tomcat的webapp中,然後啟動tomcat
訪問地址:http://localhost:8080/cas 或者 http://localhost:8080/cas/login

默認用戶名和密碼在\webapps\cas\WEB-INF\classes\application.properties裡面 用戶名:casuser 密碼:Mellon

CAS默認使用的是HTTPS協議,如果使用HTTPS協議需要SSL安全證書(需向特定的機構申請和購買)。如果對安全要求不高或是在開發測試階段,可使用HTTP協議。我們這裡講解通過修改配置,讓CAS使用HTTP協議。
微信搜索公眾號:Java後端編程,回覆:java 領取資料 。
修改CAS服務端配置文件:
\cas\WEB-INF\classes\application.properties里添加如下內容:
cas.tgc.secure=falsecas.serviceRegistry.initFromJson=true\cas\WEB-INF\classes\services目錄下的HTTPSandIMAPS-10000001.json修改內容如下:
"serviceId":"^(https|http|imaps)://.*"3. CAS Client客戶端配置(自己項目)Pom文件的依賴即pom.xml
<dependency><groupId>net.unicon.cas</groupId><artifactId>cas-client-autoconfig-support</artifactId><version>2.1.0-GA</version></dependency>application.yml配置文件
微信搜索公眾號:架構師指南,回覆:架構師 領取資料 。
客戶端1
server:port:9010cas:server-url-prefix:http://localhost:8080/casserver-login-url:http://localhost:8080/cas/loginclient-host-url:http://localhost:9010validation-type:cas3註:啟動類追加開啟CAS的註解@EnableCasClient
項目中新建一個測試類
iimportio.swagger.annotations.Api;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;@RestController@Api(description="SSO-CAS的測試")publicclassTestController{@GetMapping("/test1")publicStringtest1(){return"test1....";}}客戶端2
server:port:9011cas:server-url-prefix:http://localhost:8080/casserver-login-url:http://localhost:8080/cas/loginclient-host-url:http://localhost:9011validation-type:cas3註:啟動類追加開啟CAS的註解@EnableCasClient
項目中新建一個測試類
importio.swagger.annotations.Api;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;@RestController@Api(description="SSO-CAS的測試")publicclassTestController{@GetMapping("/test2")publicStringtest1(){return"test2....";}}客戶端1,客戶端2和cas服務器搭建好之後,接下來我們進行測試:
1.首先啟動tomcat服務器中的CAS Server。
2.分別啟動客戶端1和客戶端2,然後在瀏覽器地址欄輸入客戶端1的地址http://localhost:9010/test1

在不登錄的狀態,在瀏覽器的地址欄繼續輸入客戶端2的地址:http://localhost:9011/test2

當我們在其中一個登錄界面登錄賬號後(假設登錄客戶端2)就會跳轉到登陸後的界面,如下圖:

我們再次在瀏覽器窗口重新輸入客戶端1,http://localhost:9010/test1,或者在剛剛輸入客戶端頁面重新刷新,不用登錄即可進入頁面,如下圖:

以上就是單點登錄的測試。

點分享

點收藏

點點讚

點在看