账户初始化密码
Posted 睡你的椒
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了账户初始化密码相关的知识,希望对你有一定的参考价值。
public class RandomPwd {
/**
* 生成随机密码:6位数字 */ public static String randomSixDigitsPwd() {
Random random = new Random();
char[] arr = new char[6];
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
int randomInt = random.nextInt(10);
arr[i] = (char)(\'0\' + randomInt); // much better
sb.append(randomInt);
}
System.out.println(new String(arr));
System.out.println(sb.toString());
return new String(arr);
}
public static void main(String[] args) {
}
public static String complicatedPwd() {
Random random = new Random();
char[] arr = new char[8];
arr[nextIndex(random, arr)] = nextSpecial(random);
arr[nextIndex(random, arr)] = nextUpper(random);
arr[nextIndex(random, arr)] = nextLower(random);
arr[nextIndex(random, arr)] = nextDigit(random);
for (int i = 0; i < 8; i++) {
if (arr[i] == 0) {
arr[i] = nextChar(random);
}
}
System.out.println(new String(arr));
return new String(arr);
}
public static char nextDigit(Random random) {
return (char)(\'0\' + random.nextInt(10));
}
public static char nextUpper(Random random) {
return (char)(\'A\' + random.nextInt(26));
}
public static char nextLower(Random random) {
return (char)(\'a\' + random.nextInt(26));
}
public static char nextSpecial(Random random) {
return specialChars.charAt(random.nextInt(specialChars.length()));
}
public static int nextIndex(Random random, char[] arr) {
int index = random.nextInt(arr.length);
while (arr[index] != 0) {
index = random.nextInt(arr.length);
}
return index;
}
/**
* 生成随机密码:复杂8位 * 至少要含一个大写字段、一个小写字段、一个特殊符号、一个数字 */
public static char nextChar(Random random) {
switch (random.nextInt(4)) {
case 0: return (char)(\'A\' + random.nextInt(26));
case 1: return (char)(\'a\' + random.nextInt(26));
case 2: return (char)(\'0\' + random.nextInt(10));
default: return specialChars.charAt(random.nextInt(specialChars.length()));
}
}
private static final String specialChars = "!@#$%^&*()_+-=";
/**
* 生成随机密码:简单8位 * 字符可能由大写字母、小写字母、数字和特殊符号组织 */ public static String randomEightPwd() {
Random random = new Random();
char[] arr = new char[8];
for (int i = 0; i < arr.length; i++) {
arr[i] = nextChar(random);
}
System.out.println(new String(arr));
return new String(arr);
}
}
以上是关于账户初始化密码的主要内容,如果未能解决你的问题,请参考以下文章
Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段