通过CAS Initializr获取Server
CAS Initializr类似与spring boot 提供的Spring Initializr,可以提供项目基础结构。定制化内容只需通过修改配置文件,添加依赖即可。步骤如下:
-
CAS提供了一个CAS Initializr地址(https://casinit.herokuapp.com/starter.tgz),但是这个地址访问很慢,基本很难下载成功。可以通过Docker在本地机器上运行一个自己的 CAS Initializr 实例:
docker run --rm -p 8080:8080 apereo/cas-initializr:0.0.31
-
请求CAS Initializr获取CAS Server项目
curl http://localhost:8080/starter.tgz -d type=cas-overlay -d baseDir=overlay -d casVersion=6.6.4 | tar -xzvf -
官方文档参考地址:CAS Initializr官方文档
启动项目
-
修改配置文件:
-
直接修改src/main/resources/application.yml
-
修改/etc/cas/config/application.yml,该文件配置会覆盖src/main/resources/application.yml
-
增加以下配置:
server:
servlet:
context-path: cas
port: 8001
-
打包, 进入overlay目录:
./gradlew clean build --parallel
-
启动项目
java -server -noverify -Xmx2048M -jar build/libs/cas.war
注:默认用户名密码为 casuser:Mellon
改为使用SSL
参考:CAS配置SSL,使用https访问, 修改配置后需重新打包并启动
使用mysql存储cas用户密码
-
创建表,并插入用户
CREATE TABLE `user` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='用户表';
INSERT INTO `user`
(id, username, password)
VALUES(1, 'aaa', md5('aaa'));
-
修改/etc/cas/config/application.yml配置
cas:
authn:
jdbc:
query[0]:
url: ******
user: ******
password: ******
driver-class: com.mysql.cj.jdbc.Driver
sql: select * from user where username = ?
field-password: password
password-encoder:
type: DEFAULT
character-encoding: UTF-8
encoding-algorithm: MD5
accept:
enabled: false
tgc:
secure: false
#cas.authn.accept 指定的就是默认登录用户casuser, 密码Mellon,enabled设为false,则默认用户便不能登录
#如果 cas.authn.jdbc.query[0].password-encoder.type设为NONE,则不适用密码加密,数据库中需存明文密码
-
增加依赖
implementation "org.apereo.cas:cas-server-support-jdbc"
implementation "org.apereo.cas:cas-server-support-jdbc-drivers"
implementation "org.apereo.cas:cas-server-support-jdbc-authentication"
#官方文档只提示引入org.apereo.cas:cas-server-support-jdbc,并未说明引入剩余2个包,实测不起作用。需同时引入3个jar包。原因在于配置的mysql版本问题,cas默认的jdbc驱动不支持。
参考: https://apereo.github.io/cas/6.6.x/installation/JDBC-Drivers.html
-
重新打包并启动,便可用aaa用户登录,密码aaa
修改服务加载配置
-
修改/etc/cas/config/application.yml,增加如下内容
cas:
service-registry:
core:
init-from-json: true
json:
location: file:/etc/cas/services
-
在目录/etc/cas/services下新建文件HTTPSandHTTP.json
{
"@class": "org.apereo.cas.services.CasRegisteredService",
"serviceId": "^(https|http)://.*",
"name": "HTTPS and HTTP",
"id": 10000001,
"description": "This service definition authorizes all application urls that support HTTPS and HTTP protocols.",
"evaluationOrder": 10000
}
#如果不做修改,默认只支持https与imap客户端,http客户端无法访问
-
重新打包并启动
配置SSO,单点登录
-
增加配置
cas.tgc.crypto.encryption.key=
cas.tgc.crypto.signing.key=
注:这两个key不需要自己生成。在CAS Server启动时,如果key没有配置,会自动生成,并在启动日志中输出,复制后使用即可。也可以自己通过工具生成
官方参考文档:SSO Sessions
-
CAS的单点登录通过tgc实现,tgc全名:ticket-granting cookie
-
官方说明:
-
A ticket-granting cookie SHALL be set to expire at the end of the client’s browser session if Long-Term support is not active (4.1.1) for the corresponding TGT.
-
CAS SHALL set the cookie path to be as restrictive as possible. For example, if the CAS server is set up under the path /cas, the cookie path SHALL be set to /cas.
-
The value of ticket-granting cookies SHALL contain adequate secure random data so that a ticket-granting cookie is not guessable in a reasonable period of time.
-
The name of ticket-granting cookies SHOULD begin with the characters TGC-.
-
The value of ticket-granting cookies SHOULD follow the same rules as the ticket-granting ticket. Typically, the value of the ticket-granting cookies MAY contain the ticket-granting ticket itself as the representation of the authenticated single sign-on session.
-
CAS Server默认设置tgc的path为server根路径,比如/cas,此时chrome浏览器是看不到的,需通过如下配置将tgc配置改为/
cas.tgc.path=/
-
官方参考文档:CAS Protocol Specification
-
即便如此,chrome浏览器依然无发实现单点登录,原因是chrome开启了cookie同源策略
-
cookie同源策略是指:除非当前域名和请求域名是同源,才会默认携带cookie
-
这导致即便tgc的path设置为/, 访问/cas/login时仍然无法携带该cookie,仍需重新登录
-
可以使用Firefox浏览器,则无需理会cookie同源策略(高版本未知,亲测110.0(64位)可以)
全量配置
到目前为止,全部配置信息如下:
# Application properties that need to be
# embedded within the web application can be included here
server:
servlet:
context-path: /cas
port: 8001
ssl:
enable: true
key-alias: cas
key-store: file:/cas/caskeystore
key-store-type: PKCS12
key-store-password: 111111
key-password: 111111
cas:
authn:
jdbc:
query[0]:
url: ******
user: ******
password: ******
driver-class: com.mysql.cj.jdbc.Driver
sql: select * from user where username = ?
field-password: password
password-encoder:
type: DEFAULT
character-encoding: UTF-8
encoding-algorithm: MD5
accept:
enabled: false
tgc:
secure: false
path: /
crypto:
enabled: true
encryption:
key: ******
signing:
key: ******
webflow:
crypto:
signing:
key: ******
encryption:
key: ******
sso:
sso-enabled: true
service-registry:
core:
init-from-json: true
json:
location: file:/etc/cas/services