/**
* 验证码、邀请码工具类
*/
@Component
public class VerificationCodeUtil {
/**
* 生成验证码:字母+数字
* @param length 验证码长度
* @return 验证码字符串
*/
public static String getVerificationCode(int length) {
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
StringBuilder verificationCode = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
int index = random.nextInt(characters.length());
verificationCode.append(characters.charAt(index));
}
return verificationCode.toString();
}
/**
* 生成验证码:只有数字
* @param length 验证码长度
* @return 验证码字符串
*/
public static String getVerificationCodeOnlyNumber(int length) {
String characters = "0123456789";
StringBuilder verificationCode = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
int index = random.nextInt(characters.length());
verificationCode.append(characters.charAt(index));
}
return verificationCode.toString();
}
}
标签:JAVA,verificationCode,int,字母,random,验证码,length,characters
From: https://blog.csdn.net/weixin_57670212/article/details/140347156