1.让我们直接上全部代码
java代码
package cn.chen.utils;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class VerifyUtil {
//这里我把code单独拿出来设置为静态变量,为的就是在controller中可以方便调用,然后进行校验
public static String code = null;
// 验证码字符集
private static final char[] CHARS = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
// 字符数量
private static final int SIZE = 4;
// 干扰线数量
private static final int LINES = 8;
// 宽度
private static final int WIDTH = 80;
// 高度
private static final int HEIGHT = 40;
// 字体大小
private static final int FONT_SIZE = 30;
/**
* 生成随机验证码及图片
*/
public static Map<String, Object> createImage() {
StringBuffer sb = new StringBuffer();
// 1.创建空白图片
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 2.获取图片画笔
Graphics graphic = image.getGraphics();
// 3.设置画笔颜色
graphic.setColor(Color.LIGHT_GRAY);
// 4.绘制矩形背景
graphic.fillRect(0, 0, WIDTH, HEIGHT);
// 5.画随机字符
Random ran = new Random();
for (int i = 0; i < SIZE; i++) {
// 取随机字符索引
int n = ran.nextInt(CHARS.length);
// 设置随机颜色
graphic.setColor(getRandomColor());
// 设置字体大小
graphic.setFont(new Font(null, Font.BOLD + Font.ITALIC, FONT_SIZE));
// 画字符
graphic.drawString(CHARS[n] + "", i * WIDTH / SIZE, HEIGHT * 2 / 3);
// 记录字符
sb.append(CHARS[n]);
}
// 6.画干扰线
for (int i = 0; i < LINES; i++) {
// 设置随机颜色
graphic.setColor(getRandomColor());
// 随机画线
graphic.drawLine(ran.nextInt(WIDTH), ran.nextInt(HEIGHT), ran.nextInt(WIDTH), ran.nextInt(HEIGHT));
}
// 7.返回验证码和图片
Map<String, Object> map = new HashMap<>();
//验证码赋值给code
code = sb.toString();
//图片
map.put("image", image);
return map;
}
/**
* 随机取色
*/
public static Color getRandomColor() {
Random ran = new Random();
return new Color(ran.nextInt(256), ran.nextInt(256), ran.nextInt(256));
}
}
控制器代码
package cn.chen.controller;
import cn.chen.utils.VerifyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.OutputStream;
import java.util.Map;
@Controller
public class UntilController {
@GetMapping("/verifyCode")
public void getCode(HttpServletResponse response) throws Exception {
Map<String, Object> map = VerifyUtil.createImage();
String code = VerifyUtil.code;//这里就可以通过类名.属性的方法获取生成的验证码啦
System.out.println("验证码"+code);
//将图片输出给浏览器
BufferedImage image = (BufferedImage) map.get("image");
response.setContentType("image/png");
OutputStream os = response.getOutputStream();
ImageIO.write(image, "png", os);
}
}
前端html页面代码
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Titletitle>
head>
<body>
<h1>点击图片切换验证码h1>
<img id="img-verify" src="verifyCode" alt="更换验证码" height="36" onclick="getVerify()">
body>
<script>
//获取验证码
function getVerify() {
document.getElementById("img-verify").src = "/verifyCode?" + Math.random();
}
script>
html>
2.笔者解析:
这里通过验证码工具类生成随机的0-9与a-z的验证码,然后储存到了 静态变量code中
在控制器中获取
3.希望可以帮到你,欢迎评论,一起学习
4.原文来自:https://blog.csdn.net/qwqw3333333/article/details/86220733