The role of the CAPTCHA:
1, to prevent advertising machine registration and posting, comments.
2, to prevent brute force password, especially the password with administrator privileges.
Here is a very useful verification code generation tool: Kaptcha
This tool can generate various styles of verification code, because it is configurable.
And the principle of kaptcha work is to call Com.google.code.kaptcha.servlet.KaptchaServlet and generate a picture. At the same time, the generated verification code string is placed in the httpsession, directly from the session to get this captcha picture, without occupying actual memory.
The following properties can be conveniently configured using Kaptcha:
Kaptcha.border whether there is a border default to True we can set it ourselves yes,no
Kaptcha.border.color border color defaults to Color.Black
kaptcha.border.thickness Border thickness defaults to 1
Kaptcha.producer.impl Verification Code generator defaults to Defaultkaptcha
Kaptcha.textproducer.impl captcha Text Generator defaults to Defaulttextcreator
kaptcha.textproducer.char.string Captcha text character content range defaults to ABCDE2345678GFYNMNPWX
Kaptcha.textproducer.char.length Verification Code Text character length default is 5
Kaptcha.textproducer.font.names captcha text font style default to New font ("Arial", 1, fontSize), New Font ("Courier", 1, FontSize)
Kaptcha.textproducer.font.size captcha text character size defaults to 40
Kaptcha.textproducer.font.color captcha text character color defaults to Color.Black
Kaptcha.textproducer.char.space Verification Code text character spacing defaults to 2
Kaptcha.noise.impl Verification Code Noise generation object defaults to Defaultnoise
Kaptcha.noise.color captcha Noise color defaults to Color.Black
Kaptcha.obscurificator.impl Verification Code style engine defaults to Waterripple
Kaptcha.word.impl captcha text character rendering defaults to Defaultwordrenderer
Kaptcha.background.impl Verification Code Background generator defaults to Defaultbackground
Kaptcha.background.clear.from Verification Code Background color progressive defaults to Color.light_gray
Kaptcha.background.clear.to Verification Code Background color progressive defaults to Color.White
Kaptcha.image.width Verification Code picture width defaults to 200
Kaptcha.image.height Verification Code Picture height defaults to 50
Key key to store the verification code in the Kaptcha.session.key session
The practical framework: SSM
The required verification code for the jar package: Kaptcha-2.3.2.jar, can be downloaded to the official website: http://code.google.com/p/kaptcha/
Applicationcontext.xml need to configure the relevant properties of the CAPTCHA:
1 <!--Verification Code -2 <BeanID= "Captchaproducer"class= "Com.google.code.kaptcha.impl.DefaultKaptcha">3 < Propertyname= "config">4 <Beanclass= "Com.google.code.kaptcha.util.Config">5 <Constructor-arg>6 <Props>7 <!--the colors here only support standard and RGB colors, not hexadecimal colors -8 <!--whether there is a border -9 <propKey= "Kaptcha.border">No</prop>Ten <!--captcha text character color - One <propKey= "Kaptcha.textproducer.font.color">Black</prop> A <!--Verification Code Picture width - - <propKey= "Kaptcha.image.width">92</prop> - <!--Verification Code Picture height - the <propKey= "Kaptcha.image.height">36</prop> - <!--captcha text character size - - <propKey= "Kaptcha.textproducer.font.size">24</prop> - <!--Key key to store verification code in session - + <propKey= "Kaptcha.session.key">Code</prop> - <!--captcha Noise Color - + <propKey= "Kaptcha.noise.color">White</prop> A <!--captcha text Character spacing - at <propKey= "Kaptcha.textproducer.char.space">3</prop> - <!--validation code style engine - - <propKey= "Kaptcha.obscurificator.impl">Com.google.code.kaptcha.impl.ShadowGimpy</prop> - <!--captcha Text character length - - <propKey= "Kaptcha.textproducer.char.length">4</prop> - <!--captcha text font style - in <propKey= "Kaptcha.textproducer.font.names">Song body, italics, Microsoft Ya-hei</prop> - </Props> to </Constructor-arg> + </Bean> - </ Property> the </Bean>
Generate two-dimensional code picture control class: Captchacontroller.java
1 /**2 * Com.krry.web3 * Method Name: Generate two-dimensional code control class4 * Create person: Krry5 * @paramRequest6 * @paramResponse7 * @return8 * @throwsException9 * return type: ModelandviewTen * @exception One * @since1.0.0 A */ -@RequestMapping ("/code") - PublicModelandview getkaptchaimage (httpservletrequest request,httpservletresponse response)throwsException { theHttpSession session =request.getsession (); - //Get Verification Code - //String code = (string) session.getattribute (Constants.kaptcha_session_key); - //String code = (string) session.getattribute ("Kaptcha_code"); + //clear the browser's cache -Response.setdateheader ("Expires", 0); + //Set Standard http/1.1 no-cache headers. AResponse.setheader ("Cache-control", "No-store, No-cache, Must-revalidate"); at //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"); in //Browser Memory-----The images and resources downloaded after the current browser and server interaction are successful are cached once. The next refresh will not be on the server to download. - //get random text for Kaptcha validation toString Captext =Captchaproducer.createtext (); + //put the generated picture into the conversation - Session.setattribute (Constants.kaptcha_session_key, captext); the //Create the image with the text *BufferedImage bi =captchaproducer.createimage (captext); $Servletoutputstream out =Response.getoutputstream ();Panax Notoginseng //write the data out -Imageio.write (BI, "JPG", out); the Try { + Out.flush (); A}finally { theOut.close ();//Close + } - return NULL; $}
Foreground call:
1 <inputtype= ' text 'placeholder= ' Please enter verification code ... 'maxlength= ' 4 'AutoComplete= ' Off 'class= ' InPkr_code ' ID= ' Code '/>2 <imgsrc= ' "+basepath+"/kaptcha/code.do 'class= ' yanz_img 'onclick= ' Changeyanz ($ (this)); '>
JS method: Click on the verification code image for verification code when the, onclick is to change the tag src attribute. So to take a random number of URLs, so that each time you click on the CAPTCHA image, the JSP will be re-requested due to the SRC change
1 function Changeyanz (obj) {2 obj.attr ("src", basepath+ "/kaptcha/code.do?d=" +new Date (). GetTime ()); 3 }
Java Implementation Login Verification code (Kaptcha Verification Code component)