java后端验证码
时间: 2023-09-03 21:11:00 浏览: 80
可以使用Java后端生成验证码的方法,以下是一个简单的示例:
```java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
/**
* 生成验证码的工具类
*/
public class CaptchaUtil {
private static final int WIDTH = 100; // 验证码图片的宽度
private static final int HEIGHT = 40; // 验证码图片的高度
private static final int CODE_LENGTH = 4; // 验证码字符个数
private static final int LINE_COUNT = 20; // 干扰线数量
// 随机生成的字符集合
private static final String CODE_SET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
// 随机数生成器
private static final Random random = new Random();
/**
* 生成验证码图片,并将验证码字符串存入session中
*
* @param request
* @param response
* @return 验证码字符串
* @throws Exception
*/
public static String generateCaptcha(HttpServletRequest request, HttpServletResponse response) throws Exception {
// 设置响应头信息
response.setContentType("image/jpeg");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
// 创建一个图像对象
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 获取图形上下文对象
Graphics g = image.getGraphics();
// 设置背景色
g.setColor(Color.WHITE);
g.fillRect(0, 0, WIDTH, HEIGHT);
// 设置字体
g.setFont(new Font("Arial", Font.PLAIN, 20));
// 生成随机字符串
StringBuilder code = new StringBuilder();
for (int i = 0; i < CODE_LENGTH; i++) {
int index = random.nextInt(CODE_SET.length());
char c = CODE_SET.charAt(index);
code.append(c);
g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
g.drawString(String.valueOf(c), 20 * i + 10, 25);
}
// 绘制干扰线
for (int i = 0; i < LINE_COUNT; i++) {
g.setColor(new Color(random.nextInt(255), random.nextInt(255), random.nextInt(255)));
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.dispose();
// 将验证码存入session中
HttpSession session = request.getSession();
session.setAttribute("captchaCode", code.toString());
// 将图像输出到客户端
ImageIO.write(image, "JPEG", response.getOutputStream());
// 返回生成的验证码字符串
return code.toString();
}
/**
* 校验验证码
*
* @param request
* @param code
* 用户输入的验证码
* @return true-验证码正确,false-验证码错误
*/
public static boolean validateCaptcha(HttpServletRequest request, String code) {
HttpSession session = request.getSession();
String captchaCode = (String) session.getAttribute("captchaCode");
return captchaCode != null && captchaCode.equalsIgnoreCase(code);
}
}
```
使用方法:
在需要生成验证码的地方调用 `generateCaptcha` 方法即可,例如:
```java
// 生成验证码
String captchaCode = CaptchaUtil.generateCaptcha(request, response);
```
在需要验证验证码的地方调用 `validateCaptcha` 方法即可,例如:
```java
// 校验验证码
if (CaptchaUtil.validateCaptcha(request, code)) {
// 验证码正确
} else {
// 验证码错误
}
```
其中 `code` 是用户输入的验证码字符串。
阅读全文