1 package main.com.smart.controller; 2 3 import com.google.code.kaptcha.Producer; 4 import main.com.smart.utils.CaptchaProducer; 5 import main.com.smart.utils.RedisClient; 6 import org.apache.log4j.Logger; 7 import org.apache.log4j.spi.LoggerFactory; 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.servlet.ModelAndView; 12 13 import javax.imageio.ImageIO; 14 import javax.servlet.ServletOutputStream; 15 import javax.servlet.http.HttpServletRequest; 16 import javax.servlet.http.HttpServletResponse; 17 import javax.servlet.http.HttpSession; 18 19 import com.google.code.kaptcha.Constants; 20 21 import java.awt.image.BufferedImage; 22 23 /** 24 * 验证码 生成控制器 25 * 26 * @author yang ming 27 * @create 2016-04-29 12:17 28 */ 29 30 31 @Controller 32 @RequestMapping("/kaptcha") 33 public class CaptchaController { 34 /** 35 * 使用方法 36 * get: http://localhost:8080/smart/kaptcha 37 */ 38 private static final Logger log = Logger.getLogger(CaptchaController.class); 39 @Autowired 40 private Producer captchaProducer = null; 41 42 @RequestMapping 43 public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception { 44 RedisClient client = RedisClient.getInstance(); 45 //在控制器中获取验证码 46 //String kaptchaExpected = (String) request.getSession().getAttribute(com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); 47 log.info("获取验证"); 48 49 log.info(Constants.KAPTCHA_SESSION_KEY); 50 HttpSession session = request.getSession(); 51 String code1 = client.getValue(Constants.KAPTCHA_SESSION_KEY); 52 String code = String.valueOf(session.getAttribute(Constants.KAPTCHA_SESSION_KEY)); 53 //System.out.println("******************验证码是: " + code + "******************"); 54 55 response.setDateHeader("Expires", 0); 56 57 // Set standard HTTP/1.1 no-cache headers. 58 response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate"); 59 60 // Set IE extended HTTP/1.1 no-cache headers (use addHeader). 61 response.addHeader("Cache-Control", "post-check=0, pre-check=0"); 62 63 // Set standard HTTP/1.0 no-cache header. 64 response.setHeader("Pragma", "no-cache"); 65 66 // return a jpeg 67 response.setContentType("image/jpeg"); 68 69 // create the text for the image 70 String[] array = CaptchaProducer.getCaptchaText(); 71 //String capText = captchaProducer.createText(); 72 //int param1 = (int)capText.charAt(0); 73 //int param2 = (int)capText.charAt(1); 74 Integer result = Integer.valueOf(array[0]) + Integer.valueOf(array[1]); 75 76 // store the text in the session 77 client.setValue(Constants.KAPTCHA_SESSION_KEY, result.toString()); 78 //session.setAttribute(Constants.KAPTCHA_SESSION_KEY, result); 79 80 // create the image with the text 81 BufferedImage bi = captchaProducer.createImage(array[0]+"+"+array[1]+"=?"); 82 ServletOutputStream out = response.getOutputStream(); 83 84 // write the data out 85 ImageIO.write(bi, "jpg", out); 86 try { 87 out.flush(); 88 } finally { 89 out.close(); 90 } 91 return null; 92 } 93 94 }
时间: 2024-10-06 01:10:05