CAS單點登陸原理簡介及環境搭建

前言

最近這幾天在研究CAS 今天終於在本地部署成功了 今天寫一篇文章記錄下

原理簡介

SSO單點登錄

在多個相互信任的系統中,用戶只需要登錄一次就可以訪問其他受信任的系統。

新浪微博與新浪博客是相互信任的應用系統。

  • 當用戶首次訪問新浪微博時,新浪微博識別到用戶未登錄,將請求重定向到認證中心,認證中心也識別到用戶未登錄,則將請求重定向到登錄頁。

  • 當用戶已登錄新浪微博訪問新浪博客時,新浪博客識別到用戶未登錄,將請求重定向到認證中心,認證中心識別到用戶已登錄,返回用戶的身份,此時用戶無需登錄即可使用新浪博客。

  • 只要多個系統使用同一套單點登錄框架那麼它們將是相互信任的。

CAS包含CAS Client 和 CAS Server兩部分

  • CAS Client:要使用單點登錄的Web應用,將與同組下的Web應用構成相互信任的關係,只需在web應用中添加CAS提供的Listener和Filter即可成爲CAS Client ,其主要負責對客戶端的請求進行登錄校驗、重定向和校驗ticket工作。

  • CAS Server:主要負責對用戶的用戶名/密碼進行認證,頒發票據等,需要單獨的進行部署。同組下的任意一個Web應用登錄後其他應用都不需要登錄即可使用。

搭建CAS服務端

環境準備

jdk1.8、tomcat9、maven3+、idea

下載springboot5.x版本 cas-server代碼

git clone https://gitee.com/pingfanrenbiji/cas-overlay-template.git -b 5.2

導入idea中 目錄結構爲

tomcat啓動

訪問

現在需要解決頁面上的2個報紅

個就是說你沒用HTTPS登錄,另一個就是你現在只有一個寫死的用戶,目前這個服務端只能看看,沒什麼實際用途。

https配置

  • 生成服務器端的keysore文件

keytool -genkey -alias cas -keyalg RSA -keysize 2048 -keypass 123456 -storepass 123456 -keystore /Users/mengfanxiao/Documents/project/person/springboot/shiro-cas/cas/keystore/liuyx.keystore -dname "CN=cas.example.org,OU=liuyx.com,O=liuyx,L=JiNan,ST=JiNan,C=CN"
  • 配置域名

sudo vim /etc/hosts
127.0.0.1  cas.example.org
  • 配置tomcat

conf/server.xml

注視掉8080端口訪問

<Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="150" SSLEnabled="true">
        <SSLHostConfig>
           <Certificate certificateKeystoreFile="/Users/mengfanxiao/Documents/project/person/springboot/shiro-cas/cas/keystore/liuyx.keystore" type="RSA" certificateKeystoreType="JKS" certificateKeystorePassword="123456"/>
        </SSLHostConfig>
</Connector>

登陸用戶從數據庫中讀取

數據庫文件及賬號數據

再次訪問

https://cas.example.org:8443/cas_overlay_war/login

搭建CAS客戶端

下載官方demo client

https://gitee.com/pingfanrenbiji/cas-sample-java-webapp.git

目錄結構

地址配置

生成客戶端訪問服務端的證書

我是在本地電腦進行測試的 所以客戶端和服務器端都是在同一太電腦上
  • 通過服務端證書生成客戶端證書

keytool -exportcert -alias cas -keystore /Users/mengfanxiao/Documents/project/person/springboot/shiro-cas/cas/keystore/liuyx.keystore -file /Users/mengfanxiao/Documents/project/person/springboot/shiro-cas/cas/keystore/liuyx.keystore.cer -storepass 123456
  • 將客戶端證書導入客戶端所在的服務器上的jdk環境中

sudo keytool -import -alias cas -keystore /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/security/cacerts -file /Users/mengfanxiao/Documents/project/person/springboot/shiro-cas/cas/keystore/liuyx.keystore.cer

默認密碼:changeit
  • 查看證書文件

keytool -list -keystore /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/security/cacerts
  • 刪除證書文件

keytool -delete -alias cas -keystore  /Library/Java/JavaVirtualMachines/jdk1.8.0_221.jdk/Contents/Home/jre/lib/security/cacerts

tomcat啓動

訪問

https://localhost:8444/cas_sample_java_webapp_war_exploded/
  • 第一次訪問

輸入用戶名和密碼之後

  • 再一次訪問

直接略過授權頁面

配置http方式訪問

  • 配置參數

  • 訪問

http://localhost:8081/cas_sample_java_webapp_war_exploded/

本文使用 mdnice 排版

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章