经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
Springboot通过谷歌Kaptcha 组件,生成图形验证码
来源:cnblogs  作者:王若伊_恩赐解脱  时间:2023/5/17 8:57:59  对本文有异议

图形验证码属于老生常谈了,具体细节这里就不说了。生成图形验证码的办法非常多,今天讲解一种通过Kaptcha组件快速生成图形验证码的方法。
Kaptcha是谷歌开源的一款简单实用的图形验证码组件。我个人推荐它的最大原因是容易上手,采用约定大于配置的方式,快速契合到项目中。
话不多说,我们看看该如何使用它:
一、首先我们在springboot中引入以下maven组件:

  1. 1 <dependency>
  2. 2 <groupId>com.google.code.kaptcha</groupId>
  3. 3 <artifactId>kaptcha</artifactId>
  4. 4 <version>2.3</version>
  5. 5 </dependency>

如果上述组件你一直无法拉取下来的话,也可以用如下配置:

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

二、接着我们在springboot项目中加入对应的config配置类,(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )这一步也可以配合配置中心来完成。它的作用是自动生成我们所需的config bean。
其中的配置项我们都可以选填,这里是只是一个参考,具体内容可见下文表

  1. 1 @Component
  2. 2 public class KaptchaConfig {
  3. 3
  4. 4 @Bean
  5. 5 public DefaultKaptcha getDefaultKaptcha(){
  6. 6 com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
  7. 7 Properties properties = new Properties();
  8. 8 properties.put("kaptcha.border", "no");
  9. 9 properties.put("kaptcha.textproducer.font.color", "red");
  10. 10 properties.put("kaptcha.image.width", "213");
  11. 11 properties.put("kaptcha.image.height", "88");
  12. 12 properties.put("kaptcha.textproducer.font.size", "45");
  13. 13 properties.put("kaptcha.session.key", "verifyCode");
  14. 14 properties.put("kaptcha.textproducer.char.space", "6");
  15. 15 properties.put("kaptcha.obscurificator.impl", "com.google.code.kaptcha.impl.WaterRipple");
  16. 16 // properties.put("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
  17. 17 properties.put("kaptcha.background.clear.from", "yellow");
  18. 18 properties.put("kaptcha.background.clear.to", "green");
  19. 19 Config config = new Config(properties);
  20. 20 defaultKaptcha.setConfig(config);
  21. 21
  22. 22 return defaultKaptcha;
  23. 23 }
  24. 24 }

配置表

配置名 配置作用 默认值
kaptcha.border 图片边框,合法值:yes , no yes
kaptcha.border.color 边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue. black
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

图片样式:<br />水纹 com.google.code.kaptcha.impl.WaterRipple <br />

鱼眼 com.google.code.kaptcha.impl.FishEyeGimpy <br />

阴影 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
 
三、创建好config bean之后,我们就可以创建接口来生成验证码了 
controller类,新增接口:
  1. 1 @GetMapping("/login/getVerifyCode")
  2. 2 public void getVerifyCode(String loginKey,
  3. 3 HttpServletRequest httpServletRequest,
  4. 4 HttpServletResponse httpServletResponse) {
  5. 5 try {
  6. 6 log.warn("query verify Code" + loginKey);
  7. 7 loadService.getVerifyCode(loginKey, httpServletRequest, httpServletResponse);
  8. 8 } catch (Exception e) {
  9. 9 log.error("get verify Code failed :", e);
  10. 10 }
  11. 11 }

service类,新增逻辑:

  1. 1 public void getVerifyCode(String loginKey, HttpServletRequest httpServletRequest,
  2. 2 HttpServletResponse httpServletResponse) throws IOException {
  3. 3 ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream();
  4. 4 try {
  5. 5 //生产验证码字符串并保存到session中
  6. 6 String verifyCode = captchaProducer.createText();
  7. 7 // httpServletRequest.getSession().setAttribute("verifyCode", verifyCode); // 写入会话
  8. 8 //redisCache.setVerifyInfo(loginKey, verifyCode); //写入redis
  9. 9 captchaMap.put(loginKey, verifyCode);//写入内存
  10. 10 log.warn("reset verify code key {}, code {}", loginKey, verifyCode);
  11. 11 BufferedImage challenge = captchaProducer.createImage(verifyCode);
  12. 12 ImageIO.write(challenge, "jpg", imgOutputStream);
  13. 13 } catch (IllegalArgumentException | IOException e) {
  14. 14 httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
  15. 15 return;
  16. 16 }
  17. 17 byte[] captchaOutputStream = imgOutputStream.toByteArray();
  18. 18 httpServletResponse.setHeader("Cache-Control", "no-store");
  19. 19 httpServletResponse.setHeader("Pragma", "no-cache");
  20. 20 httpServletResponse.setDateHeader("Expires", 0);
  21. 21 httpServletResponse.setContentType("image/jpeg");
  22. 22 try (ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream()) {
  23. 23 responseOutputStream.write(captchaOutputStream);
  24. 24 responseOutputStream.flush();
  25. 25 } catch (IOException ex) {
  26. 26 log.error("find ex in create a new verify Code", ex);
  27. 27 }
  28. 28 }

相关代码全部开发完毕后,我们调用接口查看效果:

这里我发现一个在本地环境使用ok,(防盗连接:本文首发自http://www.cnblogs.com/jilodream/ )但是在使用docker部署微服务时却存在的一个异常:

  1. 1 java.lang.NullPointerException: null
  2. 2 at sun.awt.FontConfiguration.getVersion(FontConfiguration.java:1264)
  3. 3 at sun.awt.FontConfiguration.readFontConfigFile(FontConfiguration.java:219)
  4. 4 at sun.awt.FontConfiguration.init(FontConfiguration.java:107)
  5. 5 .....

网上查了下,大致原因是由于我们本地使用的是oraclejdk,但是在docker中jdk的版本则采用的是openjdk。openjdk有些字体的支撑不是很友好,这里需要在制作docker镜像时添加如下语句解决:

  1. 1 RUN apk add --update font-adobe-100dpi ttf-dejavu fontconfig

 

 
 

原文链接:https://www.cnblogs.com/jilodream/p/17402275.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号