Kaptcha works by calling Com.google.code.kaptcha.servlet.KaptchaServlet and generating a picture. The resulting Authenticode string is also placed in the httpsession.
1: Pre-work: Preparing Kaptcha jar Packages
<!--kaptcha--> <dependency> <groupId>com.github.axet</groupId> < artifactid>kaptcha</artifactid> <version>0.0.9</version> </dependency>
2: Configure the Bean for the picture Builder in the spring configuration file
<bean id= "Captchaproducer" class= "Com.google.code.kaptcha.impl.DefaultKaptcha" > <property name= "config" & Gt <bean class= "Com.google.code.kaptcha.util.Config" > <constructor-arg> <pro ps> <prop key= "Kaptcha.border" >no</prop> <prop key= "Kaptch A.border.color ">105,179,90</prop> <prop key=" Kaptcha.textproducer.font.color ">red< ;/prop> <prop key= "Kaptcha.image.width" >250</prop> <prop K ey= "Kaptcha.textproducer.font.size" >80</prop> <prop key= "Kaptcha.image.height" >90< ;/prop> <prop key= "Kaptcha.session.key" >code</prop> <prop key= "Kaptcha.textproducer.char.length" >4</prop> <prop key= "kaptcha.textproducer.font.na Mes "> SongBody, italics, Microsoft Ya-black </prop> </props> </constructor-arg> </bean> </property> </bean>
There are many image generator properties, such as font color, font size, etc., according to the English meaning can understand the configuration of what is the content of
3: The controller that generated the picture
@Controllerpublic class Captchaimagecreatecontroller {@Autowired private Producer captchaproducer;//Verification code generator @Requ Estmapping ("/captcha-image") public Modelandview HandleRequest (httpservletrequest request, HttpServletResponse Response) throws Exception {Response.setdateheader ("Expires", 0); 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). Response.AddHeader ("Cache-control", "post-check=0, pre-check=0"); Set Standard http/1.0 No-cache header. Response.setheader ("Pragma", "No-cache"); return a JPEG response.setcontenttype ("image/jpeg"); Create the text for the image String Captext = Captchaproducer.createtext (); Store the text in the session request.getsession (). SetAttribute (Constants.kaptcha_session_key, captext);//validation to be generated The code is saved in session//CReate the image with the text bufferedimage bi = captchaproducer.createimage (captext); Servletoutputstream out = Response.getoutputstream (); Write the data out Imageio.write (bi, "JPG", out); try {out.flush (); } finally {out.close (); } return null; }}
4. Verification Code Verification Controller
@Controllerpublic class Verifycontroller { @RequestMapping (value = "/checkverificationcode") @ResponseBody Public Boolean Checkverificationcode (@RequestParam ("Verifycode") String Verifycode, httpservletrequest Request) { //CAPTCHA value String kaptchaexpected = (string) request.getsession () . getattribute ( Com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); The value of the verification code entered by the user String kaptchareceived = Verifycode; System.out.println ("The Actual verification code is:" +kaptchaexpected); SYSTEM.OUT.PRINTLN ("Input Verification Code:" +kaptchareceived); if (kaptchareceived = = NULL | |!kaptchareceived.equalsignorecase (kaptchaexpected)) { return false; } return true; }}
Expect the input verification code to be removed from the session, compared to the actual input verification code, if the same, return true, otherwise false
5: Front Page
<div class= "title" > User login </div><div class= "Loginbox" > <form id= "loginform" action= "/ Checkverificationcode "method=" POST "> <div style=" height:40px; " > <label class= "Tip" > Noboribetsu record name: </label> <input name= "name" type = "text" id= "name" class= "User-text" value= "/> </div> <div style=" height:40px; " > <label class= "Tip" > Secret code: </label> <input type= "Password" I d= "password" name= "password" class= "User-text" value= "/> </div> <div style=" height:60px; " > <label class= "Tip" > Verification License code: </label> <input type= "text" name = "Verifycode" id= "Verifycode" class= "Usertext" value= ""/> onchange= "Changeverifycode ();" /> </div> <div style=" margin-left:15px "> <input type=" Submit "class= "LOGIN-BTN" value= "login"/> <input type= "reset" class= "login-btn" style= "margin-left:10px;" value= "reset"/> </div> </form></div>
Src= "Captcha-image.jpg", will trigger the controller that generates the picture, return a picture, action= "/checkverificationcode", will call the controller that validates the verification code, return the corresponding result.
Kaptcha Generate Java Verification Code