1 创建springboot工程

2 pom中引入相关依赖

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

3 三种实现方式

3.1 第一种使用xml文件配置属性

resources下新建kaptcha.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
  6. <property name="config">
  7. <bean class="com.google.code.kaptcha.util.Config">
  8. <constructor-arg type="java.util.Properties">
  9. <props>
  10. <prop key ="kaptcha.border">yes</prop>
  11. <prop key="kaptcha.border.color">105,179,90</prop>
  12. <prop key="kaptcha.textproducer.font.color">blue</prop>
  13. <prop key="kaptcha.image.width">100</prop>
  14. <prop key="kaptcha.image.height">50</prop>
  15. <prop key="kaptcha.textproducer.font.size">27</prop>
  16. <prop key="kaptcha.session.key">code</prop>
  17. <prop key="kaptcha.textproducer.char.length">4</prop>
  18. <prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
  19. <prop key="kaptcha.textproducer.char.string">0123456789ABCEFGHIJKLMNOPQRSTUVWXYZ</prop>
  20. <prop key="kaptcha.obscurificator.impl">com.google.code.kaptcha.impl.WaterRipple</prop>
  21. <prop key="kaptcha.noise.color">black</prop>
  22. <prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.DefaultNoise</prop>
  23. <prop key="kaptcha.background.clear.from">185,56,213</prop>
  24. <prop key="kaptcha.background.clear.to">white</prop>
  25. <prop key="kaptcha.textproducer.char.space">3</prop>
  26. </props>
  27. </constructor-arg>
  28. </bean>
  29. </property>
  30. </bean>
  31. </beans>

启动类加入@ImportResource注解

  1. @SpringBootApplication
  2. @ImportResource(locations={"classpath:kaptcha.xml"})
  3. public class Application {
  4. public static void main(String[] args) {
  5. SpringApplication.run(Application.class, args);
  6. }
  7. }

com.xja.controller 包中创建测试controller

  1. /**
  2. * 生成验证码并把验证码存储到session中
  3. */
  4. @Controller
  5. public class CodeController {
  6. @Autowired
  7. private Producer captchaProducer = null;
  8. @GetMapping("/kaptcha")
  9. public void getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception {
  10. HttpSession session = request.getSession();
  11. response.setDateHeader("Expires", 0);
  12. response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
  13. response.addHeader("Cache-Control", "post-check=0, pre-check=0");
  14. response.setHeader("Pragma", "no-cache");
  15. response.setContentType("image/jpeg");
  16. //生成验证码
  17. String capText = captchaProducer.createText();
  18. session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
  19. //向客户端写出
  20. BufferedImage bi = captchaProducer.createImage(capText);
  21. ServletOutputStream out = response.getOutputStream();
  22. ImageIO.write(bi, "jpg", out);
  23. try {
  24. out.flush();
  25. } finally {
  26. out.close();
  27. }
  28. }
  29. }

3.2 第二种使用配置bean配置属性

com.xja.config 下新建 KaptchaConfig

  1. @Component
  2. public class KaptchaConfig {
  3. @Bean
  4. public DefaultKaptcha getDefaultKaptcha(){
  5. com.google.code.kaptcha.impl.DefaultKaptcha defaultKaptcha = new com.google.code.kaptcha.impl.DefaultKaptcha();
  6. Properties properties = new Properties();
  7. properties.setProperty("kaptcha.border", "yes");
  8. properties.setProperty("kaptcha.border.color", "105,179,90");
  9. properties.setProperty("kaptcha.textproducer.font.color", "blue");
  10. properties.setProperty("kaptcha.image.width", "110");
  11. properties.setProperty("kaptcha.image.height", "40");
  12. properties.setProperty("kaptcha.textproducer.font.size", "30");
  13. properties.setProperty("kaptcha.session.key", "code");
  14. properties.setProperty("kaptcha.textproducer.char.length", "6");
  15. properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑");
  16. Config config = new Config(properties);
  17. defaultKaptcha.setConfig(config);
  18. return defaultKaptcha;
  19. }
  20. }

3.3 第三种使用配置bean,验证码属性使用验证码属性进行配置

建议使用第三种方法 resources下新建 kaptcha.properties

  1. ##### Kaptcha Information
  2. kaptcha.width=150
  3. kaptcha.height=42
  4. kaptcha.border=no
  5. kaptcha.textproducer.font.size=40
  6. kaptcha.textproducer.char.space=10
  7. kaptcha.textproducer.font.names=\u4EFF\u5B8B,\u5FAE\u8F6F\u96C5\u9ED1
  8. kaptcha.textproducer.char.string=1234567890
  9. kaptcha.textproducer.char.length=4
  10. kaptcha.background.clear.from=92,189,170
  11. kaptcha.background.clear.to=255,255,255

KaptchaConfig修改成从属性文件中读取

  1. @Component
  2. public class KaptchaConfig1 {
  3. private static Properties props = new Properties();
  4. @Bean
  5. public DefaultKaptcha defaultKaptcha() throws Exception {
  6. // 创建DefaultKaptcha对象
  7. DefaultKaptcha defaultKaptcha = new DefaultKaptcha();
  8. // 读取配置文件
  9. try {
  10. props.load(KaptchaConfig1.class.getClassLoader()
  11. .getResourceAsStream("kaptcha.properties"));
  12. }catch (Exception e) {
  13. e.printStackTrace();
  14. }
  15. // 将Properties文件设到DefaultKaptcha对象中
  16. defaultKaptcha.setConfig(new Config(props));
  17. return defaultKaptcha;
  18. }
  19. }

运行后,输入http://localhost:8080/kaptcha
image.png
了解hutool的验证码:https://www.hutool.cn/docs/#/captcha/%E6%A6%82%E8%BF%B0?id=circlecaptcha-%e5%9c%86%e5%9c%88%e5%b9%b2%e6%89%b0%e9%aa%8c%e8%af%81%e7%a0%81