/**
* @description: 密码安全策略,杜绝弱口令
*/
public class PwdCheckUtil {
// 斜向连续键盘数字
public static String[] KEYBOARD_SLOPE_ARR = {
"!qaz", "1qaz", "@wsx", "2wsx", "#edc", "3edc", "$rfv", "4rfv", "%tgb", "5tgb",
"^yhn", "6yhn", "&ujm", "7ujm", "*ik,", "8ik,", "(ol.", "9ol.", ")p;/", "0p;/",
"+[;.", "=[;.", "_pl,", "-pl,", ")okm", "0okm", "(ijn", "9ijn", "*uhb", "8uhb",
"&ygv", "7ygv", "^tfc", "6tfc", "%rdx", "5rdx", "$esz", "4esz"
};
// 横向连续键盘数字
public static String[] KEYBOARD_HORIZONTAL_ARR = {
"01234567890-=",
"!@#$%^&*()_+",
"qwertyuiop[]",
"QWERTYUIOP{}",
"asdfghjkl;'",
"ASDFGHJKL:",
"zxcvbnm,./",
"ZXCVBNM<>?",
};
// 特殊字符
public static String SPECIAL_CHAR = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
public static void main(String[] args) {
String password = "Zack1234";
// System.out.println("密码长度校验:"+checkPasswordLength(password, "8", "20"));
// System.out.println("是否包含数字校验:"+checkContainDigit(password));
// System.out.println("是否包含字母(不分大小写)校验:"+checkContainCase(password));
// System.out.println("是否包含小写字母校验:"+checkContainLowerCase(password));
// System.out.println("是否包含大写字母校验:"+checkContainUpperCase(password));
// System.out.println("是否包含特殊字符校验:"+checkContainSpecialChar(password));
System.out.println("不区分大小写横向键盘顺序连续输入4个及以上字符" + checkLateralKeyboardSite(password, 4, false));
// System.out.println("不区分大小写斜像向键盘顺序连续输入4个及以上字符"+checkKeyboardSlantSite(password, 3,false));
// System.out.println("不区分大小写评估a-z,z-a连续输入4个及以上字符"+checkSequentialChars(password, 3, false));
// System.out.println("连续输入3个及以上重复字符"+checkSequentialSameChars(password, 3));
}
/**
* @return 符合长度要求 返回true
* @brief 检测密码中字符长度
* @param[in] password 密码字符串
*/
public static boolean checkPasswordLength(String password, String minNum, String maxNum) {
boolean flag = false;
if (StringUtils.isBlank(maxNum)) {
minNum = StringUtils.isBlank(minNum) == true ? "0" : minNum;
if (password.length() >= Integer.parseInt(minNum)) {
flag = true;
}
} else {
minNum = StringUtils.isBlank(minNum) == true ? "0" : minNum;
if (password.length() >= Integer.parseInt(minNum) &&
password.length() <= Integer.parseInt(maxNum)) {
flag = true;
}
}
return flag;
}
/**
* @return 包含数字 返回true
* @brief 检测密码中是否包含数字
* @param[in] password 密码字符串
*/
public static boolean checkContainDigit(String password) {
char[] chPass = password.toCharArray();
boolean flag = false;
int num_count = 0;
for (int i = 0; i < chPass.length; i++) {
if (Character.isDigit(chPass[i])) {
num_count++;
}
}
if (num_count >= 1) {
flag = true;
}
return flag;
}
/**
* @return 包含字母 返回true
* @brief 检测密码中是否包含字母(不区分大小写)
* @param[in] password 密码字符串
*/
public static boolean checkContainCase(String password) {
char[] chPass = password.toCharArray();
boolean flag = false;
int char_count = 0;
for (int i = 0; i < chPass.length; i++) {
if (Character.isLetter(chPass[i])) {
char_count++;
}
}
if (char_count >= 1) {
flag = true;
}
return flag;
}
/**
* @return 包含小写字母 返回true
* @brief 检测密码中是否包含小写字母
* @param[in] password 密码字符串
*/
public static boolean checkContainLowerCase(String password) {
char[] chPass = password.toCharArray();
boolean flag = false;
int char_count = 0;
for (int i = 0; i < chPass.length; i++) {
if (Character.isLowerCase(chPass[i])) {
char_count++;
}
}
if (char_count >= 1) {
flag = true;
}
return flag;
}
/**
* @return 包含大写字母 返回true
* @brief 检测密码中是否包含大写字母
* @param[in] password 密码字符串
*/
public static boolean checkContainUpperCase(String password) {
char[] chPass = password.toCharArray();
boolean flag = false;
int char_count = 0;
for (int i = 0; i < chPass.length; i++) {
if (Character.isUpperCase(chPass[i])) {
char_count++;
}
}
if (char_count >= 1) {
flag = true;
}
return flag;
}
/**
* @return 包含特殊符号 返回true
* @brief 检测密码中是否包含特殊符号
* @param[in] password 密码字符串
*/
public static boolean checkContainSpecialChar(String password) {
char[] chPass = password.toCharArray();
boolean flag = false;
int special_count = 0;
for (int i = 0; i < chPass.length; i++) {
if (SPECIAL_CHAR.indexOf(chPass[i]) != -1) {
special_count++;
}
}
if (special_count >= 1) {
flag = true;
}
return flag;
}
/**
* @return 含有横向连续字符串 返回true
* @brief 键盘规则匹配器 横向连续检测
* @param[in] password 密码字符串
*/
public static boolean checkLateralKeyboardSite(String password, int repetitions, boolean isLower) {
String t_password = new String(password);
//将所有输入字符转为小写
t_password = t_password.toLowerCase();
int n = t_password.length();
/**
* 键盘横向规则检测
*/
boolean flag = false;
int arrLen = KEYBOARD_HORIZONTAL_ARR.length;
int limit_num = repetitions;
for (int i = 0; i + limit_num <= n; i++) {
String str = t_password.substring(i, i + limit_num);
String distinguishStr = password.substring(i, i + limit_num);
for (int j = 0; j < arrLen; j++) {
String configStr = KEYBOARD_HORIZONTAL_ARR[j];
String revOrderStr = new StringBuffer(KEYBOARD_HORIZONTAL_ARR[j]).reverse().toString();
//检测包含字母(区分大小写)
if (isLower) {
//考虑 大写键盘匹配的情况
String UpperStr = KEYBOARD_HORIZONTAL_ARR[j].toUpperCase();
if ((configStr.indexOf(distinguishStr) != -1) || (UpperStr.indexOf(distinguishStr) != -1)) {
flag = true;
return flag;
}
//考虑逆序输入情况下 连续输入
String revUpperStr = new StringBuffer(UpperStr).reverse().toString();
if ((revOrderStr.indexOf(distinguishStr) != -1) || (revUpperStr.indexOf(distinguishStr) != -1)) {
flag = true;
return flag;
}
} else {
if (configStr.indexOf(str) != -1) {
flag = true;
return flag;
}
//考虑逆序输入情况下 连续输入
if (revOrderStr.indexOf(str) != -1) {
flag = true;
return flag;
}
}
}
}
return flag;
}
/**
* @param password 字符串
* @param repetitions 重复次数
* @param isLower 是否区分大小写 true:区分大小写, false:不区分大小写
* @return boolean 如1qaz,4rfv, !qaz,@WDC,zaq1 返回true
* @throws
* @Title: checkKeyboardSlantSite
* @Description: 物理键盘,斜向连接校验, 如1qaz,4rfv, !qaz,@WDC,zaq1 返回true
*/
public static boolean checkKeyboardSlantSite(String password, int repetitions, boolean isLower) {
String t_password = new String(password);
t_password = t_password.toLowerCase();
int n = t_password.length();
/**
* 键盘斜线方向规则检测
*/
boolean flag = false;
int arrLen = KEYBOARD_SLOPE_ARR.length;
int limit_num = repetitions;
for (int i = 0; i + limit_num <= n; i++) {
String str = t_password.substring(i, i + limit_num);
String distinguishStr = password.substring(i, i + limit_num);
for (int j = 0; j < arrLen; j++) {
String configStr = KEYBOARD_SLOPE_ARR[j];
String revOrderStr = new StringBuffer(KEYBOARD_SLOPE_ARR[j]).reverse().toString();
//检测包含字母(区分大小写)
if (isLower) {
//考虑 大写键盘匹配的情况
String UpperStr = KEYBOARD_SLOPE_ARR[j].toUpperCase();
if ((configStr.indexOf(distinguishStr) != -1) || (UpperStr.indexOf(distinguishStr) != -1)) {
flag = true;
return flag;
}
//考虑逆序输入情况下 连续输入
String revUpperStr = new StringBuffer(UpperStr).reverse().toString();
if ((revOrderStr.indexOf(distinguishStr) != -1) || (revUpperStr.indexOf(distinguishStr) != -1)) {
flag = true;
return flag;
}
} else {
if (configStr.indexOf(str) != -1) {
flag = true;
return flag;
}
//考虑逆序输入情况下 连续输入
if (revOrderStr.indexOf(str) != -1) {
flag = true;
return flag;
}
}
}
}
return flag;
}
/**
* @param password 字符串
* @param repetitions 连续个数
* @param isLower 是否区分大小写 true:区分大小写, false:不区分大小写
* @return boolean 含有a-z,z-a连续字符串 返回true
* @throws
* @Title: checkSequentialChars
* @Description: 评估a-z,z-a这样的连续字符,
*/
public static boolean checkSequentialChars(String password, int repetitions, boolean isLower) {
String t_password = new String(password);
boolean flag = false;
int limit_num = repetitions;
int normal_count = 0;
int reversed_count = 0;
//检测包含字母(区分大小写)
if (!isLower) {
t_password = t_password.toLowerCase();
}
int n = t_password.length();
char[] pwdCharArr = t_password.toCharArray();
for (int i = 0; i + limit_num <= n; i++) {
normal_count = 0;
reversed_count = 0;
for (int j = 0; j < limit_num - 1; j++) {
if (pwdCharArr[i + j + 1] - pwdCharArr[i + j] == 1) {
normal_count++;
if (normal_count == limit_num - 1) {
return true;
}
}
if (pwdCharArr[i + j] - pwdCharArr[i + j + 1] == 1) {
reversed_count++;
if (reversed_count == limit_num - 1) {
return true;
}
}
}
}
return flag;
}
/**
* @param password 字符串
* @param repetitions 重复次数
* @return boolean 返回类型
* @throws
* @Title: checkSequentialSameChars
* @Description: 验证键盘上是否存在多个连续重复的字符, 如!!!!, qqqq, 1111, ====, AAAA返回true
*/
public static boolean checkSequentialSameChars(String password, int repetitions) {
String t_password = new String(password);
int n = t_password.length();
char[] pwdCharArr = t_password.toCharArray();
boolean flag = false;
int limit_num = repetitions;
int count = 0;
for (int i = 0; i + limit_num <= n; i++) {
count = 0;
for (int j = 0; j < limit_num - 1; j++) {
if (pwdCharArr[i + j] == pwdCharArr[i + j + 1]) {
count++;
if (count == limit_num - 1) {
return true;
}
}
}
}
return flag;
}
/**
* 校验密码必须为大写字母、小写字母、数字、特殊符号至少三种以上组成
*
* @param password
* @return
*/
public static boolean checkLeastThreeCategoryChar(String password) {
boolean flag = false;
int num = 0;
if (checkContainDigit(password)) {
num++;
}
if (checkContainLowerCase(password)) {
num++;
}
if (checkContainUpperCase(password)) {
num++;
}
if (checkContainSpecialChar(password)) {
num++;
}
if (num >= 3) {
flag = true;
}
return flag;
}
/**
* 检测密码中是否包含用户名
*
* @param password
* @param userName
* @param phone
* @return
*/
public static boolean checkUserNameAndPassword(String password, String userName, String phone) {
boolean flag = false;
password = password.toLowerCase();
userName = userName.toLowerCase();
if (password.equals(userName)) {
flag = false;
} else if (password.contains(userName)) {
flag = false;
} else if (userName.contains(password)) {
flag = false;
} else if (password.equals(phone)) {
flag = false;
} else if (password.contains(phone)) {
flag = false;
} else if (phone.contains(password)) {
flag = false;
} else {
flag = true;
}
return flag;
}
}
java 密码安全策略,杜绝弱口令
最新推荐文章于 2024-08-19 16:01:20 发布