1. 程式人生 > 其它 >CAS 入門實戰(2)--服務端安裝

CAS 入門實戰(2)--服務端安裝

本文主要介紹 CAS 服務端的安裝,使用到的軟體版本:JDK 1.8.0_191、Tomcat 8.5.76、CAS 5.3.16。

1、單機安裝

1.1、生成部署包

通過官方提供的 cas-overlay-template(https://github.com/apereo/cas-overlay-template/tree/5.3) 模板來生成部署包。

先 clone 專案到本地:

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

執行打包命令:

build.cmd package

命令執行完成之後會在 target 目錄下生成 cas 的 web 應用及 cas.war 的包。linux 環境可以使用 build.sh 指令碼。

1.2、生成金鑰庫

CAS 預設需要使用 Https 來訪問,可使用 Java 的 keytool 工具來生成金鑰庫,然後使用該金鑰庫在 Tomcat 中配置 SSL。

D:\tmp>keytool -genkeypair -alias cas-tomcat -keyalg RSA -keystore casServer.keystore

1.3、Tomcat 中配置 SSL

在 conf/server.xml 中註釋掉原來的 Connector,新增一個 Connector:

<!--Connector port="8080" protocol="HTTP/1.1"
           connectionTimeout="20000"
           redirectPort="8443" /
--> <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol" maxThreads="150" SSLEnabled="true"> <SSLHostConfig> <Certificate certificateKeystoreFile="d:/tmp/casServer.keystore" certificateKeystoreType="JKS" certificateKeystorePassword
="123456" /> </SSLHostConfig> </Connector>

1.4、修改日誌目錄

修改 WEB-INF\classes\log4j2.xml 檔案中的日誌檔案儲存目錄:

<Properties>
    <Property name="baseDir">d:/tmp/logs</Property>
</Properties>

1.5、部署應用

把 1.1 生成的 cas 應用目錄或 cas.war 拷貝到 Tomcat 的 webapps 目錄下並啟動 Tomcat;啟動完成後訪問應用:https://127.0.0.1:8443/cas,預設使用者名稱密碼為:casuser/Mellon

1.6、檢視 Dashboard

目前情況下是沒有許可權檢視 Dashboard 的,需要在 WEB-INF\classes\application.properties 中放開許可權:

#修改如下配置,開啟監控端點
cas.monitor.endpoints.enabled=true
cas.monitor.endpoints.sensitive=false

#新增如下配置,設定能訪問的ip,.+ 表示任意ip
cas.adminPagesSecurity.ip=127.0.0.1

修改完後重啟 Tomcat,重新登入:

1.7、JDBC 認證登入

預設情況下使用者資訊配置在 WEB-INF\classes\application.properties 中:

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

CAS 支援通過 JDBC 方式認證登入,以滿足實際生成的需要。

1.7.1、引入 JDBC 的元件

在 clone 專案的 pom.xml 檔案中增加如下配置(紅字部分):

<dependencies>
    <dependency>
        <groupId>org.apereo.cas</groupId>
        <artifactId>cas-server-webapp${app.server}</artifactId>
        <version>${cas.version}</version>
        <type>war</type>
        <scope>runtime</scope>
    </dependency>
    <!--
    ...Additional dependencies may be placed here...
    -->
    <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>
    <dependency>
        <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.28</version>
    </dependency>
</dependencies>

重新打包 build.cmd package,再把應用重新部署到 Tomcat 中。

1.7.2、建立使用者表

個應用系統一般都有使用者表,這一步是不需要的;這裡為了演示建立一個測試的使用者表:

CREATE TABLE `cas_user`  (
  `id` bigint PRIMARY KEY,
  `user_name` varchar(32) NOT NULL COMMENT '使用者名稱',
  `password` varchar(64) NOT NULL COMMENT '密碼',
  `create_time` datetime COMMENT '建立時間',
  `expired_flag` int(1) NOT NULL DEFAULT 0 COMMENT '是否過期',
  `disabled_flag` int(1) NOT NULL DEFAULT 0 COMMENT '是否有效'
)

並插入測試資料,密碼使用 MD5 加密,這裡密碼為 123456,MD5 加密後用十六進位制表示為:e10adc3949ba59abbe56e057f20f883e

insert into cas_user(id,user_name,password,create_time)
values (1,'test','e10adc3949ba59abbe56e057f20f883e',now());

1.7.3、應用中配置資料庫資訊

在 WEB-INF\classes\application.properties 中修改配置:

#該行註釋掉
#cas.authn.accept.users=casuser::Mellon

#增加下列配置
#查詢使用者資訊的SQL,會把使用者名稱作為引數傳進來
cas.authn.jdbc.query[0].sql=select * from cas_user where user_name=?
#指定密碼欄位
cas.authn.jdbc.query[0].fieldPassword=password
#指定過期欄位
cas.authn.jdbc.query[0].fieldExpired=expired_flag
#指定是否可用欄位
cas.authn.jdbc.query[0].fieldDisabled=disabled_flag

cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
cas.authn.jdbc.query[0].driverClass=com.mysql.cj.jdbc.Driver
cas.authn.jdbc.query[0].url=jdbc:mysql://10.49.196.10:3306/cas?useUnicode=true&characterEncoding=UTF-8&useSSL=false
cas.authn.jdbc.query[0].user=root
cas.authn.jdbc.query[0].password=123456

#預設加密策略,NONE 不加密
cas.authn.jdbc.query[0].passwordEncoder.type=DEFAULT
cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8
cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5

重啟 Tomcat,使用 test/123456 登入 CAS 系統。

1.8、使用 Http 方式登入

由於 Https 需要配置證書,比較麻煩,也不方便 CAS 客戶端和 CAS 服務端的通訊,可以修改為使用 Http 訪問系統。

1.8.1、修改 HTTPSandIMAPS-10000001.json 檔案

修改 WEB-INF\classes\services\HTTPSandIMAPS-10000001.json,增加 http 協議:

{
  "@class" : "org.apereo.cas.services.RegexRegisteredService",
  "serviceId" : "^(https|http|imaps)://.*",
  "name" : "HTTPS and IMAPS",
  "id" : 10000001,
  "description" : "This service definition authorizes all application urls that support HTTPS and IMAPS protocols.",
  "evaluationOrder" : 10000
}

1.8.2、修改 application.properties 檔案

修改 WEB-INF\classes\application.properties 檔案,增加如下配置:

cas.tgc.secure=false
cas.warningCookie.secure=false
cas.serviceRegistry.initFromJson=true

1.8.3、Tomcat 改回 Http 協議

刪除原來 Https 的配置,復原原始配置:

<Connector port="8080" protocol="HTTP/1.1"
     connectionTimeout="20000"
     redirectPort="8443" />

重啟 Tomcat,通過 Http 協議訪問 CAS:

2、叢集安裝

2.1、叢集架構

官方推薦的叢集架構如下:

2.2、叢集規劃

ip 用途
10.49.196.10 CAS 服務端
10.49.196.11 CAS 服務端
10.49.196.12 nginx,redis

這裡 redis 使用單機版,實際應用中可以使用 redis 的哨兵模式,防止單點故障。

2.3、票據持久化

預設票據儲存在記憶體中,叢集中個節點無法共享;CAS 提供多種票據持久化的方法,如:JMS、JPA、MongoDB、Redis、Cassandra 等等,這裡使用 Redis 來持久票據。

2.3.1、引入依賴

在 cas-overlay-template 的 pom.xml 檔案中引入相關依賴:

<profile>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <id>default</id>
    <dependencies>
        <dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-webapp${app.server}</artifactId>
            <version>${cas.version}</version>
            <type>war</type>
            <scope>runtime</scope>
        </dependency>
        ......
        <dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-support-redis-ticket-registry</artifactId>
            <version>${cas.version}</version>
        </dependency>
    </dependencies>
</profile>

重新打包 build.cmd package,再把應用重新部署到 Tomcat 中。

2.3.2、配置 Redis 資訊

在 WEB-INF\classes\application.properties 中增加配置:

cas.ticket.registry.redis.host=10.49.196.12
cas.ticket.registry.redis.database=0
cas.ticket.registry.redis.port=6379
cas.ticket.registry.redis.usePool=true

詳細說明可參考官方文件:https://apereo.github.io/cas/6.5.x/ticketing/Redis-Ticket-Registry.html (5.3 版本的文件已經沒有了,這裡使用 6.5 版本的文件);配置完後,重啟應用即可。

2.4、Session 持久化(可選)

Session 持久化用於 CAS 例項之間共享會話狀態和會話故障轉移;這一步是可選的,不建議使用,因為使用者 CAS 會話往往是短期的,並且體驗更像是請求-響應風格,而不是面向會話的。

2.4.1、引入依賴

 在 cas-overlay-template 的 pom.xml 檔案中引入相關依賴:

<profile>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <id>default</id>
    <dependencies>
        <dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-webapp${app.server}</artifactId>
            <version>${cas.version}</version>
            <type>war</type>
            <scope>runtime</scope>
        </dependency>
        ......
        <dependency>
             <groupId>org.apereo.cas</groupId>
             <artifactId>cas-server-webapp-session-redis</artifactId>
             <version>${cas.version}</version>
        </dependency>
    </dependencies>
</profile>

重新打包 build.cmd package,再把應用重新部署到 Tomcat 中。

2.4.2、配置 Session 持久資訊

在 WEB-INF\classes\application.properties 中增加配置:

cas.webflow.session.storage=true
spring.session.store-type=redis
spring.redis.host=10.49.196.12
spring.redis.port=6379

詳細說明可參考官方文件:https://apereo.github.io/cas/6.5.x/webflow/Webflow-Customization-Sessions.html (5.3 版本的文件已經沒有了,這裡使用 6.5 版本的文件);配置完後,重啟應用即可。

2.5、代理配置

這裡使用 Nginx 作為代理伺服器,配置如下:

http {
    ......

    upstream cas {
        server 10.49.196.10:8080 weight=1;
        server 10.49.196.11:8081 weight=1;
        ip_hash;
    }
    server {
        listen       8080;
        server_name  localhost;
        ......

        location /cas {
            proxy_pass http://cas/cas;
        }
    }
}

啟動 Nginx 後,就可以通過 http://10.49.196.12:8080/cas 來訪問 CAS 了。