密码复杂度检测工具

文章目录

前言

为了做密评整改,而做的一个工具,主要借助的是正则表达式

工具

/**
 * 密码复杂度检测工具
 */
public class PwdCheckUtil {


    /**
     * 校验
     * @param pwd
     */
    public static void checkStrongPwd(String pwd) {
        if (StringUtils.isEmpty(pwd)) {
            throw new CustomException("密码不能为空");
        }
        if (!PwdCheckUtil.checkPasswordLength(pwd, 8)
                || !PwdCheckUtil.checkContainCase(pwd)
                || !PwdCheckUtil.checkContainDigit(pwd)
                || !PwdCheckUtil.checkContainSpecialChar(pwd)
        ) {
            throw new CustomException("密码必须包含数字、字母、特殊符号且大于8位");
        }
        System.out.println("验证通过");
    }


    /**
     * 检测密码中字符长度
     *
     * @param password
     * @param minNum
     * @return
     */
    public static boolean checkPasswordLength(String password, Integer minNum) {
        return password.length() >= minNum;
    }


    /**
     * 判断密码是否包含数字
     *
     * @param password
     * @return
     */
    public static boolean checkContainDigit(String password) {
        Pattern pattern = Pattern.compile(".*\\d.*");
        Matcher matcher = pattern.matcher(password);
        return matcher.matches();
    }

    /**
     * 判断密码是否包含大小写
     *
     * @param password
     * @return
     */
    public static boolean checkContainCase(String password) {
        Pattern pattern = Pattern.compile("(?=.*[a-z])(?=.*[A-Z])");
        Matcher matcher = pattern.matcher(password);
        return matcher.find();
    }


    /**
     * 包含特殊符号
     *
     * @param password
     * @return
     */
    public static boolean checkContainSpecialChar(String password) {
        Pattern pattern = Pattern.compile("[!\"#$%&'()*+,-./:;<=>?@\\[\\\\\\]^_`{|}~]");
        Matcher matcher = pattern.matcher(password);
        return matcher.find();
    }


}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值