CAS 5.3使用MySQL數據庫登錄


一、本例環境說明

  • 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/ 如發現有錯,請留言,謝謝)


免責聲明!

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



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