利用Google kaptcha 進行驗證碼產生

來源:互聯網
上載者:User

標籤:

 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 ming27  * @create 2016-04-29 12:1728  */29 30 31 @Controller32 @RequestMapping("/kaptcha")33 public class CaptchaController {34     /**35      * 使用方法36      * get: http://localhost:8080/smart/kaptcha37      */38     private static final Logger log = Logger.getLogger(CaptchaController.class);39     @Autowired40     private Producer captchaProducer = null;41 42     @RequestMapping43     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 jpeg67         response.setContentType("image/jpeg");68 69         // create the text for the image70         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 session77         client.setValue(Constants.KAPTCHA_SESSION_KEY, result.toString());78         //session.setAttribute(Constants.KAPTCHA_SESSION_KEY, result);79 80         // create the image with the text81         BufferedImage bi = captchaProducer.createImage(array[0]+"+"+array[1]+"=?");82         ServletOutputStream out = response.getOutputStream();83 84         // write the data out85         ImageIO.write(bi, "jpg", out);86         try {87             out.flush();88         } finally {89             out.close();90         }91         return null;92     }93 94 }

 

利用Google kaptcha 進行驗證碼產生

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.