cas单点登录学习(二)database方式认证 md5加密 盐加密

cas单点登录学习(二)database方式认证

cas官网的说明

pom文件

添加 database认证 必要的包
我使用的是mysql,所以添加mysql驱动包

    <dependencies>
<!--        cas database 认证支持-->
        <dependency>
            <groupId>org.apereo.cas</groupId>
            <artifactId>cas-server-support-jdbc</artifactId>
            <version>${cas.version}</version>
        </dependency>
<!--mysql driver-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.25</version>
        </dependency>
    </dependencies>

yaml

#SSL配置 证书
server:
ssl:
 enabled: true
 key-store: classpath:thekeystore
 key-store-password: changeit
 key-password: changeit

cas:
authn:
 jdbc:
   query:
      #数据库连接
     - url: jdbc:mysql://127.0.0.1:3306/blog_cas?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL
        =false
       # 用户名
       user: blog-cas
       # 密码
       password: blog-cas
       # 方言
       dialect: org.hibernate.dialect.MySQLDialect
       # 数据库驱动
       driverClass: com.mysql.jdbc.Driver
       #        idleTimeout: 5000
       #查询账号密码SQL,必须包含密码字段
       sql: select  username,password from user where username=?
       # 指定密码字段
       fieldPassword: password

在数据库中创建user表
在这里插入图片描述
重新打包,启动后,就可以用 用户a/1登录了

md5加密

md5可以把明文密码计算成固定长度(32位)的字符串,不可逆

cas:
  authn:
    jdbc:
      query:
      	-   passwordEncoder:
            type: DEFAULT
            encodingAlgorithm: MD5

mysql中 查询1的md5值,复制在user表中

select md5(‘1’)

MD5加盐加密

cas 官网说明 Encode Database Authentication
md5加密并不是特别安全,两个相同的明文密码的md5值也相同,盐加密解决了这个问题,即使明文密码相同,也可以有不同的密码值,可以实现一人一个密码。
盐加密通过给密码加私有盐和公有盐后在计算出md5的值,这样实现一人一个密码

yml

# 盐加密
cas:
  authn:
    jdbc:
      encode:
        # jdbc
        -   url: jdbc:mysql://127.0.0.1:3306/blog_cas?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
            # 驱动
            driverClass: com.mysql.jdbc.Driver
            # 方言
            dialect: org.hibernate.dialect.MySQL5Dialect
            # 数据库用户名
            user: blog-cas
            # 数据库密码
            password: blog-cas
            # 加密方式
            algorithmName: MD5
            # 加密迭代次数
            numberOfIterations: 2
            # 密码字段
            passwordFieldName: password
            # 动态盐值
            saltFieldName: username
            # 静态盐值
            staticSalt: 123
            sql: SELECT username,password FROM user WHERE username =?

生成盐值
需要的包

	        <dependency>
            <groupId>org.apache.shiro</groupId>
            <artifactId>shiro-core</artifactId>
            <version>1.4.0</version>
<!--            <scope>test</scope>-->
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    package jsong;

    import org.apache.shiro.crypto.hash.ConfigurableHashService;
    import org.apache.shiro.crypto.hash.DefaultHashService;
    import org.apache.shiro.crypto.hash.HashRequest;
    import org.apache.shiro.util.ByteSource;
    import org.junit.Test;

    public class CreateSaltPassword {
        // 静态盐值
        private String staticSalt = "123";
        // 加密算法
        private String algorithmName = "MD5";
        // 密码
        private String encodedPassword = "1";
        // 用户名 动态盐值
        private String dynaSalt = "test";
        // 加密迭代次数
        private int numberOfIterations = 2;

        @Test
        public void test() throws Exception {
            ConfigurableHashService hashService = new DefaultHashService();
            hashService.setPrivateSalt(ByteSource.Util.bytes(this.staticSalt));
            hashService.setHashAlgorithmName(this.algorithmName);
            hashService.setHashIterations(this.numberOfIterations);
            HashRequest request = new HashRequest.Builder()
                    .setSalt(dynaSalt)
                    .setSource(encodedPassword)
                    .build();
            String res = hashService.computeHash(request).toHex();
            System.out.println(res);
        }
    }

把用户名test和生成的盐值存在数据库,因为用户名,所以即使密码相同,不同用户也会有不同的盐值
使用test/1用户就可以登录了
在这里插入图片描述

参考文章

发布了83 篇原创文章 · 获赞 21 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/JsongNeu/article/details/104227262