Import Dependency Pack--kaptcha-2.3.2-jdk14.jar
Add the following code to the SPRING-MVC configuration file
<!--verification Code generator configuration--> <bean id= "Captchaproducer" class= "Com.google.code.kaptcha.impl.DefaultKaptcha" > <property name= "config" > <bean class= "com.google.code.kaptcha.util.Config" > <c onstructor-arg> <props> <prop key= "Kaptcha.border" >yes</prop > <prop key= "kaptcha.border.color" >105,179,90</prop> <PR Op key= "Kaptcha.textproducer.font.color" >blue</prop> <prop key= "Kaptcha.image.width" &G
t;100</prop> <prop key= "Kaptcha.image.height" >60</prop> <prop key= "Kaptcha.textproducer.font.size" >45</prop> <prop key= "Kaptcha.session.key"
">code</prop> <prop key=" Kaptcha.textproducer.char.length ">4</prop> <propkey= "Kaptcha.textproducer.font.names" > Arial, italics, Microsoft Ya-black </prop> </props> </c onstructor-arg> </bean> </property> </bean>
3.html Code
<div class= "Checkcode" >
<input type= "text" id= "Codetext" placeholder= "Authentication Code" maxlength= "4"
class= " LOGIN_TXTBX "><input id=" Vcode type= "image" alt= "Authenticode"
src= "captcha-image.do" onclick= "ChangeCode" () "> </input>
</div>
4.js Script
/**
* Get Verification code
/
function ChangeCode () {
$ ("#vcode"). attr (' src ',
' captcha-image.do? ' + Math.floor (Math.random () *));
}
5.controller Layer
Package com.xinrui.hospital.action;
Import Java.awt.image.BufferedImage;
Import java.io.IOException;
Import Javax.annotation.Resource;
Import Javax.imageio.ImageIO;
Import Javax.servlet.ServletOutputStream;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import javax.servlet.http.HttpSession;
Import org.springframework.beans.factory.annotation.Autowired;
Import Org.springframework.stereotype.Controller;
Import org.springframework.web.bind.annotation.RequestMapping;
Import Org.springframework.web.bind.annotation.RequestMethod;
Import Com.google.code.kaptcha.Producer;
Import Com.xinrui.hospital.service.impl.AdminServiceImpl;
Import com.xinrui.hospital.util.Constants;
Import Com.xinrui.hospital.util.PrintWriterUtil; /** * * @ClassName: Logincontroller * @Description: Login Control class * @author Liang Zhicheng * @date March 17, 2016 PM 11:08:51 * * * * * * * * @Contro Ller public class Logincontroller {/** * Automatic load Verification code generator/@Autowired private produceR captchaproducer = null; /** * * @Title: Getkaptchaimage * @Description: Generate picture Verification code * @param @param request * @param @param Response * @param @throws Exception * @return void * @throws/@RequestMapping (value = "Captcha-i Mage ") public void Getkaptchaimage (HttpServletRequest request, httpservletresponse response) throws Except
Ion {HttpSession session = Request.getsession ();
Prohibit image caching Response.setdateheader ("Expires", 0);
Response.setheader ("Cache-control", "No-store, No-cache, must-revalidate");
Response.AddHeader ("Cache-control", "post-check=0, pre-check=0");
Response.setheader ("Pragma", "No-cache");
Response.setcontenttype ("Image/jpeg");
String Captext = Captchaproducer.createtext ();
Session.setattribute (Constants.kaptcha_session_key, Captext);
BufferedImage bi = captchaproducer.createimage (captext); ServletouTputstream out = Response.getoutputstream ();
Output the picture in memory to the client imageio.write (bi, "JPG", out) through the flow form;
try {out.flush ();
finally {out.close ();
}
}
}