工具类
对密码进行判断校验是否为弱密码
import java.util.*;
public class HandlePasswordUtil {
/**
* 对密码进行校验判断,弱密码将返回true,强密码返回false
*
* 如果密码是以下几种情况将返回true,即是弱密码:
* 1.如果密码是admin、
* 2.密码位数小于等于6位、
* 3.密码全部是相同的字符(如:aaaaaaa、8888888)、
* 4.密码全部是空字符、
* 5.密码全是顺序字符(如:123456、987654、bcdefg、gfedcb)
*/
public static boolean isWeekPassword(String password) {
password = password.trim();
//判断密码是不是特殊的admin
if (password.length() <= 6) {
return true;
}
char[] chPassword = password.toCharArray();
LinkedHashSet<Character> link = new LinkedHashSet<>();
StringBuilder buff = new StringBuilder();
boolean result = false;
int count = 0;
for (int i = 0; i < chPassword.length; i++) {
//判断密码是否是连续的正序或降序数字或字母
int preNumber = i == 0 ? -999 : chPassword[i - 1] - 0; //前一字符对应的ascii码
int currentNumber = chPassword[i] - 0;//当前字符对应的ascii码
int asciiNum = preNumber + currentNumber;
if ((96 <= asciiNum && asciiNum <= 114) || (130 <= asciiNum && asciiNum <= 180) || (194 <= asciiNum && asciiNum <= 244)) {
//判断密码的相邻字符是否是正序或降序的数字或字符
if (Math.abs(preNumber - currentNumber) == 1) {
count += 1;
}
}
//处理不重复的字符
if (!link.contains(chPassword[i])) {
link.add(chPassword[i]);
buff.append(chPassword[i]);
}
}
//处理空字符串和密码全是同一字符的
String resultStr = String.valueOf(buff);
if (count == chPassword.length - 1 || resultStr.length() <= 1) {
result = true;
}
return result;
}
}
测试
public class Maintest {
public static void main(String[] args) {
String password = "123456"; //弱密码
/*String password = "123456789"; //弱密码
String password = "987654"; //弱密码
String password = "987654321"; //弱密码
String password = "11111111"; //弱密码
String password = "admin"; //弱密码
String password = "9 "; //弱密码
String password = "123456x89"; //强密码*/
boolean weekPassword = HandlePasswordUtil.isWeekPassword(password);
if (weekPassword) {
System.out.println("弱密码");
} else {
System.out.println("强密码");
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/80299.html