CAPTCHA implement verification code to verify the user login, to prevent the password from being violently cracked.
The following is run in the SPRINGMVC framework.
imported jar Packages:Kaptcha-2.3.2.jar
: http://download.csdn.net/detail/u013147600/9052871
or in maven---pom.xml: Configure the following
<!--captcha Tools--
<dependency>
<groupId>com.google.code</groupId>
<artifactId>kaptcha</artifactId>
<version>2.3.2</version>
</dependency>
configuration of Web. XML
<span style= "FONT-SIZE:14PX;" ><!--Verification Code servlet--<servlet> <servlet-name>kaptcha</servlet-name> <servlet-class >com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class> </servlet> < servlet-mapping> <servlet-name>kaptcha</servlet-name> <url-pattern>/kaptcha.jpg </url-pattern> </servlet-mapping></span>
springmvc.xml Configuration
(Captchaproducer is equivalent to a class, and the user modifies its properties according to its own requirements)
<span style= "FONT-SIZE:14PX;" ><!--captcha Configuration of some properties--><bean id= "Captchaproducer" class= "Com.google.code.kaptcha.impl.DefaultKaptcha" > <property name= "config" > <bean class= "Com.google.code.kaptcha.util.Config" > <constructor-arg> <props> <prop key= "Kaptcha.border ">yes</prop> <prop key=" Kaptcha.border.color ">105,179,90</prop> <prop key= "Kaptcha.textproducer.font.color" >blue</prop> <prop key= "Ka Ptcha.image.width ">125</prop> <prop key=" Kaptcha.image.height ">45</prop> <prop key= "Kaptcha.textproducer.font.size" >45</prop> <prop K ey= "Kaptcha.session.key" >code</prop> <prop key= "Kaptcha.textproducer.char.length" >4</prop> <prop key= "Kaptcha.textproducer.font.names" > Arial, Italic, Microsoft ya Black </prop> </props> </constructor-arg> </bean> </property> ; </bean></span>
Springutils.java Parsing class
(Used to parse the bean in Springmvc.xml)
<span style= "FONT-SIZE:14PX;" >package Com.authc.utils;import Org.springframework.context.applicationcontext;import org.springframework.context.support.classpathxmlapplicationcontext;/** * @author LyX * * 2015-8-18 3:53:19 * * Com.utils.SpringUtil *todo */public class Springutil {private static ApplicationContext ctx =new Classpathxmlapplicationcontext ("Springmvc.xml");p ublic static Object Getbean (String beanid) {return Ctx.getbean ( Beanid);}} </span>
Usercontroller Control Layer class
<span style= "FONT-SIZE:14PX;" > @Controller @requestmapping ("/member") public class Usercontroller {private Producer captchaproducer; @RequestMapping (value = "captcha-image") public Modelandview getkaptchaimage (httpservletrequest request, HTTPSERVLETR Esponse response) throws Exception {HttpSession session = Request.getsession (); Gets the captchaproducer Bean captchaproducer = (Producer) springutil.getbean ("Captchaproducer") in Springmvc.xml; String code = (string) session.getattribute (Constants.kaptcha_session_key); SYSTEM.OUT.PRINTLN ("System generated verification code:" + code); 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 (); The system generated validation is placed in the session Session.setattribute (Constants.kaptcha_session_key, Captext); BufferedImage bi = captchaproducer.createimage (captext); Servletoutputstream out = Response.getoutputstream (); Imageio.write (BI, "JPG", out); try {out.flush (); } finally {out.close (); } return null; } @RequestMapping ("/checkcode") public string Checkcode (HttpServletRequest request) {//Get the user input verification code string Submitcode = Webutils.getcleanparam (Request, "J_code");//Get system generated verification code from session string kaptchaexpected = (string) Request.getsession (). getattribute (Com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY); SYSTEM.OUT.PRINTLN ("User input verification code is:" +submitcode+ ", system-generated verification code:" +kaptchaexpected);//Compare if (Stringutils.isempty ( Submitcode) | | Stringutils.equalsignorecase (Submitcode, kaptchaexpected)) {Request.setattribute ("Checkcode", "captcha correct!") ");} Else{request.setattribute ("Checkcode", "CAPTCHA Error! ");} ReTurn "/kaptcha";}} </span>
Note: This is the controller in the validation, the more appropriate method should be in JS inside the user input verification code verification
kaptcha.jsp Page
<span style= "FONT-SIZE:14PX;" ><%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%><% @page iselignored= "false"%> <%string path = Request.getcontextpath (); String basepath = request.getscheme () + "://" +request.getservername () + ":" +request.getserverport () +path+ "/";%> <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
Kaptcha Configurable items:Kaptcha.border whether there is a border default to True we can set the Yes,nokaptcha.border.color border color by default to Color.BLACKkaptcha.border.thickness border thickness The default is 1kaptcha.producer.impl Authenticode generator default to DefaultKaptchakaptcha.textproducer.impl Authenticode text generator defaults to Defaulttextcreatorkaptcha. Textproducer.char.string Verification Code text character content range default to abcde2345678gfynmnpwxkaptcha.textproducer.char.length captcha text character length Default to 5kaptcha.textproducer.font.names captcha text font style default to New font ("Arial", 1, fontSize), New Font ("Courier", 1, FontSize) KAPTC Ha.textproducer.font.size Verification Code text character size default to 40kaptcha.textproducer.font.color captcha text character color Default Color.BLACKkaptcha.textproducer.char.space captcha text character spacing defaults to 2kaptcha.noise.impl captcha Noise Generation Object Default to DefaultNoisekaptcha.noise.color captcha noise color default to Color.BLACKkaptcha.obscurificator.impl CAPTCHA style engine defaults to Waterripplekaptcha . Word.impl captcha text character rendering defaults to DefaultWordRendererkaptcha.background.impl captcha background generator defaults to Defaultbackgroundkaptcha.background . Clear.from Verification Code background color Progressive default to Color.LIGHT_GRAYkaptcha.background.clear.to CAPTCHA background color progressive default to Color.whiTEkaptcha.image.width captcha Picture width default to 200kaptcha.image.height captcha picture height default is 50
Simple:
Reference: http://blog.csdn.net/rambo_china/article/details/7720181
http://cuisuqiang.iteye.com/blog/2048428
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java-captcha Implementation Verification Code (II)