强密码校验
强密码规则: 必须包含大小写字母和特殊字符, 长度为8至30位
JS版本强密码规则校验
var regex= /^(?![A-z0-9]+$)(?=.[^%&',;=?$\x22])(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{8,20}$/;
console.log(regex.test("Qq123456*"));
Java版本强密码规则校验
package com.util;
/**
* @Description 密码强度校验
* * 1) 密码控制只能输入字母、数字、特殊符号(~!@#$%^&*()_+[]{}|\;:'",./<>?)
* * 2) 长度 8-30 位,必须包括大小写字母、数字、特殊符号中的4种
* * 3) 密码不能用户名信息相同
* @Author Yzx
* @Date 2021-4-28 15:27
* @Version V1.0.0
*/
public class CheckPassword {
//数字
public static final String REG_NUMBER = ".*\\d+.*";
//小写字母
public static final String REG_UPPERCASE = ".*[A-Z]+.*";
//大写字母
public static final String REG_LOWERCASE = ".*[a-z]+.*";
//特殊符号(~!@#$%^&*()_+|<>,.?/:;'[]{}\)
public static final String REG_SYMBOL = ".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*";
public static boolean checkPasswordRule(String password,String username){
//密码为空及长度大于8位小于30位判断
if (password == null || password.length() <8 || password.length()>30) return false;
int i = 0;
if (password.matches(CheckPassword.REG_NUMBER)) i++;
if (password.matches(CheckPassword.REG_LOWERCASE))i++;
if (password.matches(CheckPassword.REG_UPPERCASE)) i++;
if (password.matches(CheckPassword.REG_SYMBOL)) i++;
boolean contains = password.equals(username);
//此处可以判定包含几种强密码为4
if (i < 4 || contains) return false;
return true;
}
public static void main(String[] args) {
System.out.println(checkPasswordRule("1234@Root", ""));
}
}