教你實現 SSO 單點登錄
1. 概述
1.1. 什麼是 SSO?
單點登錄( 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 Server 爲需要獨立部署的 web 應用
-
CAS Client 支持非常多的客戶端(這裏指單點登錄系統中的各個 web 應用),包括 Java、.Net 、ISAPI、Php、Perl、uPortal、Acegi、Ruby、VBScript 等客戶端
-
另外,搜索公衆號後端架構師後臺回覆 “商城”,獲取一份驚喜禮包。
有了 CAS, 我們的系統架構就演變成下面這樣的:
從架構上可以看出,CAS 包含兩個部分:CAS Server 和 CAS Client.
-
CAS Server 需要獨立部署,主要負責對用戶的認證工作,CAS Client 負責處理
-
對客戶端受保護資源的訪問請求,需要登錄,重定向到 CAS Server。
下面,我們一步步搭建 CAS 實現 SSO.
1.3. 開發環境要求
Jdk1.8+ maven3.6 idea tomcat9.0+ windows10
2. CAS Server 服務器端
2.1. CAS 服務器端軟件包下載
- 下載版本爲 5.3
下載服務器的 overlay 地址: https://github.com/apereo/cas-overlay-template/tree/5.3
壓縮包:cas-overlay-template-5.3.zip
解壓好後用命令:build.cmd package
然後用編譯的目錄查看 war 包:
2.2. 服務器端的基本部署和測試
將 war 包放到 tomcat 的 webapp 中,然後啓動 tomcat
訪問地址:http://localhost:8080/cas
或者 http://localhost:8080/cas/login
默認用戶名和密碼在\webapps\cas\WEB-INF\classes\application.properties
裏面 用戶名:casuser 密碼:Mellon
2.3. CAS Server 服務器配置
2.3.1 去除 https 認證
CAS 默認使用的是 HTTPS 協議,如果使用 HTTPS 協議需要 SSL 安全證書(需向特定的機構申請和購買)。如果對安全要求不高或是在開發測試階段,可使用 HTTP 協議。我們這裏講解通過修改配置,讓 CAS 使用 HTTP 協議。
修改 CAS 服務端配置文件:
\cas\WEB-INF\classes\application.properties
裏添加如下內容:
cas.tgc.secure=false
cas.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: 9010
cas:
server-url-prefix: http://localhost:8080/cas
server-login-url: http://localhost:8080/cas/login
client-host-url: http://localhost:9010
validation-type: cas3
注:啓動類追加開啓 CAS 的註解 @EnableCasClient
項目中新建一個測試類
iimport io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(description = "SSO-CAS的測試")
public class TestController {
@GetMapping("/test1")
public String test1(){
return "test1....";
}
}
客戶端 2
server:
port: 9011
cas:
server-url-prefix: http://localhost:8080/cas
server-login-url: http://localhost:8080/cas/login
client-host-url: http://localhost:9011
validation-type: cas3
注:啓動類追加開啓 CAS 的註解 @EnableCasClient
項目中新建一個測試類
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@Api(description = "SSO-CAS的測試")
public class TestController {
@GetMapping("/test2")
public String test1(){
return "test2....";
}
}
客戶端 1,客戶端 2 和 cas 服務器搭建好之後,接下來我們進行測試:
-
首先啓動 tomcat 服務器中的 CAS Server。另外,搜索公衆號頂級算法後臺回覆 “算法心得”,獲取一份驚喜禮包。
-
分別啓動客戶端 1 和客戶端 2,然後在瀏覽器地址欄輸入客戶端 1 的地址
http://localhost:9010/test1
在不登錄的狀態,在瀏覽器的地址欄繼續輸入客戶端 2 的地址:http://localhost:9011/test2
當我們在其中一個登錄界面登錄賬號後(假設登錄客戶端 2)就會跳轉到登陸後的界面,如下圖:
我們再次在瀏覽器窗口重新輸入客戶端 1,http://localhost:9010/test1
,或者在剛剛輸入客戶端頁面重新刷新, 不用登錄即可進入頁面,如下圖:
以上就是單點登錄的測試。
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/o2Gr_iJdCzL8p9ERTwMfuQ