图片效果展示
一.导入生成验证码工具类
package com.systop.tanzhen.vo;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
public class VerifyCode {
//宽和高
private int w = 85;
private int h = 40;
private Random r = new Random();
// 定义有那些字体
private String[] fontNames = { "宋体", "华文楷体", "黑体", "微软雅黑", "楷体_GB2312" };
// 定义有那些验证码的随机字符
private String codes = "23456789abcdefghjkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ";
// 生成背景色
private Color bgColor = new Color(0, 255, 255);
// 用于gettext 方法 获得生成的验证码文本
private String text;
// 生成随机颜色
private Color randomColor() {
int red = r.nextInt(255);
int green = r.nextInt(255);
int blue = r.nextInt(255);
return new Color(red, green, blue);
}
// 生成随机字体
private Font randomFont() {
int index = r.nextInt(fontNames.length);
String fontName = fontNames[index];
int style = r.nextInt(4);
int size = r.nextInt(5) + 24;
return new Font(fontName, style, size);
}
// 画干扰线
private void drawLine(BufferedImage image) {
int num = 3;
Graphics2D g2 = (Graphics2D) image.getGraphics();
for (int i = 0; i < num; i++) {
int x1 = r.nextInt(w);
int y1 = r.nextInt(h);
int x2 = r.nextInt(w);
int y2 = r.nextInt(h);
g2.setStroke(new BasicStroke(1.5F));// 不知道
g2.setColor(Color.white);
g2.drawLine(x1, y1, x2, y2);
}
}
// 得到codes的长度内的随机数 并使用charAt 取得随机数位置上的codes中的字符
private char randomChar() {
int index = r.nextInt(codes.length());
return codes.charAt(index);
}
// 创建一张验证码的图片
public BufferedImage createImage() {
BufferedImage image = new BufferedImage(w, h,
BufferedImage.TYPE_INT_RGB);
Graphics2D g2 = (Graphics2D) image.getGraphics();
StringBuilder sb = new StringBuilder();
// 向图中画四个字符
for (int i = 0; i < 4; i++) {
String s = randomChar() + "";
sb.append(s);
float x = i * 1.0F * w / 4;
g2.setFont(randomFont());
g2.setColor(randomColor());
g2.drawString(s, x, h - 5);
}
this.text = sb.toString();
drawLine(image);
// 返回图片
return image;
}
// 得到验证码的文本 后面是用来和用户输入的验证码 检测用
public String getText() {
return text;
}
// 定义输出的对象和输出的方向
public static void output(BufferedImage bi, OutputStream fos)
throws FileNotFoundException, IOException {
ImageIO.write(bi, "JPEG", fos);
}
}
二.编写Controller生成验证码的接口
package com.systop.tanzhen.controller;
import com.systop.tanzhen.vo.VerifyCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
@Api(tags = "验证码")
@RestController
@CrossOrigin
@RequestMapping("/code")
public class VerifyController {
@Autowired
private RedisTemplate redisTemplate;
@ApiOperation(value = "验证码生成")
@GetMapping("/verify")
public void Verify(HttpServletRequest request, HttpServletResponse response) throws IOException {
VerifyCode code = new VerifyCode();
BufferedImage image = code.createImage();
//验证码
System.err.println(code.getText());
//保存验证码到Redis,一分钟有效期
redisTemplate.opsForValue().set("code",code.getText(),1L, TimeUnit.MINUTES);
//验证码图片格式
ImageIO.write(image,"jpg",response.getOutputStream());
}
}
vue前端代码
<el-form-item
label="验证码"
label-width="70px"
prop="code"
:rules="[{ required: true, message: '请输入验证码', trigger: 'blur' }]"
>
<div class="code">
<el-input
style="width: 190px"
v-model="dynamicValidateForm.code"
class="code1"
></el-input>
<img
:src="captchaImage"
alt="图片无法加载"
style=""
@click="changeVerify()"
/>
</div>
</el-form-item>
changeVerify() {
axios
.get("http://localhost:8888/code/verify", {
responseType: "arraybuffer",
})
.then((response) => {
console.log(response);
const imageBase64 = btoa(
new Uint8Array(response.data).reduce(
(data, byte) => data + String.fromCharCode(byte),
""
)
);
this.captchaImage = `data:image/jpeg;base64,${imageBase64}`;
})
.catch((error) => {
console.error(error);
});
},