本文目录导读:

在Java中生成随机验证码通常有以下几种常用方法,我会按照从简单到复杂的顺序为你介绍。
纯数字验证码(4位或6位)
使用Random类生成n位数字验证码
import java.util.Random;
public class NumberCaptcha {
public static String generate(int length) {
Random random = new Random();
StringBuilder code = new StringBuilder();
for (int i = 0; i < length; i++) {
code.append(random.nextInt(10)); // 生成0-9的数字
}
return code.toString();
}
public static void main(String[] args) {
// 生成6位数字验证码
String captcha = generate(6);
System.out.println("数字验证码: " + captcha);
}
}
使用ThreadLocalRandom(性能更好)
import java.util.concurrent.ThreadLocalRandom;
public class NumberCaptchaV2 {
public static String generate(int length) {
StringBuilder code = new StringBuilder();
for (int i = 0; i < length; i++) {
code.append(ThreadLocalRandom.current().nextInt(10));
}
return code.toString();
}
}
字母数字混合验证码(推荐)
基础版本
import java.util.Random;
public class MixedCaptcha {
// 验证码字符集合(去除了容易混淆的字符)
private static final String CHARS =
"ABCDEFGHJKLMNPQRSTUVWXYZ23456789abcdefghjkmnpqrstuvwxyz";
public static String generate(int length) {
Random random = new Random();
StringBuilder code = new StringBuilder();
for (int i = 0; i < length; i++) {
int index = random.nextInt(CHARS.length());
code.append(CHARS.charAt(index));
}
return code.toString();
}
public static void main(String[] args) {
// 生成4位混合验证码
String captcha = generate(4);
System.out.println("混合验证码: " + captcha);
}
}
增强版本(保证包含数字和字母)
import java.util.Random;
public class EnhancedCaptcha {
private static final String LETTERS = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz";
private static final String DIGITS = "23456789";
public static String generate(int length) {
if (length < 2) {
throw new IllegalArgumentException("验证码长度至少为2");
}
Random random = new Random();
StringBuilder code = new StringBuilder();
// 确保至少包含1个数字和1个字母
code.append(LETTERS.charAt(random.nextInt(LETTERS.length())));
code.append(DIGITS.charAt(random.nextInt(DIGITS.length())));
// 补齐剩余长度
String allChars = LETTERS + DIGITS;
for (int i = 2; i < length; i++) {
code.append(allChars.charAt(random.nextInt(allChars.length())));
}
// 打乱顺序
return shuffle(code.toString(), random);
}
// 打乱字符串顺序
private static String shuffle(String str, Random random) {
char[] chars = str.toCharArray();
for (int i = chars.length - 1; i > 0; i--) {
int j = random.nextInt(i + 1);
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
return new String(chars);
}
}
数学运算验证码(防机器人)
import java.util.Random;
public class MathCaptcha {
// 生成数学表达式和结果
public static CaptchaResult generate() {
Random random = new Random();
int a = random.nextInt(100);
int b = random.nextInt(100);
int operator = random.nextInt(3);
String expression;
int result;
switch (operator) {
case 0: // 加法
expression = a + " + " + b;
result = a + b;
break;
case 1: // 减法(确保结果为正数)
if (a < b) {
int temp = a;
a = b;
b = temp;
}
expression = a + " - " + b;
result = a - b;
break;
default: // 乘法(简化为个位数)
a = random.nextInt(9) + 1;
b = random.nextInt(9) + 1;
expression = a + " × " + b;
result = a * b;
break;
}
return new CaptchaResult(expression, result);
}
// 结果封装类
public static class CaptchaResult {
private String expression;
private int result;
public CaptchaResult(String expression, int result) {
this.expression = expression;
this.result = result;
}
public String getExpression() { return expression; }
public int getResult() { return result; }
}
public static void main(String[] args) {
CaptchaResult captcha = generate();
System.out.println("数学题: " + captcha.getExpression() + " = ?");
System.out.println("答案: " + captcha.getResult());
}
}
图形验证码(带干扰)
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.Random;
public class ImageCaptcha {
private static final int WIDTH = 120;
private static final int HEIGHT = 40;
private static final String CHARS = "ABCDEFGHJKLMNPQRSTUVWXYZ23456789";
public static BufferedImage createImage(String text) {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT,
BufferedImage.TYPE_INT_RGB);
Graphics2D g = image.createGraphics();
Random random = new Random();
// 设置背景色
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
// 绘制干扰线
g.setColor(Color.GRAY);
g.setStroke(new BasicStroke(2));
for (int i = 0; i < 5; i++) {
int x1 = random.nextInt(WIDTH);
int y1 = random.nextInt(HEIGHT);
int x2 = random.nextInt(WIDTH);
int y2 = random.nextInt(HEIGHT);
g.drawLine(x1, y1, x2, y2);
}
// 绘制验证码
g.setFont(new Font("Arial", Font.BOLD, 28));
char[] chars = text.toCharArray();
for (int i = 0; i < chars.length; i++) {
// 随机颜色
g.setColor(new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
// 随机旋转和位置偏移
int x = 20 + i * 20 + random.nextInt(10);
int y = 30 + random.nextInt(10);
g.drawString(String.valueOf(chars[i]), x, y);
}
// 添加噪点
for (int i = 0; i < 100; i++) {
int x = random.nextInt(WIDTH);
int y = random.nextInt(HEIGHT);
g.setColor(new Color(random.nextInt(256),
random.nextInt(256), random.nextInt(256)));
g.drawLine(x, y, x, y);
}
g.dispose();
return image;
}
}
实用工具类(综合版本)
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;
public class CaptchaUtil {
private static final String LETTERS = "ABCDEFGHJKLMNPQRSTUVWXYZ";
private static final String DIGITS = "23456789";
private static final String MIXED = LETTERS + DIGITS;
/**
* 生成纯数字验证码
*/
public static String number(int length) {
StringBuilder code = new StringBuilder();
for (int i = 0; i < length; i++) {
code.append(ThreadLocalRandom.current().nextInt(10));
}
return code.toString();
}
/**
* 生成字母数字混合验证码
*/
public static String mixed(int length) {
StringBuilder code = new StringBuilder();
for (int i = 0; i < length; i++) {
int index = ThreadLocalRandom.current().nextInt(MIXED.length());
code.append(MIXED.charAt(index));
}
return code.toString();
}
/**
* 生成大写字母验证码
*/
public static String upperCase(int length) {
StringBuilder code = new StringBuilder();
for (int i = 0; i < length; i++) {
int index = ThreadLocalRandom.current().nextInt(LETTERS.length());
code.append(LETTERS.charAt(index));
}
return code.toString();
}
/**
* 判断验证码是否有效(不区分大小写)
*/
public static boolean validate(String input, String expected) {
if (input == null || expected == null) {
return false;
}
return input.equalsIgnoreCase(expected);
}
}
使用示例
public class Main {
public static void main(String[] args) {
System.out.println("6位数字验证码: " + CaptchaUtil.number(6));
System.out.println("4位混合验证码: " + CaptchaUtil.mixed(4));
System.out.println("验证码验证: " + CaptchaUtil.validate("AbC1", "abc1"));
// 生成验证码并验证
String captcha = CaptchaUtil.mixed(4);
System.out.println("生成的验证码: " + captcha);
System.out.println("用户输入: aB23");
System.out.println("验证结果: " + CaptchaUtil.validate("aB23", captcha));
}
}
建议:
- 生产环境建议使用混合验证码(字母+数字)
- 避免使用容易混淆的字符(如0和O,1和l,I)
- 验证码通常4-6位即可
- 注意验证码的时效性(通常5分钟内有效)
- 验证时应该不区分大小写
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播