1 PackageMain.com.smart.controller;2 3 ImportCom.google.code.kaptcha.Producer;4 ImportMain.com.smart.utils.CaptchaProducer;5 Importmain.com.smart.utils.RedisClient;6 ImportOrg.apache.log4j.Logger;7 Importorg.apache.log4j.spi.LoggerFactory;8 Importorg.springframework.beans.factory.annotation.Autowired;9 ImportOrg.springframework.stereotype.Controller;Ten Importorg.springframework.web.bind.annotation.RequestMapping; One ImportOrg.springframework.web.servlet.ModelAndView; A - ImportJavax.imageio.ImageIO; - ImportJavax.servlet.ServletOutputStream; the Importjavax.servlet.http.HttpServletRequest; - ImportJavax.servlet.http.HttpServletResponse; - Importjavax.servlet.http.HttpSession; - + Importcom.google.code.kaptcha.Constants; - + ImportJava.awt.image.BufferedImage; A at /** - * Verification Code generation Controller - * - * @authorYang Ming - * @create 2016-04-29 12:17 - */ in - to @Controller +@RequestMapping ("/kaptcha") - Public classCaptchacontroller { the /** * * How to use $ * Get:http://localhost: 8080/smart/kaptchaPanax Notoginseng */ - Private Static FinalLogger log = Logger.getlogger (Captchacontroller.class); the @Autowired + PrivateProducer Captchaproducer =NULL; A the @RequestMapping + PublicModelandview Getkaptchaimage (httpservletrequest request, httpservletresponse response)throwsException { -Redisclient client =redisclient.getinstance (); $ //get the verification code in the Controller $ //String kaptchaexpected = (string) request.getsession (). getattribute (com.google.code.kaptcha.Constants.KAPTCHA_ Session_key); -Log.info ("Get Validation"); - the Log.info (constants.kaptcha_session_key); -HttpSession session =request.getsession ();WuyiString Code1 =Client.getvalue (constants.kaptcha_session_key); theString Code =string.valueof (Session.getattribute (Constants.kaptcha_session_key)); - //System.out.println ("****************** Verification Code is:" + code + "******************"); Wu -Response.setdateheader ("Expires", 0); About $ //Set Standard http/1.1 no-cache headers. -Response.setheader ("Cache-control", "No-store, No-cache, Must-revalidate"); - - //Set IE Extended http/1.1 no-cache headers (use AddHeader). AResponse.AddHeader ("Cache-control", "Post-check=0, pre-check=0"); + the //Set Standard http/1.0 no-cache header. -Response.setheader ("Pragma", "No-cache"); $ the //return a JPEG theResponse.setcontenttype ("Image/jpeg"); the the //Create the text for the image -string[] Array =Captchaproducer.getcaptchatext (); in //String captext = Captchaproducer.createtext (); the //int param1 = (int) captext.charat (0); the //int param2 = (int) captext.charat (1); AboutInteger result = integer.valueof (array[0]) + integer.valueof (array[1]); the the //Store the text in the session the Client.setvalue (Constants.kaptcha_session_key, result.tostring ()); + //Session.setattribute (Constants.kaptcha_session_key, result); - the //Create the image with the textBayiBufferedImage bi = captchaproducer.createimage (array[0]+ "+" +array[1]+ "=?"); theServletoutputstream out =Response.getoutputstream (); the - //write the data out -Imageio.write (BI, "JPG", out); the Try { the Out.flush (); the}finally { the out.close (); - } the return NULL; the } the 94}
Using Google Kaptcha for verification code generation