一.需求描述
公司开发系统越来越多,每个系统都有自己的登录认证流程,给用户很繁琐的体验,统一认证单点登录迫在眉睫
二.流程图
三.本地运行cas服务端
1.拉取cas服务端代码,切换到对应分支(我使用的是6.4.x)
https://github.com/apereo/cas-overlay-template
2.由于6.4版本默认jdk11以上,所以在idea进行如下配置
3.在build.gradle文件加入mysql、jdbc依赖配置,并下载依赖
implementation "org.apereo.cas:cas-server-support-json-service-registry:${project.'cas.version'}"
implementation "org.apereo.cas:cas-server-support-jdbc:${project.'cas.version'}"
implementation "org.apereo.cas:cas-server-support-jdbc-drivers:${project.'cas.version'}"
implementation "mysql:mysql-connector-java:5.1.46"
4.yml配置数据源以及http请求支持
cas:
authn:
jdbc:
query[0]:
sql: SELECT * FROM user WHERE username = ?
url: jdbc:mysql://xxxxxx:3306/cas?useUnicode=true&characterEncoding=utf8&useSSL=false
user: root
password: xxxx
fieldPassword: password
driverClass: com.mysql.jdbc.Driver
password-encoder:
type: DEFAULT
encoding-algorithm: MD5
character-encoding: UTF-8
fieldExpired: expired #是否提示改密码的字段
fieldDisabled: disabled #是否禁用用户的字段
accept:
enabled: false
tgc:
secure: false
service-registry:
core:
init-from-json: true
json:
location: file:/etc/cas/services
#cas.authn.accept 指定的就是默认登录用户casuser, 密码Mellon,enabled设为false,则默认用户便不能登录
#如果 cas.authn.jdbc.query[0].password-encoder.type设为NONE,则不适用密码加密,数据库中需存明文密码
5.在jdk的bin目录生成证书thekeystore
keytool -genkeypair(keytool -genkeypair)来生成对应的keystore
6.在项目对应的盘根目录创建对应子目录(etc/cas/service),把证书和json文件放进对应的位置
{
"@class": "org.apereo.cas.services.RegexRegisteredService",
"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
}
7.mysql创建数据库和对应的表,并插入数据
CREATE TABLE `cas` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) DEFAULT NULL COMMENT '用户名',
`password` varchar(255) DEFAULT NULL COMMENT '密码',
`create_time` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
由于cas对密码经过md5加密的,所以密码需要处理后再插入
8.运行启动cas
四. docker-compose部署cas服务
1. 将项目上传到linux服务器,通过命令生成对应的镜像
docker build -t cas.tar:6.4 .
2.编写docker-compose.yml
version: "3"
services:
cas:
image: cas:6.4
container_name: cas-server
restart: always
ports:
- 8083:8080
- 8443:8443
volumes:
- ./conf/cas.properties:/etc/cas/config/cas.properties:ro
- ./conf/thekeystore:/etc/cas/thekeystore:ro
- ./conf/HTTPSandHTTP-10000001.json:/etc/cas/services/HTTPSandHTTP-10000001.json:ro
3.编写cas.properties
as.server.name=https://自己的域名:8443
cas.server.prefix=${cas.server.name}/cas
logging.config=file:/etc/cas/config/log4j2.xml
cas.tgc.secure=false
cas.serviceRegistry.initFromJson=true
cas.serviceRegistry.json.location=file:/etc/cas/services
4.启动并访问