初见cas服务端2:支持数据库和自定义密码加密和比对

3 篇文章 0 订阅

上一篇环境搭建好了,接下来,支持数据库。

1、我这边为了方便,我在cas-overlay-template 创建了java的路径,方便后面修改。

2、我把容易变化的配置文件放到resources

3、在我们刚刚拷贝的application.properties里现在连接数据库的配置文件。

cas.authn.jdbc.query[0].driverClass=com.mysql.jdbc.Driver
cas.authn.jdbc.query[0].url=jdbc:mysql://xxx:3306/security_demo?useUnicode=yes&characterEncoding=UTF-8&useSSL=false
cas.authn.jdbc.query[0].user=root
cas.authn.jdbc.query[0].password=123456
cas.authn.jdbc.query[0].sql=select *from cas where username=?
cas.authn.jdbc.query[0].fieldPassword=password
cas.authn.jdbc.query[0].passwordEncoder.type=DEFAULT
#cas.authn.jdbc.query[0].passwordEncoder.type=com.zsy.encoder.CustomEncoderPassword
cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=utf-8
cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=md5

解释下,前面几个就不说了,sql,可以直接把密码查出来,就不用写fieldPassword了,,按照我上面的,如果返回多个字段,就要指定密码字段;type这个官方文档写的很明白了,https://apereo.github.io/cas/5.3.x/installation/Configuration-Properties-Common.html#database-settings

4、表结构很简单

 

启动测试;打包部署到tomcat也行,打包后可以看看修改的配置是否打到war包里,正常情况是打进去了;

访问,是正常的,准备登陆

 

登陆成功:

如果密码能满足,这样就可以用了。

 

如果密码加解密满足不了,需要自定义加解密了。其实这个加解密,cas已经支持了,我这里只是给测试

package com.zsy.encoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * @desc
 * @Author zhaoshouyun
 * @Date 2020/8/16 17:11
 */

public class CustomEncoderPassword implements PasswordEncoder {
    private static final Logger LOGGER = LoggerFactory.getLogger(CustomEncoderPassword.class);
    //建议使用security 里的加密
    BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();

    @Override
    public String encode(CharSequence textPassword) {
        String encode =encoder.encode(textPassword);
        LOGGER.info("明文:[{}],加过密后的密文:[{}]",textPassword,encode);
        return encode;
    }

    @Override
    public boolean matches(CharSequence rawPassword, String encodedPassword) {
        boolean matches = encoder.matches(rawPassword, encodedPassword);
        LOGGER.info("密码比对结果:{}",matches);
        return matches;
    }

    public static void main(String[] args) {
        String textPassword = "123456";
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode(textPassword);
        System.out.println(encode);
    }
}

接下来把他配置到配置文件里;把type换成密码类即可

我写了日志,需要在日志文件里配置下,才能输出日志

<AsyncLogger name="com.zsy.encoder.CustomEncoderPassword" level="info" includeLocation="true">
    <AppenderRef ref="casConsole"/>
</AsyncLogger>

数据库密码修改为自定义加密的密码:lishi用户

重新启动,访问

 

登陆成功了,

日志:

 

 

后面继续研究指出图片验证码和自定义验证流程

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值