使用MySQL在cas52中实现认证访问(cas5.2 mysql)

使用MySQL在CAS5.2中实现认证访问

CAS(Central Authentication Service)是一个基于标准的Web单点登录协议(CAS协议)的应用系统。它可以提供基于token的认证和授权服务,同时可以避免在各个应用系统中重复登录的繁琐操作。CAS5.2是CAS的一个稳定版本,它支持多种身份验证机制,包括LDAP、JDBC、OAuth等。这篇文章将介绍如何使用MySQL数据库作为身份验证源,在CAS5.2中实现认证访问。

步骤一:安装MySQL数据库

在服务器上安装MySQL数据库,并创建一个数据库及用户用于CAS认证服务。我们以CentOS7为例,执行以下命令:

sudo yum install -y mariadb mariadb-server mariadb-devel
sudo systemctl start mariadb
sudo systemctl enable mariadb
sudo mysql_secure_installation

在mysql控制台执行以下命令:

CREATE DATABASE `cas5_db` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
CREATE USER 'cas5_user'@'localhost' IDENTIFIED BY 'cas5_password';
GRANT ALL PRIVILEGES ON cas5_db.* TO 'cas5_user'@'localhost';
FLUSH PRIVILEGES;

步骤二:安装CAS5.2

可以直接在https://github.com/apereo/cas-overlay-template/releases下载最新版的CAS Overlay模板,解压后可以看到pom.xml文件等。根据项目需要修改其中的配置项。我们这里只需要修改一下cas.properties的配置,指定数据库连接参数和认证策略即可。

cas.serviceRegistry.initFromJson=true
cas.serviceRegistry.json.location=file:/etc/cas/services
cas.authn.jdbc.query[0].sql=SELECT password FROM users WHERE username=?
cas.authn.jdbc.query[0].fieldPassword=password
cas.authn.jdbc.query[0].url=jdbc:mysql://localhost:3306/cas5_db
cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQL5Dialect
cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
cas.authn.jdbc.query[0].user=cas5_user
cas.authn.jdbc.query[0].password=cas5_password
cas.authn.jdbc.query[0].autoCommit=true

步骤三:创建用户和服务

在CAS管理后台,可以创建用户和服务。我们以一个简单的web应用为例,假设其访问路径为http://webapp.example.com。首先我们需要在/etc/cas/services目录下创建一个服务配置文件,用于存储web应用的认证策略。

{
"@class" : "org.apereo.cas.services.RegexRegisteredService",
"serviceId" : "^http://webapp.example.com/.*",
"name" : "WebApp",
"id" : 10000001,
"evaluationOrder" : 0,
"attributeReleasePolicy" : {
"@class" : "org.apereo.cas.services.ReturnAllowedAttributeReleasePolicy",
"allowedAttributes" : [ "cn", "eml" ]
},
"properties" : {
"key" : "value"
}
}

然后在CAS管理后台的“用户管理”菜单中,创建用户并设置密码,可以选择从数据库中导入已有用户。

步骤四:测试访问

打开http://cas.example.com/cas/login?service=http://webapp.example.com,输入CAS管理员账号和密码进行登录。登录成功后,将重定向到http://webapp.example.com,此时可以获取到用户的认证信息,如用户名、邮箱等。

{
"user" : "admin",
"eml" : "admin@example.com"
}

总结

本文介绍了如何在CAS5.2中使用MySQL数据库作为身份验证源,实现认证访问。具体步骤包括安装MySQL数据库、安装CAS5.2、创建用户和服务、测试访问。通过这些操作,我们可以在Web应用中使用CAS提供的单点登录服务,提高用户访问效率,降低操作成本。


数据运维技术 » 使用MySQL在cas52中实现认证访问(cas5.2 mysql)