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.10: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 了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM