統一認證 - Apereo CAS 小試
做這些嘗試的時候,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 104908911560 Jan 25 14:25 LICENSE.txt -rw-r--r-- 1 NOTECH 10490892768 Jan 25 14:28 README.md -rw-r--r-- 1 NOTECH 10490894353 Jan 25 14:28 build.cmd -rwxr-xr-x 1 NOTECH 10490895608 Jan 25 14:28 build.sh drwxr-xr-x 1 NOTECH 10490890 Jan 25 14:25 etc drwxr-xr-x 1 NOTECH 10490890 Jan 25 14:28 maven -rwxr-xr-x 1 NOTECH 10490897332 Jan 25 14:28 mvnw -rw-r--r-- 1 NOTECH 10490895839 Jan 25 14:28 mvnw.bat -rw-r--r-- 1 NOTECH 10490899458 Jan 28 10:15 pom.xml drwxr-xr-x 1 NOTECH 10490890 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
驗證資訊儲存
好了,到這時可能發現:天啦嚕!用哪個使用者可以登入啊? 從頭到尾都沒有使用者資訊的配置,而按照我們所瞭解的Apereo CAS的尿性,它可不會有什麼預設值的。為簡單試玩一下,我們可以直接在上面的application.properties檔案裡面,直接hardcode一個使用者在裡面,如下:
cas.authn.accept.users=casuser::Mellon
重新 build package
, build run
,開啟登入頁面 https://localhost :6443/cas, 輸入使用者名稱casuser,密碼Mellon,應該就可以登入成功了
把使用者資訊hardcode在配置檔案顯然是很helloworld的做法,CAS提供了很多使用者資訊儲存方式,有各種DB,LDAP等。具體可以參考官網的配置檔案,有很詳細的說明 https://apereo.github.io/cas/...
這次我們採用的是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所有的使用者,只要登入成功後都可以訪問