統一認證 - Apereo CAS 小試

上一篇文章我們對Apereo CAS有了簡要的瞭解,這篇文章我們將動手練習Apereo CAS。主要是CAS單機版的搭設,用戶信息存儲到數據庫,以及dashboard的使用

做這些嘗試的時候,Apereo CAS比較穩定的版本是5.3.x,使用如果想按照這個文章搭設的話,最好採用相同的版本

Apereo CAS單機版的搭設

Apereo CAS秉承耶魯的自由文化傳統,整個產品高度自由化,哪哪都提供了極其靈活的使用方式。比如單機版的部署,一般的軟件提供的單機版都是下載一來,運行某個文件就直接開跑的。Apereo就不同,即使是單機版,也要配置一些內容纔可以運行的。

不單單是配置,單機版的代碼實現也是可以改的,而且還可以很優雅地改,就是可以在不修改原來代碼的前提下進行修改。Apereo CAS採用了Maven的overlayer 特性,提供了一份CAS的overlayer或者叫template,我們可以從下載一份layer ,然後在裏面按照約定的方式,實現功能覆蓋Apereo CAS提供的類,或者配置文件。

git clone https://github.com/apereo/cas-overlay-template

這是Apereo CAS官方提供的一個overlay,大家也可以下載使用其他組織提供的overlay。該項目的目錄結構如下:

C:\githome\github\cas\cas-server>ls -l
total 1220
-rw-r--r-- 1 NOTECH 1049089  11560 Jan 25 14:25 LICENSE.txt
-rw-r--r-- 1 NOTECH 1049089   2768 Jan 25 14:28 README.md
-rw-r--r-- 1 NOTECH 1049089   4353 Jan 25 14:28 build.cmd
-rwxr-xr-x 1 NOTECH 1049089   5608 Jan 25 14:28 build.sh
drwxr-xr-x 1 NOTECH 1049089      0 Jan 25 14:25 etc
drwxr-xr-x 1 NOTECH 1049089      0 Jan 25 14:28 maven
-rwxr-xr-x 1 NOTECH 1049089   7332 Jan 25 14:28 mvnw
-rw-r--r-- 1 NOTECH 1049089   5839 Jan 25 14:28 mvnw.bat
-rw-r--r-- 1 NOTECH 1049089   9458 Jan 28 10:15 pom.xml
drwxr-xr-x 1 NOTECH 1049089      0 Jan 25 14:31 src

其實就是一個簡單的maven項目,多了一個etc的目錄,然後pom文件裏面有一個cas-server-webapp的overlayer依賴。這時我們可以直接跑mvn package, 一樣會生成相應的cas包,只是這個包跑不起來,因爲cas需要一些配置才能起來的。

前面說了overlayer會按照目錄路徑進行覆蓋,也就是如果overlayer的項目裏面有文件路徑相同,那麼打包的時候就會進行覆蓋。而上一篇blog說了,Apereo CAS是基於springboot的開發的,那麼我們要覆蓋對應的配置文件,那就新建src\main\resources目錄。

安全證書

首先,Apereo CAS作爲一個安全的統一認證中心,那麼本身也要安全的吧。所有它提供了HTTPS的鏈接方式,也就意味着我們需要提供一個keystore。命令行打開目錄cas-server/src/main/resources/etc/cas,執行以下命令生成對應的keystore

keytool -genkey -keyalg RSA -alias thekeystore -keystore thekeystore 
-storepass changeit -validity 360 -keysize 2048

changeit 是這個keystore的密碼,最好改成你自己的密碼,當然,作爲demo用這個也是可以的

接着我們要把這個keystore導成證書給客戶端用:

keytool -export -alias thekeystore -file thekeystore.crt 
-keystore thekeystore

現在我們要把這個導出來的證書導進去JVM裏面

keytool -import -alias thekeystore -storepass changeit -file thekeystore.crt -keystore "C:\Program Files\Java\jdk1.8.0_101\jre\lib\security\cacerts"

keytool -import -alias thekeystore -storepass changeit -file thekeystore.crt -keystore "C:\Program Files\Java\jre1.8.0_101\lib\security\cacerts"

CAS配置

接着我們把證書的路徑,CAS啓動端口等信息配置到springboot標準的配置文件application.properties裏面。

server.context-path=/cas
server.port=6443

server.ssl.key-store=classpath:/etc/cas/thekeystore
server.ssl.key-store-password=changeit
server.ssl.key-password=changeit

好了,打包build package,然後build run跑一下看看。應該可以看到CAS的默認登錄頁面 https://localhost:6443/cas
image

驗證信息存儲

好了,到這時可能發現:天啦嚕!用哪個用戶可以登錄啊? 從頭到尾都沒有用戶信息的配置,而按照我們所瞭解的Apereo CAS的尿性,它可不會有什麼默認值的。爲簡單試玩一下,我們可以直接在上面的application.properties文件裏面,直接hardcode一個用戶在裏面,如下:

cas.authn.accept.users=casuser::Mellon

重新build package,build run,打開登錄頁面 https://localhost:6443/cas, 輸入用戶名casuser,密碼Mellon,應該就可以登錄成功了
image

把用戶信息hardcode在配置文件顯然是很helloworld的做法,CAS提供了很多用戶信息存儲方式,有各種DB,LDAP等。具體可以參考官網的配置文件,有很詳細的說明 https://apereo.github.io/cas/5.3.x/installation/Configuration-Properties.html#database-authentication

這次我們採用的是MYSQL的鏈接方式。首先我們要在overlayers的pom文件裏面添加JDBC的support,如下:

    <dependency>
                    <groupId>org.apereo.cas</groupId>
                    <artifactId>cas-server-support-jdbc</artifactId>
                    <version>${cas.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.apereo.cas</groupId>
                    <artifactId>cas-server-support-jdbc-drivers</artifactId>
                    <version>${cas.version}</version>
                </dependency>

然後還是在原來的application.properties文件裏面,把authentication的相關配置寫上:

cas.authn.jdbc.query[0].sql=select * from cms_auth_user where user_name=?
cas.authn.jdbc.query[0].healthQuery=
cas.authn.jdbc.query[0].isolateInternalQueries=false
cas.authn.jdbc.query[0].url=jdbc:mysql://127.0.0.1:3306/CASTEST?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
cas.authn.jdbc.query[0].failFast=true
cas.authn.jdbc.query[0].isolationLevelName=ISOLATION_READ_COMMITTED
cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
cas.authn.jdbc.query[0].leakThreshold=10
cas.authn.jdbc.query[0].propagationBehaviorName=PROPAGATION_REQUIRED
cas.authn.jdbc.query[0].batchSize=1
cas.authn.jdbc.query[0].user=root
#cas.authn.jdbc.query[0].ddlAuto=create-drop
cas.authn.jdbc.query[0].maxAgeDays=180
cas.authn.jdbc.query[0].password=123456
cas.authn.jdbc.query[0].autocommit=false
cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
cas.authn.jdbc.query[0].idleTimeout=5000

這裏只是列出了一些必要的配置,比如driver.class是什麼,query的語句是什麼,連接哪個數據庫等等。配置成功後,我們可以在數據庫裏面插入一兩個用戶試試

CAS監控中心

Apereo CAS提供了一個監控中心, 當大家登錄CAS成功後,滿懷希望地點擊dashboard的鏈接時,展示給大家的是冷冷的 "Access Denied"頁面! 無良啊無良,高度地自由是有代價的!!! CAS認爲大家默認是不需要這個功能的,所以默認是關閉的! 我們需要通過配置文件打開這個功能! 你以爲只要打開endpoints.enabled=true就可以了嗎? 我們把CAS想簡單的,這傢伙龜毛到每一個監控的功能都需要獨立打開的!所以就有了下面長長的一個配置文件!

endpoints.enabled=true
endpoints.sensitive=false

endpoints.restart.enabled=false
endpoints.shutdown.enabled=false

management.security.enabled=true
management.security.roles=ACTUATOR,ADMIN
management.security.sessions=if_required
management.context-path=/status
management.add-application-context-header=false

security.basic.authorize-mode=role
security.basic.enabled=false
security.basic.path=/cas/status/**

cas.adminPagesSecurity.ip=.+

cas.monitor.endpoints.dashboard.enabled=true 
cas.monitor.endpoints.dashboard.sensitive=false

cas.monitor.endpoints.discovery.enabled=true 
cas.monitor.endpoints.discovery.sensitive=false

cas.monitor.endpoints.auditEvents.enabled=true 
cas.monitor.endpoints.auditEvents.sensitive=false

cas.monitor.endpoints.authenticationEvents.enabled=true 
cas.monitor.endpoints.authenticationEvents.sensitive=false

cas.monitor.endpoints.configurationState.enabled=true 
cas.monitor.endpoints.configurationState.sensitive=false

cas.monitor.endpoints.healthCheck.enabled=true 
cas.monitor.endpoints.healthCheck.sensitive=false

cas.monitor.endpoints.loggingConfig.enabled=true 
cas.monitor.endpoints.loggingConfig.sensitive=false

cas.monitor.endpoints.metrics.enabled=true 
cas.monitor.endpoints.metrics.sensitive=false

cas.monitor.endpoints.attributeResolution.enabled=true 
cas.monitor.endpoints.attributeResolution.sensitive=false

cas.monitor.endpoints.singleSignOnReport.enabled=true 
cas.monitor.endpoints.singleSignOnReport.sensitive=false

cas.monitor.endpoints.statistics.enabled=true 
cas.monitor.endpoints.statistics.sensitive=false

cas.monitor.endpoints.trustedDevices.enabled=true 
cas.monitor.endpoints.trustedDevices.sensitive=false

cas.monitor.endpoints.status.enabled=true 
cas.monitor.endpoints.status.sensitive=false

cas.monitor.endpoints.singleSignOnStatus.enabled=true 
cas.monitor.endpoints.singleSignOnStatus.sensitive=false

cas.monitor.endpoints.springWebflowReport.enabled=true 
cas.monitor.endpoints.springWebflowReport.sensitive=false

cas.monitor.endpoints.registeredServicesReport.enabled=true 
cas.monitor.endpoints.registeredServicesReport.sensitive=false

cas.monitor.endpoints.configurationMetadata.enabled=true 
cas.monitor.endpoints.configurationMetadata.sensitive=false

這裏是實在受不了CAS的配置粒度細微到懷疑人生,所以adminSercurity沒有打開,放開給所有的IP所有的用戶,只要登錄成功後都可以訪問

在這裏插入圖片描述

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