zoukankan      html  css  js  c++  java
  • 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.3pom.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.3srcmain esourcesapplication.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 执行构建脚本

    .uild.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.3pom.xml文件中是否额外添加了MySQL驱动
    2. 检查cas-overlay-template-5.3srcmain esourcesapplication.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/ 如发现有错,请留言,谢谢)

  • 相关阅读:
    微信开发者工具打开一直处于初始化应用通信能力界面的解决办法
    vuex学习笔记
    生产环境下的log 在正式环境下隐藏log
    el-table 表头添加下拉筛选框 附带输入过滤
    element-ui table type=“expand“ 无内容时 不可展开
    窗口移到了电脑桌面边缘外拖不回来解决办法
    yarn的基础语法:yarn安装完vue cli3后提示不是内部命令
    nodejs npm错误Error:UNKNOWN:unknown error,mkdir 'D:Develop odejs ode_global'at Error(可行)
    vue-cli 更新遇到的问题,卸载不掉旧版本2.9.6(可行)
    vue基础——命名路由
  • 原文地址:https://www.cnblogs.com/zhangyongli2011/p/15166171.html
Copyright © 2011-2022 走看看