密码复杂度校验工具类
要求密码长度至少10位,且必须包含大小写字母、数字和特殊字符中的至少三种类型
/**
* 密码长度至少10位,必须包含大小写字母、数字和特殊字符中的至少三种类型
* @param password
* @return
*/
public static boolean isStrongPassword(String password) {
// 密码长度至少为8个字符
if (password.length() < 10) {
return false;
}
// 包含大写字母、小写字母、数字和特殊字符中的至少三种类型
int complexityCount = 0;
Pattern uppercasePattern = Pattern.compile("[A-Z]");
Matcher uppercaseMatcher = uppercasePattern.matcher(password);
if (uppercaseMatcher.find()) {
complexityCount++;
}
Pattern lowercasePattern = Pattern.compile("[a-z]");
Matcher lowercaseMatcher = lowercasePattern.matcher(password);
if (lowercaseMatcher.find()) {
complexityCount++;
}
Pattern digitPattern = Pattern.compile("\\d");
Matcher digitMatcher = digitPattern.matcher(password);
if (digitMatcher.find()) {
complexityCount++;
}
Pattern specialCharPattern = Pattern.compile("[!@#$%^&*(),.?\":{}|<>]");
Matcher specialCharMatcher = specialCharPattern.matcher(password);
if (specialCharMatcher.find()) {
complexityCount++;
}
if (complexityCount >= 3) {
return true;
} else {
return false;
}
}