SpringMVC+kaptcha实现图形验证码

小灰灰 2022-08-10 03:38 309阅读 0赞

基础准备

maven

在pom.xml中引入依赖即可。

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

配置

在applicationContext中写入配置

  1. <bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha">
  2. <property name="config">
  3. <bean class="com.google.code.kaptcha.util.Config">
  4. <constructor-arg>
  5. <props>
  6. <prop key="kaptcha.border">yes</prop>
  7. <prop key="kaptcha.border.color">105,179,90</prop>
  8. <prop key="kaptcha.textproducer.font.color">blue</prop>
  9. <prop key="kaptcha.image.width">125</prop>
  10. <prop key="kaptcha.image.height">45</prop>
  11. <prop key="kaptcha.textproducer.font.size">45</prop>
  12. <prop key="kaptcha.session.key">code</prop>
  13. <prop key="kaptcha.textproducer.char.length">4</prop>
  14. <prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop>
  15. </props>
  16. </constructor-arg>
  17. </bean>
  18. </property>
  19. </bean>

代码编写

后端

声明一个Controller用来处理请求,并返回图片。同时将验证码存在Session中,用于后期验证。

  1. @Controller
  2. @RequestMapping("/kaptcha")
  3. public class CaptchaController {
  4. Producer captchaProducer = null;
  5. @Autowired
  6. public void setCaptchaProducer(Producer captchaProducer) {
  7. this.captchaProducer = captchaProducer;
  8. }
  9. @RequestMapping("/doGet")
  10. public ModelAndView doGet(HttpServletRequest request, HttpServletResponse response) throws Exception {
  11. response.setDateHeader("Expires", 0);
  12. // Set standard HTTP/1.1 no-cache headers.
  13. response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
  14. // Set IE extended HTTP/1.1 no-cache headers (use addHeader).
  15. response.addHeader("Cache-Control", "post-check=0, pre-check=0");
  16. // Set standard HTTP/1.0 no-cache header.
  17. response.setHeader("Pragma", "no-cache");
  18. // return a jpeg
  19. response.setContentType("image/jpeg");
  20. // create the text for the image
  21. String capText = captchaProducer.createText();
  22. // store the text in the session
  23. request.getSession().setAttribute(Constants.KAPTCHA_SESSION_KEY, capText);
  24. // create the image with the text
  25. BufferedImage bi = captchaProducer.createImage(capText);
  26. ServletOutputStream out = response.getOutputStream();
  27. // write the data out
  28. ImageIO.write(bi, "jpg", out);
  29. try {
  30. out.flush();
  31. } finally {
  32. out.close();
  33. }
  34. System.out.println("Captchca:"+request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY));
  35. return null;
  36. }
  37. }

前端

主页这里为了不让验证码图片被缓存,需要在请求后边加入随机数。

  1. function refreshCaptcha(){
  2. var ran = Math.floor(Math.random() * 100)
  3. $('#captcha1').attr('src','/webRoot/kaptcha/doGet?' + ran);
  4. }

验证码认证

将用户输入的字符串与Session中保存的验证码进行比较即可

  1. @ResponseBody
  2. @RequestMapping(value="doLogin", method = RequestMethod.POST)
  3. public ResultInfo doLogin(HttpServletRequest request, HttpServletResponse response){
  4. ResultInfo result = new ResultInfo();
  5. String captcha = request.getParameter("captcha");
  6. if(!captcha.equals(request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY))){
  7. result.setStateId(-1);
  8. result.setErrorMsg("验证码不正确");
  9. return result;
  10. }
  11. /*其它的登录代码*/
  12. return result;
  13. }

发表评论

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

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

相关阅读