/**
* 随机密码生成(包含大写字母、小写字母、数字、特殊符号至少一位)
* @param len 长度
* @return
*/
public static String getRandomPassword(int len){
String str = null;
char charr[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890*&^!#+-".toCharArray();
StringBuilder sb = new StringBuilder();
Random r = new Random();
for (int x = 0; x < len; ++x) {
sb.append(charr[r.nextInt(charr.length)]);
}
str = sb.toString();
String pattern1 = ".*[a-z]+.*";
String pattern2 = ".*[A-Z]+.*";
String pattern3 = ".*[0-9]+.*";
String pattern4 = ".*[&^!*#+-]+.*";
if (!(str.matches(pattern1) && str.matches(pattern2) &&
str.matches(pattern3) && str.matches(pattern4))) {
str = getRandomPassword(len);
}
return str;
}
生成随机密码(大小写字母、数字、特殊字符)
最新推荐文章于 2024-08-19 18:02:38 发布