Verification Code utility Jtaptcha and utility jtaptcha
CAPTCHA is Completely Automated Public Turing Test to Tell Computers and Humans Apart
The abbreviation of a completely automated Turing test that distinguishes computers from humans
1. Introduce the jar package
Jcaptcha-1.0.jar
2. Configure the mvc integration file ***. xml
<! -- Specifies the directory where the configuration file of Spring Bean is located. The default configuration is under the WEB-INF directory --> <context-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: applicationContext *. xml, classpath :***. xml </param-value> </context-param> <filter-name> encoding </filter-name> <filter-class> org. springframework. web. filter. characterEncodingFilter </filter-class> <init-param> <param-name> encoding </param-name> <param-value> UTF-8 </param-value> </init- param> </filter>View Code
2.1 ***. xml content
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd" default-lazy-init = "true"> <! -- Kaptcha Verification Code <bean id = "captchaProducer" class = "com. google. code. kaptcha. impl. defaultKaptcha "> <property name =" config "> <bean class =" com. google. code. kaptcha. util. config "> <constructor-arg type =" java. util. properties "> <value> kaptcha. border = yes </value> </constructor-arg> </bean> </property> </bean> --> <bean id = "captchaService" class = "com. octo. captcha. service. multitype. genericManageableCaptchaService "> <Constructor-arg index = "0" ref = "imageEngine"/> <constructor-arg type = "int" index = "1" value = "180"/> <constructor- arg type = "int" index = "2" value = "100000" type = "codeph" text = "codeph"/> <constructor-arg type = "int" index = "3" value = "75000"/> </bean> <bean id = "imageEngine" class = "com. octo. captcha. engine. genericCaptchaEngine "> <constructor-arg index =" 0 "> <list> <ref bean =" captchaFactory "/> </list> </constructor-arg> </bean> <bean I D = "captchaFactory" class = "com. octo. captcha. image. gimpy. gimpyFactory "> <constructor-arg> <ref bean =" wordgen "/> </constructor-arg> <ref bean =" wordtoimage "/> </constructor -arg> </bean> <bean id = "wordgen" class = "com. octo. captcha. component. word. wordgenerator. randomWordGenerator "> <! -- Optional --> <constructor-arg> <value> aabbccddeefgghhkkmnnooppqqsstuuvvwxxyyzz </value> </constructor-arg> </bean> <bean id = "wordtoimage" class = "com. octo. captcha. component. image. wordtoimage. composedWordToImage "> <constructor-arg index =" 0 "> <ref bean =" fontGenRandom "/> </constructor-arg> <constructor-arg index =" 1 "> <ref bean = "backGenUni"/> </constructor-arg> <constructor-arg index = "2"> <ref bean = "decorat EdPaster "/> </constructor-arg> </bean> <bean id =" fontGenRandom "class =" com. octo. captcha. component. image. fontgenerator. randomFontGenerator "> <! -- Minimum font --> <constructor-arg index = "0"> <value> 14 </value> </constructor-arg> <! -- Max font --> <constructor-arg index = "1"> <value> 20 </value> </constructor-arg> <constructor-arg index = "2"> <list> <bean class = "java. awt. font "> <constructor-arg index =" 0 "> <value> Arial </value> </constructor-arg> <constructor-arg index =" 1 "> <value> 0 </value> </constructor-arg> <constructor-arg index = "2"> <value> 32 </value> </constructor-arg> </bean> </list> </constructor-arg> </bean> <bean id = "backGenUni" class = "Com. octo. captcha. component. image. backgroundgenerator. UniColorBackgroundGenerator"> <! -- Background width --> <constructor-arg index = "0"> <value> 80 </value> </constructor-arg> <! -- Background height --> <constructor-arg index = "1"> <value> 28 </value> </constructor-arg> </bean> <bean id = "decoratedPaster" class = "com. octo. captcha. component. image. textpaster. decoratedRandomTextPaster "> <! -- Maximum character length --> <constructor-arg type = "java. lang. integer "index =" 0 "> <value> 4 </value> </constructor-arg> <! -- Minimum character length --> <constructor-arg type = "java. lang. integer "index =" 1 "> <value> 4 </value> </constructor-arg> <! -- Text color --> <constructor-arg index = "2"> <ref bean = "colorGen"/> </constructor-arg> <! -- Text obfuscation --> <constructor-arg index = "3"> <list> <! -- <Ref bean = "baffleDecorator"/> --> </list> </constructor-arg> </bean> <bean id = "baffleDecorator" class = "com. octo. captcha. component. image. textpaster. textdecorator. baffleTextDecorator "> <constructor-arg type =" java. lang. integer "index =" 0 "> <value> 1 </value> </constructor-arg> <constructor-arg type =" java. awt. color "index =" 1 "> <ref bean =" colorWrite "/> </constructor-arg> </bean> <bean id =" colorGen "class =" com. octo. captcha. component. image. color. singleColorGenerator "> <constructor-arg type =" java. awt. color "index =" 0 "> <ref bean =" colorBlack "/> </constructor-arg> </bean> <bean id =" colorWrite "class =" java. awt. color "> <constructor-arg type =" int "index =" 0 "> <value> 255 </value> </constructor-arg> <constructor-arg type =" int "index =" 1 "> <value> 255 </value> </constructor-arg> <constructor-arg type =" int "index =" 2 "> <value> 255 </value> </constructor-arg> </bean> <bean id = "colorBlack" class = "java. awt. color "> <constructor-arg type =" int "index =" 0 "> <value> 50 </value> </constructor-arg> <constructor-arg type =" int "index =" 1 "> <value> 50 </value> </constructor-arg> <constructor-arg type =" int "index =" 2 "> <value> 50 </value> </constructor-arg> </bean> </beans>View Code
3. web. xml integration ***. xml
<Servlet> <servlet-name> Jcaptcha </servlet-name> <servlet-class> com. rbao. east. servlet. jcaptchaServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name> Jcaptcha </servlet-name> <url-pattern>/captcha. svl </url-pattern> </servlet-mapping>View Code
4. Create a servlet that provides the verification code
Import java. awt. image. bufferedImage; import java. io. IOException; import javax. imageio. imageIO; import javax. servlet. servletException; import javax. servlet. servletOutputStream; import javax. servlet. http. httpServlet; import javax. servlet. http. httpServletRequest; import javax. servlet. http. httpServletResponse; import org. apache. commons. io. output. byteArrayOutputStream; import org. springframework. beans. factory. be AnFactoryUtils; import org. springframework. web. context. webApplicationContext; import org. springframework. web. context. support. webApplicationContextUtils; import com. octo. captcha. service. captchaServiceException; import com. octo. captcha. service. image. imageCaptchaService;/*** Servlet that provides the verification code image */@ SuppressWarnings ("serial") public class JcaptchaServlet extends HttpServlet {public static final String CAPTCHA _ IMAGE_FORMAT = "jpeg"; private ImageCaptchaService captchaService; @ Override public void init () throws ServletException {WebApplicationContext appCtx = WebApplicationContextUtils. getWebApplicationContext (getServletContext (); captchaService = (ImageCaptchaService) BeanFactoryUtils. beanOfTypeIncludingAncestors (appCtx, ImageCaptchaService. class) ;}@ Override protected void doGet (HttpServletReque St request, HttpServletResponse response) throws ServletException, IOException {byte [] bytes = null; // the output stream to render the captcha image as jpeg into ByteArrayOutputStream extends outputstream = new bytes (); try {// get the session id that will identify the generated captcha. // the same id must be used to validate the response, the session id // is a go Od candidate! String captchaId = request. getSession (). getId (); BufferedImage challenge = captchaService. getImageChallengeForID (captchaId, request. getLocale (); // Jimi. putImage ("image/jpeg", challenge, jpegOutputStream); ImageIO. write (challenge, CAPTCHA_IMAGE_FORMAT, writable outputstream);} catch (IllegalArgumentException e) {response. sendError (HttpServletResponse. SC _NOT_FOUND); return;} catch (CaptchaServiceException e) {response. sendError (HttpServletResponse. SC _INTERNAL_SERVER_ERROR); return;} // catch (JimiException e) {// response. sendError (HttpServletResponse. SC _INTERNAL_SERVER_ERROR); // return; //} captchaChallengeAsJpeg = extends outputstream. toByteArray (); // flush it in the response. setHeader ("Cache-Control", "no-store"); response. setHeader ("Pragma", "no-cache"); response. setDateHeader ("Expires", 0); response. setContentType ("image/" + CAPTCHA_IMAGE_FORMAT); ServletOutputStream responseOutputStream = response. getOutputStream (); responseOutputStream. write (captchaChallengeAsJpeg); responseOutputStream. flush (); responseOutputStream. close ();}}View Code
I don't know how to write it at the beginning. Let's do it first. Record some technical or problems you encounter at work.