一、Server端搭建
1.template下載
下載CAS 模板 Overlay Template,我這里使用 Apereo CAS 5.3.x 版本,JDK需要1.8+
地址:https://github.com/apereo/cas-overlay-template/tree/5.3
解壓下載的zip壓縮包
2.keystore配置
如果不對keystore進行配置,項目啟動將拋出找不到證書文件的異常,無法正常啟動。
打開項目目錄build.cmd (Linux 為build.sh),可以看到有如下代碼
:gencert
where /q keytool
if ERRORLEVEL 1 (
@echo Java keytool.exe not found in path.
exit /b 1
) else (
if not exist %CAS_DIR% mkdir %CAS_DIR%
@echo on
@echo Generating self-signed SSL cert for %DNAME% in %CAS_DIR%\thekeystore
keytool -genkeypair -alias cas -keyalg RSA -keypass changeit -storepass changeit -keystore %CAS_DIR%\thekeystore -dname %DNAME% -ext SAN=%CERT_SUBJ_ALT_NAMES%
@echo Exporting cert for use in trust store (used by cas clients)
keytool -exportcert -alias cas -storepass changeit -keystore %CAS_DIR%\thekeystore -file %CAS_DIR%\cas.cer
)
@goto:eof
可以看到,在template的build工具中已經為我們提供了生成keystore的腳本,就不需要再按照大量網上教程去命令行手動進行創建。
證書信息可在腳本頂部 DNAME 和 CERT_SUBJ_ALT_NAMES 兩個變量中進行定義:
@if "%DNAME%" == "" set DNAME=CN=cas.example.org,OU=Example,OU=Org,C=US
@if "%CERT_SUBJ_ALT_NAMES%" == "" set CERT_SUBJ_ALT_NAMES=dns:example.org,dns:localhost,dns:%COMPUTERNAME%,ip:127.0.0.1
在項目目錄執行代碼:
build.cmd gencert
就生成了證書文件。
需要注意的是:
(1) 由于配置的項目路徑CAS_DIR是 \etc\cas,在Windows環境下,會將證書文件生成到當前盤符目錄的對應文件夾下。如項目文件在F盤某個目錄,那么文件會生成到F:\etc\cas目錄下。
(2) 生成證書的時候,會出現warning:JKS 密鑰庫使用專用格式。建議使用 "keytool -importkeystore -srckeystore \etc\cas\thekeystore -destkeystore \etc\cas\thekeystore -deststoretype pkcs12" 遷移到行業標準格式 PKCS12。因為僅做演示,可以不進行此操作。
3.其他配置
修改cas-overlay-template-5.3\etc\cas\config目錄下的cas.properties配置文件。
4.項目運行
項目運行通常采取 build.cmd run 或者 build.cmd bootrun 命令。
這里,我們采用調試命令
build.cmd debug
將默認開啟5000調試端口,方便遠程調試。
命令執行后,可以看到項目目錄下生產了build及target文件夾。
target\cas\WEB-INF\classes目錄下:
- services 文件夾存放了以正則方式注冊的json service(RegexRegisteredService)
- application.properties 文件包含項目大量配置,包含默認的用戶名密碼信息
項目啟動后,訪問 https://localhost:8443/cas/login
默認用戶登錄:
casuser
Mellon