Java-captcha Implementation Verification Code (II)

Source: Internet
Author: User
Tags border color

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


or in maven---pom.xml: Configure the following

<!--captcha Tools--

configuration of Web. XML

<span style= "FONT-SIZE:14PX;" ><!--Verification Code servlet--<servlet> <servlet-name>kaptcha</servlet-name> <servlet-class ></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= ""                  > <property name= "config" > <bean class= "" > <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&gt      ; </bean></span> 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;/** * @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 (; 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 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 CAPTCHA background color progressive default to Color.whiTEkaptcha.image.width captcha Picture width default to 200kaptcha.image.height captcha picture height default is 50  



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java-captcha Implementation Verification Code (II)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.