效果图:
引入依赖:
<!--hutool工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.15</version>
</dependency>
核心代码
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import cn.hutool.captcha.ShearCaptcha;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.RandomUtil;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.UUID;
import static cn.hutool.core.img.ImgUtil.toBufferedImage;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/performance")
public class PerformanceController {
/**
* 获取扭曲干扰的验证码
* @param response
*/
@GetMapping("/createShearCaptcha")
public void getShearCaptcha(HttpServletResponse response)
{
//定义图形验证码的长、宽、验证码字符数、干扰线宽度
ShearCaptcha shearCaptcha = CaptchaUtil.createShearCaptcha(150, 50,4,3);
//设置背景颜色
shearCaptcha.setBackground(new Color(249, 251, 220));
//生成四位验证码
String code = RandomUtil.randomNumbers(4);
//生成验证码图片
Image image = shearCaptcha.createImage(code);
//返回验证码信息
responseCode(response, code, image);
}
/**
* 获取圆圈干扰的验证码
* @param response
*/
@GetMapping("/createCircleCaptcha")
public void getCircleCaptcha(HttpServletResponse response)
{
//定义图形验证码的长、宽、验证码位数、干扰圈圈数量
CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(150, 50,4,30);
//设置背景颜色
circleCaptcha.setBackground(new Color(249, 251, 220));
//生成四位验证码
String code = RandomUtil.randomNumbers(4);
Image image = circleCaptcha.createImage(code);
//返回验证码信息
responseCode(response, code, image);
}
private static void responseCode(HttpServletResponse response, String code, Image image) {
response.setContentType("image/jpeg");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
String uuidStr = UUID.randomUUID().toString().replace("-", "");
response.setHeader("verifyCodeUuid",uuidStr);
try {
BufferedImage bufferedImage = toBufferedImage(image);
// 创建一个ByteArrayOutputStream,用于存储图片数据
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// 写入图片数据到ByteArrayOutputStream
ImageIO.write(bufferedImage, "jpeg", baos);
// 将ByteArrayOutputStream转换为ByteArrayInputStream
byte[] imageInBytes = baos.toByteArray();
//输出
IoUtil.write(response.getOutputStream(), true, imageInBytes);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
在这里响应的时候,我在请求头中加了个verifyCodeUuid (验证码唯一标识), 是用于后续 校验验证码使用的,可以将验证码存在redis ,存储的key的话可以使用 【前缀+请求ip+verifyCodeUuid 】 形式来进行存储,后续获取校验的时候也是一样的key,前端就需要将verifyCodeUuid 和用户输入的验证码一并传过来。这是一种简单的实现,具体 实现还需看具体业务,这里供参考。
@GetMapping("/createShearCaptchaV2")
public void getShearCaptchaV2(HttpServletResponse response)
{
//定义图形验证码的长、宽、验证码字符数、干扰线宽度
ShearCaptcha shearCaptcha = CaptchaUtil.createShearCaptcha(150, 50,4,3);
response.setContentType("image/jpeg");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
String uuidStr = UUID.randomUUID().toString().replace("-", "");
log.info("生成验证码 uuidStr:{} ,code:{}",uuidStr, shearCaptcha.getCode());
response.setHeader("verifyCodeUuid",uuidStr);
try {
//输出
shearCaptcha.write(response.getOutputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
}