CAS 5.3使用MySQL数据库登录

2023-10-19 更新,由于阿里云maven镜像仓库里缺失了很多CAS5.3 依赖,如果缺少可以用附件里的同步到本地仓库 链接:https://pan.baidu.com/s/14DhRci_AmivA8DCZ6Sx1wg?pwd=58d2 提取码:58d2

一、本例环境说明

  • JDK 1.8
  • CAS 5.3
  • apache-maven-3.6.0
  • mysql-5.6.32

二、CAS 5.3基础环境搭建与验证

需要按照《CAS 5.3服务器搭建》搭建好环境,并使用系统默认用户名密码验证通过。

三、MySQL相关准备

3.1 创建MySQL数据库

(创建过程略)

3.2 新建user表,并增加2条记录

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `login_name` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `password` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL,
  `address` varchar(20) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
  `role` varchar(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL,
  PRIMARY KEY (`id`) USING BTREE,
  UNIQUE INDEX `login_name_un`(`login_name`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 4 CHARACTER SET = utf8 COLLATE = utf8_unicode_ci ROW_FORMAT = Compact;

-- ----------------------------
-- Records of user
-- ----------------------------
INSERT INTO `user` VALUES (1, '1', '1', '1', 'admin');
INSERT INTO `user` VALUES (2, '22', '22', '22', 'other');

SET FOREIGN_KEY_CHECKS = 1;

1)具体表结构如下

2)具体表内容如下

四、CAS配置MySQL相关信息

4.1 修改CAS的maven依赖

修改cas-overlay-template-5.3\pom.xml文件

1)properties节点中,新增MySQL驱动版本

<mysql.driver.version>5.1.49</mysql.driver.version>

注意:一定要先去查询是否有该版本,本文使用是的当前5系列最后一个版本5.1.49,查询地址:https://mvnrepository.com/artifact/mysql/mysql-connector-java

2)cas-server-webapp${app.server}所在同级dependency节点中增加以下配置

<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>${mysql.driver.version}</version>
</dependency>

4.2 修改CAS自定义application.properties配置

打开cas-overlay-template-5.3\src\main\resources\application.properties文件

1)注释静态验证的配置

2)增加MySQL配置

# 查询账号密码SQL,必须包含密码字段
cas.authn.jdbc.query[0].sql=select * from user where login_name=?
# 指定上面的SQL查询字段名(必须)
cas.authn.jdbc.query[0].fieldPassword=password
# 数据库连接
cas.authn.jdbc.query[0].url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
# 数据库用户名
cas.authn.jdbc.query[0].user=root
# 数据库用户密码
cas.authn.jdbc.query[0].password=自己Mysql的密码
# 数据库驱动
cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver

配置说明

1)cas.authn.jdbc.query[0].sql=select * from user where login_name=? 其中 user是mysql中的表名,login_name是用户名字段

2)cas.authn.jdbc.query[0].fieldPassword=password 其中password是user表中的密码字段

3)cas.authn.jdbc.query[0].url 数据库连接一定要修改成自己的数据库名称

4)cas.authn.jdbc.query[0].password 为自己MySQL密码

五、CAS验证

5.1 执行构建脚本

.\build.cmd run

5.2 看得到如下提示表示启动成功

5.3 浏览器访问CAS

http://localhost:8443/cas

5.4 输入MySQL user表中的用户名1,密码1点击登录

5.5 登录成功

至此,CAS结合MySQL验证搭建完成.

六、注意事项

6.1 如果登录提示"认证信息无效"

  1. 检查cas-overlay-template-5.3\pom.xml文件中是否额外添加了MySQL驱动
  2. 检查cas-overlay-template-5.3\src\main\resources\application.properties文件是否用户表写错,用户名密码写错等,具体请查看4.2中MySQL配置说明

6.2 附其它一些MySQL配置说明,以供参考

##
# application.properties
# MySQL 配置
#
# 查询账号密码SQL,必须包含密码字段
cas.authn.jdbc.query[0].sql=select * from user where login_name=?
# 指定上面的SQL查询字段名(必须)
cas.authn.jdbc.query[0].fieldPassword=password
# 指定过期字段,1为过期,若过期不可用
cas.authn.jdbc.query[0].fieldExpired=expired
# 为不可用字段段,1为不可用,需要修改密码
cas.authn.jdbc.query[0].fieldDisabled=disabled
# 数据库连接
cas.authn.jdbc.query[0].url=jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=UTF-8
# 数据库dialect配置
cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
# 数据库用户名
cas.authn.jdbc.query[0].user=root
# 数据库用户密码
cas.authn.jdbc.query[0].password=自己Mysql的密码
# 数据库事务自动提交
cas.authn.jdbc.query[0].autocommit=false
# 数据库驱动
cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
# 超时配置
cas.authn.jdbc.query[0].idleTimeout=50000
# 默认加密策略,通过encodingAlgorithm来指定算法,默认NONE不加密
# NONE|DEFAULT|STANDARD|BCRYPT|SCRYPT|PBKDF2
cas.authn.jdbc.query[0].passwordEncoder.type=NONE
#cas.authn.jdbc.query[0].passwordEncoder.type=org.muses.jeeplatform.cas.authentication.encode.MD5PasswordEncoder
# 字符类型
cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8
# 加密算法
cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5
# 加密盐
cas.authn.jdbc.query[0].passwordEncoder.secret=
# 加密字符长度
cas.authn.jdbc.query[0].passwordEncoder.strength=16

(转发请注明出处:http://www.cnblogs.com/zhangyongli2011/ 如发现有错,请留言,谢谢)

posted @ 2021-08-20 13:52  非法小恋  阅读(1182)  评论(0编辑  收藏  举报