zoukankan      html  css  js  c++  java
  • cas单点登录-jdbc认证(三)

    前言

     本节的内容为JDBC认证,查找数据库进行验证,其中包括:

    • 密码加密策略(无密码,简单加密,加盐处理)
    • 认证策略(jdbc)

    一、业务需求

    不同的公司,需求业务需求或者架构不一样导致我们实现验证的方式不一样,那么cas为我们提供了很多认证的模式(当然也可以自定义),其中常用的有:

    • JDBC认证
    • LDAP认证
    • Basic认证
    • Shiro认证
    • Pac4j认证
    • MongoDB认证
    • Rest认证
    • IP黑白名单

    还有可能交给第三方认证,例如:微信、QQ、新浪,github等等

    当然也有一些公司或者企业也非常的变态,如:

    1. 认证中心不能直接访问账号库,cas也提供功能,可以考虑用REST认证模块来处理这个事情
    2. 老系统账号唯一性不确定,例如 组织+账号 才是唯一值,这时候只能自定义认证器(后面章节会有教程)
    3. 业务系统要求返回用户密码(多属性返回)

    二、加密方案

    cas支持jdbc校验方案:

    • 根据sql给予用户名进行查询,根据密码字段进行鉴定(select * from table_users where username=?)可判断有效等
    • 通过盐等手段进行编码加密再进行匹配(推荐)
    • 根据sql给予用户名以及密码进行查询(select count(x) from tables_users where username = ? and password=?),不可判断有效期,若数量大于0则成功
    • 根据用户名密码连接数据库,原理是通过jdbc,若连接成功则成功

    下文会讲述前两种的配置方法


    常用单向加密算法:MD5、SHA、HMAC

    一般的加密策略的三种:

    • 单项加密算法(密码)
    • 单向加密算法(密码+动态盐+私有盐)*加密次数(推荐
    • 不加密(不推荐

    上述提到的加密方案策略,下面都会一一说明白

    三、Jdbc认证

    3.1、创建数据库cas,新增一张用户表sys_user

    说明:

    • expired字段表示过期,1表示已过期,需要修改密码
    • disabled表示账号是否禁用,1表示禁用

    3.2、sys_user表数据

    id username password expired email disabled
    1 admin 845d5f1153c27beed29f479640445148 0   1
    2 jacky caf1a3dfb505ffed0d024130f58c5cfa 1   0
    4 zhangsan 68053af2923e00204c3ca7c6a3150cf7 0   0

    说明:

    • 如果采用MD5加密,那password就是MD5加密后的密文,sha同样如此
    • admin、jacky、zhangsan明文密码分别是xiaoxiao、321、789

    3.2、修改sso-server/pom.xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <parent>
            <groupId>com.carl.auth</groupId>
            <artifactId>sso</artifactId>
            <version>1.0.0</version>
            <relativePath>../pom.xml</relativePath>
        </parent>
    
        <artifactId>sso-server</artifactId>
        <packaging>war</packaging>
    
        <name>sso-server</name>
        <description>CAS认证服务,负责各系统的鉴权的鉴权</description>
    
        <dependencies>
            <dependency>
                <groupId>org.apereo.cas</groupId>
                <artifactId>cas-server-webapp-tomcat</artifactId>
                <version>${cas.version}</version>
                <type>war</type>
                <scope>runtime</scope>
            </dependency>
            <!--新增支持jdbc验证-->
            <dependency>
                <groupId>org.apereo.cas</groupId>
                <artifactId>cas-server-support-jdbc</artifactId>
                <version>${cas.version}</version>
            </dependency>
            <!--使用mysql驱动-->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>${mysql.version}</version>
            </dependency>
        </dependencies>
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.apereo.cas</groupId>
                    <artifactId>cas-server-support-bom</artifactId>
                    <version>${cas.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>com.rimerosolutions.maven.plugins</groupId>
                    <artifactId>wrapper-maven-plugin</artifactId>
                    <version>0.0.5</version>
                    <configuration>
                        <verifyDownload>true</verifyDownload>
                        <checksumAlgorithm>MD5</checksumAlgorithm>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <version>${springboot.version}</version>
                    <configuration>
                        <mainClass>org.springframework.boot.loader.WarLauncher</mainClass>
                        <addResources>true</addResources>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.1.0</version>
                    <configuration>
                        <warName>cas</warName>
                        <failOnMissingWebXml>false</failOnMissingWebXml>
                        <recompressZippedFiles>false</recompressZippedFiles>
                        <archive>
                            <compress>false</compress>
                            <manifestFile>${project.build.directory}/war/work/org.apereo.cas/cas-server-webapp-tomcat/META-INF/MANIFEST.MF</manifestFile>
                        </archive>
                        <overlays>
                            <overlay>
                                <groupId>org.apereo.cas</groupId>
                                <artifactId>cas-server-webapp-tomcat</artifactId>
                            </overlay>
                        </overlays>
                    </configuration>
                </plugin>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.3</version>
                </plugin>
            </plugins>
            <finalName>cas</finalName>
        </build>
    </project>

     3.3、application.properties新增配置

    #jdbc验证配置
    #Query Database Authentication 数据库查询校验用户名开始
    #查询账号密码sql,必须包含密码字段
    cas.authn.jdbc.query[0].sql=select * from sys_user where username=?
    #指定上面的sql查询字段名(必须)
    cas.authn.jdbc.query[0].fieldPassword=password
    #指定过期字段,1为过期,若过期需要修改密码
    cas.authn.jdbc.query[0].fieldExpired=expired
    #为不可用字段段,1为不可用,
    cas.authn.jdbc.query[0].fieldDisabled=disabled
    #数据库方言hibernate的知识
    cas.authn.jdbc.query[0].dialect=org.hibernate.dialect.MySQLDialect
    #数据库驱动 
    cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
    #数据库连接
    cas.authn.jdbc.query[0].url=jdbc:mysql://localhost:53306/cas?useUnicode=true
    &characterEncoding=UTF-8
    #数据库用户名
    cas.authn.jdbc.query[0].user=root
    #数据库密码
    cas.authn.jdbc.query[0].password=123456
    #默认加密策略,通过encodingAlgorithm来指定算法,默认NONE不加密
    cas.authn.jdbc.query[0].passwordEncoder.type=DEFAULT
    cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8
    cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=MD5
    #Query Database Authentication 数据库查询校验用户名结束 #jdbc验证配置

    以上配置,如驱动,查询数据库等等需要根据不同的场景进行调整

    • 若密码无加密,调整passwordEncoder.type=NONE
    • 若密码加密策略为SHA,调整passwordEncoder.encodingAlgorithm=SHA
    • 若算法为自定义,实现org.springframework.security.crypto.password.PasswordEncoder接口,并且把类名配置在passwordEncoder.type

    3.4、执行流程

    例如:输入admin/xiaoxiao

    3.5、对密码进行盐值处理再加密,application.properties配置文件修改

    #Encode Database Authentication 开始
    #加密次数
    cas.authn.jdbc.encode[0].numberOfIterations=2
    #该列名的值可替代上面的值,但对密码加密时必须取该值进行处理
    cas.authn.jdbc.encode[0].numberOfIterationsFieldName=
    # 盐值固定列
    cas.authn.jdbc.encode[0].saltFieldName=username
    #静态盐值
    cas.authn.jdbc.encode[0].staticSalt=.
    cas.authn.jdbc.encode[0].sql=select * from sys_user_encode where username=?
    #对处理盐值后的算法
    cas.authn.jdbc.encode[0].algorithmName=MD5
    cas.authn.jdbc.encode[0].passwordFieldName=password
    cas.authn.jdbc.encode[0].expiredFieldName=expired
    cas.authn.jdbc.encode[0].disabledFieldName=disabled
    cas.authn.jdbc.encode[0].url=jdbc:hsqldb:mem:cas-hsql-database
    cas.authn.jdbc.encode[0].dialect=jdbc:mysql://localhost:53306/cas?useUnicode=true&characterEncoding=UTF-8
    cas.authn.jdbc.encode[0].user=root 
    cas.authn.jdbc.encode[0].password=123456
    cas.authn.jdbc.encode[0].driverClass=
    com.mysql.jdbc.Driver

    #Encode Database Authentication 结束

    4、验证

    4.1、输入admin/xiaoxiao

     

     4.2、输入、jacky/321

    4.3、输入zhangsan/789

    5、总结 

    • pom.xm配置引入jdbc支持包,和 数据库驱动包 
    • application.properties增加数据连接配置和加密方式

    欢迎关注

  • 相关阅读:
    .NET:在ASP.NET中如何进行IP限制
    vim配置文件和插件
    初学Perl的感受之数据类型
    ASP.NET伪静态详解及配置
    Wayback Machine
    对单元测试的一点感悟——这是一把双刃剑
    python中使用postgres
    第三章 匿名方法
    在C#程序中使用ocx的方法
    可扩展的 “密码强度” 代码示例
  • 原文地址:https://www.cnblogs.com/520playboy/p/7827375.html
Copyright © 2011-2022 走看看