Kaptcha生成验证码【SpringBoot版】

左手的ㄟ右手 2022-05-17 11:18 361阅读 0赞

话不多说直接上代码吧


首先引入Kaptcha的依赖

  1. <dependency>
  2. <groupId>com.github.penggle</groupId>
  3. <artifactId>kaptcha</artifactId>
  4. <version>2.3.2</version>
  5. </dependency>

然后这里通过java配置Kaptcha

  1. import com.google.code.kaptcha.impl.DefaultKaptcha;
  2. import com.google.code.kaptcha.util.Config;
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.stereotype.Component;
  5. import java.util.Properties;
  6. /** * - @author IBean - @date 2018/8/9 **/
  7. @Component
  8. public class KaptchaConfig {
  9. @Bean
  10. public DefaultKaptcha getDefaultKaptcha() {
  11. com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
  12. Properties properties = new Properties();
  13. properties.setProperty("kaptcha.border", "yes");
  14. properties.setProperty("kaptcha.border.color", "105,179,90");
  15. properties.setProperty("kaptcha.image.width", "125");
  16. properties.setProperty("kaptcha.image.height", "38");
  17. properties.setProperty("kaptcha.textproducer.char.length", "4");
  18. properties.setProperty("kaptcha.textproducer.font.size", "32");
  19. properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
  20. properties.setProperty("kaptcha.textproducer.font.color", "white");
  21. properties.setProperty("kaptcha.noise.color", "black");
  22. properties.setProperty("kaptcha.background.clear.from", "102,153,153");
  23. properties.setProperty("kaptcha.background.clear.to", "183,234,112");
  24. properties.setProperty("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.ShadowGimpy");
  25. Config config = new Config(properties);
  26. defaultKaptcha.setConfig(config);
  27. return defaultKaptcha;
  28. }
  29. }
  • 首先别忘了@Component,还有@Bean,这样后续才能通过@Autowired进行注入
  • 了解一下配置吧
    1.kaptcha.border 边框相关,就是图片有没有边
    2.kaptcha.border.color 边框颜色
    3.kaptcha.image.width 生成的图片宽度
    4.kaptcha.image.height 生成的图片高度
    5.kaptcha.textproducer.char.length 生成的验证码里字符的个数
    6.啊不想写了,我再写最后一个
    7.kaptcha.obscurificator.impl生成验证码,我理解的就是样式,有三种,水纹,鱼眼,阴影com.google.code.kaptcha.impl.ShadowGimpy,我用的就是这个阴影,感觉这个显示效果最好
  • 其余配置emmm百度一下吧


    接下来是获取验证码的部分

    private DefaultKaptcha defaultKaptcha; //上边那个那个配置

    1. @Autowired
    2. public KaptchaController(DefaultKaptcha defaultKaptcha) {
    3. this.defaultKaptcha = defaultKaptcha;
    4. }
    5. /** * 获取验证码 的 请求路径 * * @throws Exception */
    6. @RequestMapping("/vaildCode")
    7. @ResponseBody
    8. public void defaultKaptcha() {
    9. ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
    10. OutputStream out = null;
    11. try {
    12. //生产验证码字符串并保存到session中
    13. String createText = defaultKaptcha.createText();
    14. //这个createText其实可以随便写然后下面createImage都会显示在验证码的图片里,不过我没试过中文。
    15. //当然写个数学表达式或者其他骚操作都行啊,然后把答案放到session(vrifyCode)里就好啦
    16. request.getSession().setAttribute("vrifyCode", createText);
    17. //使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
    18. BufferedImage challenge = defaultKaptcha.createImage(createText);
    19. ImageIO.write(challenge, "jpg", jpegOutputStream);
    20. //定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
    21. byte[] captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
    22. response.setHeader("Cache-Control", "no-store");
    23. response.setHeader("Pragma", "no-cache");
    24. response.setDateHeader("Expires", 0);
    25. response.setContentType("image/jpeg");
    26. out = response.getOutputStream();
    27. out.write(captchaChallengeAsJpeg);
    28. out.flush();
    29. } catch (Exception e) {
    30. logger.error(e.getMessage());
    31. } finally {
    32. try {
    33. jpegOutputStream.close();
    34. if (out != null) {
    35. out.close();
    36. }
    37. System.gc();
    38. } catch (IOException e) {
    39. logger.error(e.getMessage());
    40. }
    41. }
    42. }

@ResponseBody这个必须加哦,某篇博文我简单的解释了下

  1. 前端部分
  2. <img class="vaildImg" alt="验证码" style="cursor:pointer;">
  3. //初始化的时候加载验证码
  4. $('.vaildImg').attr('src', '/vaildCode?d=' + (new Date()).getTime());
  5. //点击图片切换验证码
  6. $('.vaildImg').click(function () {
  7. $(this).attr('src', '/vaildCode?d=' + (new Date()).getTime());
  8. });

然后就好了
然后就没了
然后我也不知道该写啥了
不出意外这个就好使了
话说只有没钱才会本地生成验证码吧
不过能用就好啦b( ̄▽ ̄)d 

发表评论

表情:
评论列表 (有 0 条评论,361人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Kaptcha生成图片验证

    Kaptcha简介 kaptcha 是一个很有用的验证码生成工具。由于它是可配置的,有了它,你能够生成各种样式的验证码。 Kaptcha 是一个可高度配置的实用验证码