搭建单点登录系统之二:完善CAS5.3服务端的配置

CAS服务端搭建好之后,还是有很多地方不能满足我们的需求,需要修改cas.war包中 WEB-INF\classes\application.properties 配置文件,增加我们自定义的配置

1、去掉https认证。

2、从数据库认证用户名和密码。

3、忘记密码发邮件重置密码。

#####################自定义的配置#####################

#配置授权,需要配置 server name的信息,不然就会跑到cas.example.org
cas.server.name=http://cas.xxx.com
cas.server.prefix=${cas.server.name}${server.context-path}

##
#不使用加密传输https协议,仅使用http,默认是打开状态
#
cas.tgc.secure=false

#开启识别json文件,默认false
cas.serviceRegistry.initFromJson=true

##
#Logout 退出
#
#是否允许退出后跳转到指定的页面,默认是false,即登录不重定向到service参数指定的页面
cas.logout.followServiceRedirects=true


##
# Query Database Authentication 数据库查询校验用户名开始
#
# 查询账号密码sql,必须包含密码字段
cas.authn.jdbc.query[0].sql=select * from sys_user where account = ?
cas.authn.jdbc.query[0].fieldPassword=PASSWORD
cas.authn.jdbc.query[0].fieldExpired=ISEXPIRED
cas.authn.jdbc.query[0].fieldDisabled=ISLOCK
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://10.1.1.23:3306/jk2_uat?useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=round&allowMultiQueries=true
cas.authn.jdbc.query[0].user=root
cas.authn.jdbc.query[0].password=123456


#默认加密策略,通过encodingAlgorithm来指定算法,默认NONE不加密
cas.authn.jdbc.query[0].passwordEncoder.type=com.hanchen.cas.CustomPasswordEncoder
cas.authn.jdbc.query[0].passwordEncoder.characterEncoding=UTF-8
cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm=SHA-256

#
##密码管理开始
cas.authn.pm.enabled=true

#发送邮件
spring.mail.host=smtp.xxx.com
#使用25端口,465端口需要SSL协议
spring.mail.port=25
#邮箱用户名(公司邮箱)
spring.mail.username=xxx@xxx.com
#邮箱密码
spring.mail.password=xxxxxx
spring.mail.testConnection=false
spring.mail.properties.mail.smtp.auth=true


#是否必须ssl,如果是465端口,这项则为true
spring.mail.properties.mail.smtp.ssl.enable=false

#发送邮箱
cas.authn.pm.reset.mail.from=${spring.mail.username}
#新密码必须匹配表达式
cas.authn.pm.policy-pattern=\\w{1,24}

#邮箱查找
#根据用户名查找邮箱
cas.authn.pm.jdbc.sqlFindEmail=SELECT email FROM sys_user WHERE account = ?
#修改密码
cas.authn.pm.jdbc.sqlChangePassword=update sys_user set password = ? where account = ?
cas.authn.pm.jdbc.url=${cas.authn.jdbc.query[0].url}
cas.authn.pm.jdbc.user=${cas.authn.jdbc.query[0].user}
cas.authn.pm.jdbc.password=${cas.authn.jdbc.query[0].password}
cas.authn.pm.jdbc.dialect=${cas.authn.jdbc.query[0].dialect}
cas.authn.pm.jdbc.driverClass=${cas.authn.jdbc.query[0].driverClass}
#密码修改加密规则,这个必须要和原始密码加密规则一致
cas.authn.pm.jdbc.passwordEncoder.type=${cas.authn.jdbc.query[0].passwordEncoder.type}
cas.authn.pm.jdbc.passwordEncoder.characterEncoding=${cas.authn.jdbc.query[0].passwordEncoder.characterEncoding}
cas.authn.pm.jdbc.passwordEncoder.encodingAlgorithm=${cas.authn.jdbc.query[0].passwordEncoder.encodingAlgorithm}
cas.authn.pm.jdbc.passwordEncoder.secret=${cas.authn.jdbc.query[0].passwordEncoder.secret}
#默认是false,不会提交update语句
cas.authn.pm.jdbc.autocommit=true

#是否开启问题回答
cas.authn.pm.reset.securityQuestionsEnabled=false

#####################自定义的配置#####################

4、自定义密码加密类。

在下载的源码目录 src下增加自定义的密码加密类。

package com.hanchen.cas;

import java.security.MessageDigest;

import org.apache.commons.codec.binary.Base64;
import org.springframework.security.crypto.password.PasswordEncoder;

public class CustomPasswordEncoder implements PasswordEncoder {

    public String encode(CharSequence password) {
        try {
            // 给数据进行SHA-256加密
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            byte[] digest = md.digest(password.toString().getBytes("UTF-8"));
            String encodePassword = new String(Base64.encodeBase64(digest));

            System.out.println("encode方法:加密前(" + password + "),加密后(" + encodePassword + ")");

            return encodePassword;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 调用这个方法来判断密码是否匹配
     */
    @Override
    public boolean matches(CharSequence rawPassword, String encodePassword) {
        // 判断密码是否存在
        if (rawPassword == null) {
            return false;
        }
        // 通过SHA-256加密后的密码
        String pass = this.encode(rawPassword.toString());

        System.out.println(
                "matches方法:rawPassword:" + rawPassword + ",encodePassword:" + encodePassword + ",pass:" + pass);

        // 比较密码是否相等的问题
        return pass.equals(encodePassword);
    }
}

5、修改cas.war包下 WEB-INF\classes\services\HTTPSandIMAPS-10000001.json 这个文件,增加 http 表示允许 http 协议的客户端访问服务端。

猜你喜欢

转载自www.cnblogs.com/gaopengpy/p/12371249.html