第一步:pom文件中添加
<dependency>
<groupId>com.github.penggle</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
第二步:添加配置类KaptchaConfig
@Configuration
public class KaptchaConfig {
@Bean
public DefaultKaptcha getDefaultKaptcha() {
com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
Properties properties = new Properties();
// 图片边框
properties.setProperty("kaptcha.border", "yes");
// 边框颜色
properties.setProperty("kaptcha.border.color", "105,179,90");
// 字体颜色
properties.setProperty("kaptcha.textproducer.font.color", "blue");
// 图片宽
properties.setProperty("kaptcha.image.width", "110");
// 图片高
properties.setProperty("kaptcha.image.height", "40");
// 字体大小
properties.setProperty("kaptcha.textproducer.font.size", "30");
// session key
properties.setProperty("kaptcha.session.key", "code");
// 验证码长度
properties.setProperty("kaptcha.textproducer.char.length", "4");
// 字体
properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
Config config = new Config(properties);
defaultKaptcha.setConfig(config);
return defaultKaptcha;
}
}
第三步:CaptchaController
@RestController
@RequestMapping(path = "/controller/captcha", produces = { "application/json;charset=UTF-8" })
public class CaptchaController {
private static final Logger logger = LoggerFactory.getLogger(CaptchaController.class);
@Autowired
DefaultKaptcha defaultKaptcha;
@GetMapping("/kaptcha")
public String generateKaptcha(HttpServletRequest request, HttpServletResponse response) {
logger.debug("coming into generateKaptcha method");
String jpg_base64 = null;
try {
byte[] captchaChallengeAsJpeg = null;
ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
String createText = defaultKaptcha.createText();
request.getSession().setAttribute("vrifyCode", createText);
BufferedImage challenge = defaultKaptcha.createImage(createText);
ImageIO.write(challenge, "jpg", jpegOutputStream);
captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
BASE64Encoder encoder = new BASE64Encoder();
jpg_base64 = encoder.encodeBuffer(captchaChallengeAsJpeg).trim();
jpg_base64 = jpg_base64.replaceAll("\n", "").replaceAll("\r", "");
} catch (IOException e) {
logger.error("generate captcha failed", e);
return CommonUtil.errorJsonStr(ErrorEnum.E_514);
}
return CommonUtil.successJsonStr(jpg_base64);
}
@GetMapping("/vrifyKaptcha")
public @ResponseBody String vrifyKaptcha(HttpServletRequest httpServletRequest,
HttpServletResponse httpServletResponse) {
logger.debug("coming into generateKaptcha method");
String captchaId = (String) httpServletRequest.getSession().getAttribute("vrifyCode");
String parameter = httpServletRequest.getParameter("vrifyCode");
if (!captchaId.equals(parameter)) {
logger.error("vrity captcha failed");
return CommonUtil.errorJsonStr(ErrorEnum.E_511);
} else {
logger.debug("vrity captcha success");
return CommonUtil.successJsonStr();
}
}
}
springboot+kaptcha返回base64编码的jpg格式的图片,验证验证码是否正确的接口。
前端接收方式:img标签
<img id = "base64" src="https://img-blog.csdnimg.cn/2022010703424228277.gif" />