前言:Google Captcha 是Google生成验证码的一个工具类,其原理是将随机生成字符串保存到session中,同时以图片的形式返回给页面,之后前台页面提交到后台进行对比。
1、jar包准备官方提供的pom应该是
<dependency> <groupId>com.google.code.kaptcha</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
但是下载不下来,我在阿里的maven仓库找到的pom如下:
<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency>
测试可以正常下载,这里推荐阿里的maven仓库,下载速度还行,挺稳定,附地址:#welcome
2、spring bean的配置1 <!-- google kaptcha的相关配置--> 2 <bean class="com.google.code.kaptcha.impl.DefaultKaptcha"> 3 <property> 4 <bean class="com.google.code.kaptcha.util.Config"> 5 <constructor-arg> 6 <props> 7 <!-- 是否有边框 可选yes 或者 no --> 8 <prop key="kaptcha.border">yes</prop> 9 <!-- 边框颜色 --> 10 <prop key="kaptcha.border.color">105,179,90</prop> 11 <!-- 验证码文本字符颜色 --> 12 <prop key="kaptcha.textproducer.font.color">blue</prop> 13 <!-- 验证码文本字符大小 --> 14 <prop key="kaptcha.textproducer.font.size">45</prop> 15 <!-- 验证码图片的宽度 默认200 --> 16 <prop key="kaptcha.image.width">125</prop> 17 <!-- 验证码图片的高度 默认50 --> 18 <prop key="kaptcha.image.height">45</prop> 19 <!-- 验证码文本字符长度 默认为5 --> 20 <prop key="kaptcha.textproducer.char.length">4</prop> 21 <!-- 验证码文本字体样式 默认为new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize) --> 22 <prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop> 23 </props> 24 </constructor-arg> 25 </bean> 26 </property> 27 </bean>
3、Controller的两个方法1 package com.ccg.controller; 2 3 import java.awt.image.BufferedImage; 4 import java.io.IOException; 5 import java.io.PrintWriter; 6 7 import javax.annotation.Resource; 8 import javax.imageio.ImageIO; 9 import javax.servlet.ServletOutputStream; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 import javax.servlet.http.HttpSession; 13 14 import org.springframework.stereotype.Controller; 15 import org.springframework.web.bind.annotation.RequestMapping; 16 import org.springframework.web.bind.annotation.RequestParam; 17 import org.springframework.web.servlet.ModelAndView; 18 19 import com.google.code.kaptcha.Constants; 20 import com.google.code.kaptcha.Producer; 21 22 @Controller 23 @RequestMapping("captcha") 24 public class CaptchaController { 25 26 @Resource 27 private Producer captchaProducer; 28 /** 29 * 30 * 获取验证码图片 31 * @author ccg 32 * @param request 33 * @param response 34 * @return 35 * @throws IOException 36 * Created 2017年1月17日 下午5:07:28 37 */ 38 @RequestMapping("getCaptchaCode") 39 public ModelAndView getCaptchaCode(HttpServletRequest request, HttpServletResponse response) throws IOException{ 40 HttpSession session = request.getSession(); 41 42 response.setDateHeader("Expires", 0); 43 response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); 44 response.addHeader("Cache-Control", "post-check=0, pre-check=0"); 45 response.setHeader("Pragma", "no-cache"); 46 response.setContentType("image/jpeg"); 47 48 //生成验证码文本 49 String capText = captchaProducer.createText(); 50 session.setAttribute(Constants.KAPTCHA_SESSION_KEY, capText); 51 System.out.println("生成验证码文本===="+capText); 52 //利用生成的字符串构建图片 53 BufferedImage bi = captchaProducer.createImage(capText); 54 ServletOutputStream out = response.getOutputStream(); 55 ImageIO.write(bi, "jpg", out); 56 57 try { 58 out.flush(); 59 } finally { 60 out.close(); 61 } 62 return null; 63 } 64 65 /** 66 * 67 * 前端输入的验证码与生成的对比 68 * @author ccg 69 * @param request 70 * @param response 71 * @param captchaCode 72 * Created 2017年1月17日 下午5:34:23 73 */ 74 @RequestMapping("checkCaptchaCode") 75 public void checkCaptchaCode(HttpServletRequest request, HttpServletResponse response,@RequestParam("captchaCode") String captchaCode){ 76 System.out.println("页面输入验证码===="+captchaCode); 77 78 response.setCharacterEncoding("UTF-8"); 79 response.setHeader("Pragma", "No-cache"); 80 response.setHeader("Cache-Control", "no-cache"); 81 response.setDateHeader("Expires", 0); 82 83 String generateCode =(String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY); 84 String result = ""; 85 if(generateCode.equals(captchaCode)){ 86 result = "验证成功"; 87 }else{ 88 result = "输入错误"; 89 } 90 PrintWriter out = null; 91 try { 92 out = response.getWriter(); 93 } catch (IOException e) { 94 e.printStackTrace(); 95 } 96 out.print(result); 97 out.flush(); 98 } 99 }
4、前台页面代码