使用spring boot 实现验证码源代码,直接复制使用
public void getCodeImage(HttpServletResponse response) {
RandomGenerator randomGenerator = new RandomGenerator("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",5);
LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(100, 30,5,150);
response.setContentType("image/jpeg");
response.setHeader("Pragma", "No-cache");
try {
lineCaptcha.setGenerator(randomGenerator);
lineCaptcha.write(response.getOutputStream());
log.info("生成的验证码:{}", lineCaptcha.getCode());
String yzmid = IdUtil.fastSimpleUUID();
redisUtil.set(Constants.YZM_PREFIX_NAME+yzmid,lineCaptcha.getCode());
redisUtil.expire(Constants.YZM_PREFIX_NAME+yzmid, Constants.EXPIRE_TIME / 1000);
response.getOutputStream().close();
} catch (IOException e) {
e.printStackTrace();
}
}