Spring Boot集成Kaptcha实现图片验证码

àì夳堔傛蜴生んèń 2022-03-11 07:50 417阅读 0赞

废话不多说,直接开始

首先,在pom.xml用引入Kaptcha依赖

  1. <!-- 验证码 -->
  2. <dependency>
  3. <groupId>com.github.penggle</groupId>
  4. <artifactId>kaptcha</artifactId>
  5. <version>2.3.2</version>
  6. </dependency>

由于springBoot没有xml配置,是采用配置类来实现的自定义配置,所以创建一个自定义配置类,这个配置类就是用来定义验证码各自参数的:

  1. //验证码配置类
  2. @Component
  3. public class KaptchaConfig {
  4. @Bean
  5. public DefaultKaptcha getDefaultKaptcha(){
  6. DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
  7. Properties properties = new Properties();
  8. properties.setProperty("kaptcha.border", "yes"); //是否有边框
  9. properties.setProperty("kaptcha.border.color", "105,179,90"); //边框颜色
  10. properties.setProperty("kaptcha.textproducer.font.color", "green");//字体颜色
  11. properties.setProperty("kaptcha.noise.color", "0,255,255"); //干扰颜色
  12. properties.setProperty("kaptcha.image.width", "110"); //宽度
  13. properties.setProperty("kaptcha.image.height", "40"); //高度
  14. properties.setProperty("kaptcha.textproducer.font.size", "30"); //字体大小
  15. properties.setProperty("kaptcha.textproducer.char.space", "3"); //文字间距
  16. properties.setProperty("kaptcha.session.key", "code");
  17. properties.setProperty("kaptcha.textproducer.char.length", "4"); //验证码个数
  18. properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑"); //字体样式
  19. Config config = new Config(properties);
  20. defaultKaptcha.setConfig(config);
  21. return defaultKaptcha;
  22. }
  23. }

Kaptcha参数设置如下:























































































































Constant 描述 默认值
kaptcha.border 图片边框,合法值:yes , no yes
kaptcha.border.color 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. black
kaptcha.border.thickness 边框厚度,合法值:>0 1
kaptcha.image.width 图片宽 200
kaptcha.image.height 图片高 50
kaptcha.producer.impl 图片实现类 com.google.code.kaptcha.impl.DefaultKaptcha
kaptcha.textproducer.impl 文本实现类 com.google.code.kaptcha.text.impl.DefaultTextCreator
kaptcha.textproducer.char.string 文本集合,验证码值从此集合中获取 abcde2345678gfynmnpwx
kaptcha.textproducer.char.length 验证码长度 5
kaptcha.textproducer.font.names 字体 Arial, Courier
kaptcha.textproducer.font.size 字体大小 40px
kaptcha.textproducer.font.color 字体颜色,合法值: r,g,b  或者 white,black,blue. black
kaptcha.textproducer.char.space 文字间隔 2
kaptcha.noise.impl 干扰实现类 com.google.code.kaptcha.impl.DefaultNoise
kaptcha.noise.color 干扰颜色,合法值: r,g,b 或者 white,black,blue. black
kaptcha.obscurificator.impl 图片样式:
水纹com.google.code.kaptcha.impl.WaterRipple
鱼眼com.google.code.kaptcha.impl.FishEyeGimpy
阴影com.google.code.kaptcha.impl.ShadowGimpy
com.google.code.kaptcha.impl.WaterRipple
kaptcha.background.impl 背景实现类 com.google.code.kaptcha.impl.DefaultBackground
kaptcha.background.clear.from 背景颜色渐变,开始颜色 light grey
kaptcha.background.clear.to 背景颜色渐变,结束颜色 white
kaptcha.word.impl 文字渲染器 com.google.code.kaptcha.text.impl.DefaultWordRenderer
kaptcha.session.key session key KAPTCHA_SESSION_KEY
kaptcha.session.date session date KAPTCHA_SESSION_DATE

配置完成后,需要定义一个接口用来获取验证码图片的数据,然后把图片验证码的值保存在session中,在用户登录时,可以判断输入的验证码和session中的验证码是否相同。

  1. /**
  2. * 获取验证码 的 请求路径
  3. *
  4. * @param httpServletRequest
  5. * @param httpServletResponse
  6. * @throws Exception
  7. */
  8. @RequestMapping("/defaultKaptcha")
  9. @ApiOperation(value = "后台-获取图片验证码接口")
  10. public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
  11. throws Exception {
  12. byte[] captchaChallengeAsJpeg = null;
  13. ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();
  14. try {
  15. // 生产验证码字符串并保存到session中
  16. String createText = captchaProducer.createText();
  17. httpServletRequest.getSession().setAttribute("vrifyCode", createText);
  18. log.info("验证码值:" + httpServletRequest.getSession().getAttribute("vrifyCode"));
  19. // 使用生产的验证码字符串返回一个BufferedImage对象并转为byte写入到byte数组中
  20. BufferedImage challenge = captchaProducer.createImage(createText);
  21. ImageIO.write(challenge, "jpg", jpegOutputStream);
  22. } catch (IllegalArgumentException e) {
  23. httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
  24. return;
  25. }
  26. // 定义response输出类型为image/jpeg类型,使用response输出流输出图片的byte数组
  27. captchaChallengeAsJpeg = jpegOutputStream.toByteArray();
  28. httpServletResponse.setHeader("Cache-Control", "no-store");
  29. httpServletResponse.setHeader("Pragma", "no-cache");
  30. httpServletResponse.setDateHeader("Expires", 0);
  31. httpServletResponse.setContentType("image/jpeg");
  32. ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream();
  33. responseOutputStream.write(captchaChallengeAsJpeg);
  34. responseOutputStream.flush();
  35. responseOutputStream.close();
  36. }

当前端的标签指向这个接口时,他就会获取到图片验证码,下面的方法是点击验证码图片,刷新验证码。

  1. <body>
  2. <p>111</p>
  3. <img border=0 src="http://localhost:8015/api/defaultKaptcha" id="imageMask" onclick="myReload()" style="cursor: pointer">
  4. </body>
  5. <script>
  6. function myReload() {
  7. document.getElementById("imageMask").src = document.getElementById("imageMask").src + "?nocache=" + new Date().getTime();
  8. }
  9. </script>

一切准备就绪后,启动项目查看结果:

2019030716251330.png

控制台打印的验证码值:20190307162558834.png

发表评论

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

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

相关阅读

    相关 Kaptcha生成图片验证

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