Kaptcha图片验证码工具&SpringBoot整合kaptcha(谷歌验证码工具)实现验证码功能

Love The Way You Lie 2023-10-09 23:59 57阅读 0赞

验证码的作用

图片验证码自从诞生以来从未被抛弃,依然发出属于它所应有的光。验证码经常验证如下一些场景。

1、用户登录,防止机器人登录

2、论坛留言,防止恶意灌水

3、短信验证码发送,防止盗刷短信

Kaptcha 简介

Kaptcha 是一个可高度配置的实用验证码生成工具,可自由配置的选项如:

  • 验证码的字体
  • 验证码字体的大小
  • 验证码字体的字体颜色
  • 验证码内容的范围(数字,字母,中文汉字!)
  • 验证码图片的大小,边框,边框粗细,边框颜色
  • 验证码的干扰线
  • 验证码的样式(鱼眼样式、3D、普通模糊)

Kaptcha详细配置表

  1. 配置项:kaptcha.border
  2. 描述:图片边框,合法值:yes , no
  3. 默认值:yes
  4. 配置项:kaptcha.border.color
  5. 描述:边框颜色,合法值: r,g,b (and optional alpha) 或者 white,black,blue.
  6. 默认值:black
  7. 配置项:kaptcha.image.width
  8. 描述:图片宽
  9. 默认值:200
  10. 配置项:kaptcha.image.height
  11. 描述:图片高
  12. 默认值:50
  13. 配置项:kaptcha.producer.impl
  14. 描述:图片实现类
  15. 默认值:com.google.code.kaptcha.impl.DefaultKaptcha
  16. 配置项:kaptcha.textproducer.impl
  17. 描述:文本实现类
  18. 默认值:com.google.code.kaptcha.text.impl.DefaultTextCreator
  19. 配置项:kaptcha.textproducer.char.string
  20. 描述:文本集合,验证码值从此集合中获取
  21. 默认值:abcde2345678gfynmnpwx
  22. 配置项:kaptcha.textproducer.char.length
  23. 描述:验证码长度
  24. 默认值:5
  25. 配置项:kaptcha.textproducer.font.names
  26. 描述:字体
  27. 默认值:Arial, Courier
  28. 配置项:kaptcha.textproducer.font.size
  29. 描述:字体大小
  30. 默认值:40px.
  31. 配置项:kaptcha.textproducer.font.color
  32. 描述:字体颜色,合法值: r,g,b 或者 white,black,blue.
  33. 默认值:black
  34. 配置项:kaptcha.textproducer.char.space
  35. 描述:文字间隔
  36. 默认值:2
  37. 配置项:kaptcha.noise.impl
  38. 描述:干扰实现类
  39. 默认值:com.google.code.kaptcha.impl.DefaultNoise
  40. 配置项:kaptcha.noise.color
  41. 描述:干扰 颜色,合法值: r,g,b 或者 white,black,blue.
  42. 默认值:black
  43. 配置项:kaptcha.obscurificator.impl
  44. 描述:图片样式,
  45. 水纹 com.google.code.kaptcha.impl.WaterRipple
  46. 鱼眼 com.google.code.kaptcha.impl.FishEyeGimpy
  47. 阴影 com.google.code.kaptcha.impl.ShadowGimpy
  48. 默认值:com.google.code.kaptcha.impl.WaterRipple
  49. 配置项:kaptcha.background.impl
  50. 描述:背景实现类
  51. 默认值:com.google.code.kaptcha.impl.DefaultBackground
  52. 配置项:kaptcha.background.clear.from
  53. 描述:背景颜色渐变,开始颜色
  54. 默认值:light grey
  55. 配置项:kaptcha.background.clear.to
  56. 描述:背景颜色渐变, 结束颜色
  57. 默认值:white
  58. 配置项:kaptcha.word.impl
  59. 描述:文字渲染器
  60. 默认值:com.google.code.kaptcha.text.impl.DefaultWordRenderer
  61. 配置项:kaptcha.session.key
  62. 描述:session key
  63. 默认值:KAPTCHA_SESSION_KEY
  64. 配置项:kaptcha.session.date
  65. 描述:session date
  66. 默认值:KAPTCHA_SESSION_DATE

SpringBoot整合 Kaptcha

1、pom.xml文件中引入

  1. <!-- https://mvnrepository.com/artifact/com.oopsguy.kaptcha/kaptcha-spring-boot-autoconfigure -->
  2. <dependency>
  3. <groupId>com.oopsguy.kaptcha</groupId>
  4. <artifactId>kaptcha-spring-boot-autoconfigure</artifactId>
  5. <version>1.0.0-beta-2</version>
  6. </dependency>

2、配置DefaultKaptcha

  1. import java.util.Properties;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import com.google.code.kaptcha.impl.DefaultKaptcha;
  5. import com.google.code.kaptcha.util.Config;
  6. @Configuration
  7. public class ConfigBean {
  8. @Bean
  9. public DefaultKaptcha getDefaultKaptcha(){
  10. DefaultKaptcha dk = new DefaultKaptcha();
  11. Properties properties = new Properties();
  12. properties.put("kaptcha.border", "yes");
  13. properties.put("kaptcha.border.color","105,179,90");
  14. properties.put("kaptcha.textproducer.font.color","blue");
  15. properties.put("kaptcha.image.width","125");
  16. properties.put("kaptcha.image.height","45");
  17. properties.put("kaptcha.textproducer.font.size","45");
  18. properties.put("kaptcha.session.key","code");
  19. properties.put("kaptcha.textproducer.char.length","4");
  20. properties.put("kaptcha.textproducer.font.names","宋体,楷体,微软雅黑");
  21. Config config = new Config(properties );
  22. dk.setConfig(config);
  23. return dk;
  24. }
  25. }

3、编写controller

  1. package com.piano.student.controller;
  2. import java.awt.image.BufferedImage;
  3. import java.io.IOException;
  4. import javax.imageio.ImageIO;
  5. import javax.servlet.ServletOutputStream;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Controller;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.servlet.ModelAndView;
  13. import com.google.code.kaptcha.Constants;
  14. import com.google.code.kaptcha.impl.DefaultKaptcha;
  15. @Controller
  16. public class KaptchaController {
  17. @Autowired
  18. private DefaultKaptcha captchaProducer;
  19. @RequestMapping(value = "verification", method = RequestMethod.GET)
  20. public ModelAndView verification(HttpServletRequest request, HttpServletResponse response) throws IOException {
  21. response.setDateHeader("Expires", 0);
  22. // Set standard HTTP/1.1 no-cache headers.
  23. response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
  24. // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
  25. response.addHeader("Cache-Control", "post-check=0, pre-check=0");
  26. // Set standard HTTP/1.0 no-cache header.
  27. response.setHeader("Pragma", "no-cache");
  28. // return a jpeg
  29. response.setContentType("image/jpeg");
  30. // create the text for the image
  31. String capText = captchaProducer.createText();
  32. // store the text in the session
  33. request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
  34. // create the image with the text
  35. BufferedImage bi = captchaProducer.createImage(capText);
  36. ServletOutputStream out = response.getOutputStream();
  37. // write the data out
  38. ImageIO.write(bi, "jpg", out);
  39. try {
  40. out.flush();
  41. } finally {
  42. out.close();
  43. }
  44. return null;
  45. }
  46. }

4、访问http://127.0.0.1:8083/verification

Kaptcha图片验证码工具\_配置项



介绍:

kaptcha是Google提供的一个图形验证码插件,有了它,你可以通过简单的配置生成各种样式的验证码。

1:SpringBoot引入kaptcha的依赖

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

2:编写kaptcha配置类

  1. package simulationvirtual.VirtualExperiment.config;
  2. import com.google.code.kaptcha.Producer;
  3. import com.google.code.kaptcha.impl.DefaultKaptcha;
  4. import com.google.code.kaptcha.util.Config;
  5. import org.springframework.context.annotation.Bean;
  6. import org.springframework.context.annotation.Configuration;
  7. import java.util.Properties;
  8. @Configuration
  9. public class KaptchaConfig {
  10. /**
  11. * Kaptcha图形验证码工具配置类
  12. * @author: Xiongch
  13. * @param:
  14. * @return: com.google.code.kaptcha.Producer
  15. * @date: 2022/9/9 15:47
  16. */
  17. @Bean
  18. public Producer kaptchaProducer() {
  19. // 实例一个DefaultKaptcha
  20. DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
  21. // 创建配置对象
  22. Properties properties = new Properties();
  23. // 设置边框
  24. properties.setProperty("kaptcha.border", "yes");
  25. // 设置颜色
  26. properties.setProperty("kaptcha.border.color", "105,179,90");
  27. // 设置字体颜色
  28. properties.setProperty("kaptcha.textproducer.font.color", "blue");
  29. // 设置宽度
  30. properties.setProperty("kaptcha.image.width", "125");
  31. // 高度
  32. properties.setProperty("kaptcha.image.height", "50");
  33. // 设置session.key
  34. properties.setProperty("kaptcha.session.key", "code");
  35. // 设置文本长度
  36. properties.setProperty("kaptcha.textproducer.char.length", "4");
  37. // 设置字体
  38. properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
  39. // 将以上属性设置为实例一个DefaultKaptcha的属性
  40. Config config = new Config(properties);
  41. defaultKaptcha.setConfig(config);
  42. // 将defaultKaptcha返回
  43. return defaultKaptcha;
  44. }
  45. }

2.1:Kaptcha详细配置


























































































序号 属性名 描述 示例
1 kaptcha.width 验证码宽度 200
2 kaptcha.height 验证码高度 50
3 kaptcha.border.enabled 是否显示边框 false
4 kaptcha.border.color 边框颜色 black
5 kaptcha.border.thickness 边框厚度 2
6 kaptcha.content.length 验证码文本长度 5
7 kaptcha.content.source 文本源 abcde2345678gfynmnpwx
8 kaptcha.content.space 文本间隔 2
9 kaptcha.font.name 字体名称 Arial
10 kaptcha.font.size 字体大小 40
11 kaptcha.font.color 字体颜色 black
12 kaptcha.background-color.from 背景颜色(开始渐变色) lightGray
13 kaptcha.background-color.to 背景颜色(结束渐变色 white

3:编写Controller,实现验证码请求接口

  1. package simulationvirtual.VirtualExperiment.controller.tools;
  2. import com.google.code.kaptcha.Producer;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.data.redis.core.RedisTemplate;
  5. import org.springframework.util.FastByteArrayOutputStream;
  6. import org.springframework.web.bind.annotation.GetMapping;
  7. import org.springframework.web.bind.annotation.RestController;
  8. import simulationvirtual.VirtualExperiment.util.AjaxResult;
  9. import simulationvirtual.VirtualExperiment.util.Base64;
  10. import simulationvirtual.VirtualExperiment.util.UUIDutil;
  11. import javax.imageio.ImageIO;
  12. import javax.servlet.http.HttpServletResponse;
  13. import javax.servlet.http.HttpSession;
  14. import java.awt.image.BufferedImage;
  15. import java.io.IOException;
  16. import java.util.concurrent.TimeUnit;
  17. @RestController
  18. public class KaptchaController {
  19. @Autowired
  20. private RedisTemplate<String,Object> redisTemplate;
  21. @Autowired
  22. private Producer kaptchaProduer;
  23. /**
  24. * 生成图形验证码
  25. * @author: Xiongch
  26. * @param:null
  27. * @return:Ajax_Result(统一返回工具)
  28. * @date: 2022/9/9 15:47
  29. */
  30. @GetMapping("/kaptcha")
  31. public AjaxResult getKaptcha(HttpServletResponse response, HttpSession session){
  32. AjaxResult Ajax_Result = AjaxResult.success();
  33. String imagecode = kaptchaProduer.createText();
  34. // 生成图片
  35. BufferedImage image = kaptchaProduer.createImage(imagecode);
  36. // 将验证码存入Session
  37. session.setAttribute("kaptcha",imagecode);
  38. //将图片输出给浏览器
  39. String uuid = UUIDutil.getUUID();//uuid-->验证码唯一标识
  40. FastByteArrayOutputStream os = new FastByteArrayOutputStream();
  41. try {
  42. response.setContentType("image/png");
  43. ImageIO.write(image,"png",os);
  44. //验证码实现redis缓存,过期时间2分钟
  45. session.setAttribute("uuid",imagecode);
  46. redisTemplate.opsForValue().set(uuid,imagecode,2, TimeUnit.MINUTES);
  47. } catch (IOException e) {
  48. return AjaxResult.error(e.getMessage());
  49. }
  50. Ajax_Result.put("uuid",uuid);
  51. Ajax_Result.put("img", Base64.encode(os.toByteArray()));
  52. return Ajax_Result;
  53. }
  54. }

4:vue前端展示代码

  1. <div class="login-code">
  2. <img :src="codeUrl" @click="getCode" class="login-code-img"/>
  3. </div>

4.1:点击刷新方法

  1. methods: {
  2. getCode() {
  3. this.$axios({
  4. method:'GET',
  5. url:"/kaptcha",
  6. headers: {
  7. "Content-Type": "application/json"
  8. }
  9. }).then(res=>{
  10. this.codeUrl = "data:image/gif;base64," + res.data.img;
  11. })
  12. },

5:效果如下

img

转自:https://blog.csdn.net/qq\_50954744/article/details/126810009

发表评论

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

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

相关阅读

    相关 Kaptcha生成图片验证

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