/**
* 有效性校验
*
* @param password
* @param message
* @return
*/
public static boolean check(String password, List<String> message, boolean defaultMode) {
if (StringUtil.isEmpty(password)) {
message.add("不能为空");
return false;
}
final int len = password.length();
if (len < 8 || len > 16) {
message.add("长度不能小于8位或大于16位");
return false;
}
final int originLen = message.size();
// 有效字符数
charValidNum(password, message, defaultMode);
// 连续字符
// serieChar(password, message);
// 连续重复
// repeatChar(password, 1, 4, message);
// repeatChar(password, 2, 3, message);
// repeatChar(password, 3, 2, message);
// 对折
// symmChar(password, message);
return message.size() == originLen;
}
/**
* 有效字符数
*
* @param password
* @param msg
* @return
*/
private static boolean charValidNum(String password, List<String> msg, boolean defaultMode) {
final int len = password.length();
boolean hasDigit = false, hasUpper = false, hasLow = false, hasOther = false;
for (int idx = 0; idx < len; idx++) {
char ch = password.charAt(idx);
CharType type = getCharType(ch);// 类型
if (type == CharType.DIGIT) hasDigit = true;
if (type == CharType.UPPER) hasUpper = true;
if (type == CharType.LOWER) hasLow = true;
if (type == CharType.OTHER) hasOther = true;
}
boolean upperOrLow = defaultMode ? (hasUpper&&hasLow) : (hasUpper||hasLow);
boolean needSome = !(hasDigit && upperOrLow && hasOther);
if (needSome) msg.add("需包含大小写字母、数字、符号");
return needSome;
}
static enum CharType {
DIGIT, LOWER, UPPER, OTHER
}
private static CharType getCharType(char ch) {
if (ch >= 'a' && ch <= 'z') return CharType.LOWER;
if (ch >= 'A' && ch <= 'Z') return CharType.UPPER;
if (ch >= '0' && ch <= '9') return CharType.DIGIT;
return CharType.OTHER;
}
private static final String[] serie_tab = { //
"abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", //
"01234567890", "09876543210", //
"qwertyuiop", "QWERTYUIOP", //
"asdfghjkl", "ASDFGHJKL", //
"zxcvbnm", "ZXCVBNM" //
};
/**
* 连续的
*
* @param password
* @param msg
* @return
*/
private static boolean serieChar(String password, List<String> msg) {
final int max = 3, len = password.length(), maxIdx = len - max;
for (int idx = 0; idx <= maxIdx; idx++) {
String sub = password.substring(idx, idx + max);
for (String serie : serie_tab) {
if (serie.contains(sub)) {
msg.add("连续字符不能超过" + max + "个");
return true;
}
}
}
return false;
}
/**
* 连续重复
*
* @param password
* @param num
* @param max
* @param msg
* @return
*/
private static boolean repeatChar(String password, int num, int max, List<String> msg) {
int len = password.length() - num, same = 0;
for (int idx = num; idx < len; idx += num) {
if (password.substring(idx - num, idx).equals(password.substring(idx, idx + num))) {
same++;
if (same >= max) {
msg.add(num + "个字符不能连续重复" + max + "次");
return true;
}
} else {
same = 0;
}
}
return false;
}
/**
* 对称
*
* @param password
* @param msg
* @return
*/
private static boolean symmChar(String password, List<String> msg) {
int len = password.length();
int pos = len / 2;
String p1 = password.substring(0, pos), p2 = password.substring(len - pos);
if (p1.equals(p2)) {
msg.add("不能前后对称,如:abcabc或abc1abc");
return true;
} else {
StringBuilder sb = new StringBuilder(p2);
String p3 = sb.reverse().toString();
if (p1.equals(p3)) {
msg.add("不能前后反向对称,如:abccba或abc1cba");
return true;
}
}
return false;
}
java密码强校验
最新推荐文章于 2025-04-25 18:07:04 发布