POM

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

创建spring-context-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>
  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">125</prop>
  14. <prop key="kaptcha.image.height">45</prop>
  15. <prop key="kaptcha.textproducer.font.size">45</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. </props>
  20. </constructor-arg>
  21. </bean>
  22. </property>
  23. </bean>
  24. </beans>

控制器

  1. package cn.hzlim.my.shop.web.ui.controller;
  2. import com.google.code.kaptcha.Constants;
  3. import com.google.code.kaptcha.Producer;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.bind.annotation.RequestMethod;
  8. import org.springframework.web.servlet.ModelAndView;
  9. import javax.imageio.ImageIO;
  10. import javax.servlet.ServletOutputStream;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import java.awt.image.BufferedImage;
  14. import java.io.IOException;
  15. import java.util.Enumeration;
  16. /**
  17. * 验证码控制器
  18. *
  19. * @title: KaptchaController
  20. * @description:
  21. *
  22. * @author: gu
  23. * @date: 2019-07-14 22:41
  24. * @version 1.0.0
  25. *
  26. */
  27. @Controller
  28. public class KaptchaController {
  29. @Autowired
  30. private Producer captchaProducer;
  31. @RequestMapping(value = "verification", method = RequestMethod.GET)
  32. public ModelAndView verification(HttpServletRequest request, HttpServletResponse response) throws IOException {
  33. response.setDateHeader("Expires", 0);
  34. // Set standard HTTP/1.1 no-cache headers.
  35. response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
  36. // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
  37. response.addHeader("Cache-Control", "post-check=0, pre-check=0");
  38. // Set standard HTTP/1.0 no-cache header.
  39. response.setHeader("Pragma", "no-cache");
  40. // return a jpeg
  41. response.setContentType("image/jpeg");
  42. // create the text for the image
  43. String capText = captchaProducer.createText();
  44. // store the text in the session
  45. request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
  46. // create the image with the text
  47. BufferedImage bi = captchaProducer.createImage(capText);
  48. ServletOutputStream out = response.getOutputStream();
  49. // write the data out
  50. ImageIO.write(bi, "jpg", out);
  51. try {
  52. out.flush();
  53. } finally {
  54. out.close();
  55. }
  56. return null;
  57. }
  58. }

登陆验证

  1. private boolean checkVerification(HttpServletRequest request, String verification){
  2. String verificationCode =(String)request.getSession().getAttribute(Constats.KAPTCHA_SESSION_KEY);
  3. if (StringUtils.equals(verificationCode,verificaion)){
  4. return true;
  5. }
  6. return false;
  7. }

页面

  1. <img id="code" src="/verification" style="cursor: pointer;" title="看不清?换一张" />
  2. <script>
  3. $(function () {
  4. // 刷新验证码
  5. $("#code").bind("click", function () {
  6. $(this).hide().attr('src', '/verification?random=' + Math.random()).fadeIn();
  7. });
  8. });
  9. </script>