The SpringMvc project uses GoogleKaptcha to generate a verification code.

Source: Internet
Author: User

The SpringMvc project uses GoogleKaptcha to generate a verification code.
Google captcha is a tool class for google to generate verification codes. Its principle is to save the randomly generated string to the session and return it to the page as an image, then, the foreground page is submitted to the background for comparison.

 

1. Prepare the jar package

The official pom should be

<dependency>      <groupId>com.google.code.kaptcha</groupId>      <artifactId>kaptcha</artifactId>      <version>2.3.2</version>  </dependency>

But it cannot be downloaded. The pom file I found in Alibaba's maven repository is as follows:

<dependency>      <groupId>com.github.penggle</groupId>      <artifactId>kaptcha</artifactId>      <version>2.3.2</version>  </dependency>

Test can be normal download, here recommended Ali maven repository, download speed is okay, very stable, with address: http://maven.aliyun.com/nexus/#welcome

2. spring bean Configuration
1 <! -- Configuration of google kaptcha --> 2 <bean id = "captchaProducer" class = "com. google. code. kaptcha. impl. defaultKaptcha "> 3 <property name =" config "> 4 <bean class =" com. google. code. kaptcha. util. config "> 5 <constructor-arg> 6 <props> 7 <! -- Whether a border exists: yes or no --> 8 <prop key = "kaptcha. border"> yes </prop> 9 <! -- Border color --> 10 <prop key = "kaptcha. border. color"> 105,179, 90 </prop> 11 <! -- Verification Code text character color --> 12 <prop key = "kaptcha. textproducer. font. color"> blue </prop> 13 <! -- Verification Code text character size --> 14 <prop key = "kaptcha. textproducer. font. size"> 45 </prop> 15 <! -- The default width of the Verification Code image is 200 --> 16 <prop key = "kaptcha. image. width"> 125 </prop> 17 <! -- The height of the Verification Code image defaults to 50 --> 18 <prop key = "kaptcha. image. height"> 45 </prop> 19 <! -- The default length of the Verification Code text is 5 --> 20 <prop key = "kaptcha. textproducer. char. length"> 4 </prop> 21 <! -- The default Font style of the Verification Code text is new Font ("Arial", 1, fontSize), new Font ("Courier", 1, fontSize) --> 22 <prop key = "kaptcha. textproducer. font. names ">,, 23 </prop> 23 </props> 24 </constructor-arg> 25 </bean> 26 </property> 27 </bean>
3. Two Methods of Controller
1 package com. ccg. controller; 2 3 import java. awt. image. bufferedImage; 4 import java. io. IOException; 5 import java. io. printWriter; 6 7 import javax. annotation. resource; 8 import javax. imageio. imageIO; 9 import javax. servlet. servletOutputStream; 10 import javax. servlet. http. httpServletRequest; 11 import javax. servlet. http. httpServletResponse; 12 import javax. servlet. http. httpSession; 13 14 import org. springframework. stereotype. controller; 15 import org. springframework. web. bind. annotation. requestMapping; 16 import org. springframework. web. bind. annotation. requestParam; 17 import org. springframework. web. servlet. modelAndView; 18 19 import com. google. code. kaptcha. constants; 20 import com. google. code. kaptcha. producer; 21 22 @ Controller23 @ RequestMapping ("captcha") 24 public class CaptchaController {25 26 @ Resource27 private Producer captchaProducer; 28/** 29*30 * Get the verification code picture 31 * @ author ccg32 * @ param request33 * @ param response34 * @ return35 * @ throws IOException36 * Created January 17, 2017: 2837 */38 @ RequestMapping ("getCaptchaCode") 39 public ModelAndView getCaptchaCode (HttpServletRequest request, HttpServletResponse response) throws IOException {40 HttpSession session = request. getSession (); 41 42 response. setDateHeader ("Expires", 0); 43 response. setHeader ("Cache-Control", "no-store, no-cache, must-revalidate"); 44 response. addHeader ("Cache-Control", "post-check = 0, pre-check = 0"); 45 response. setHeader ("Pragma", "no-cache"); 46 response. setContentType ("image/jpeg"); 47 48 // generate Verification Code text 49 String capText = captchaProducer. createText (); 50 session. setAttribute (Constants. KAPTCHA_SESSION_KEY, capText); 51 System. out. println ("generate Verification Code text ====" + capText); 52 // use the generated string to construct the image 53 BufferedImage bi = captchaProducer. createImage (capText); 54 ServletOutputStream out = response. getOutputStream (); 55 ImageIO. write (bi, "jpg", out); 56 57 try {58 out. flush (); 59} finally {60 out. close (); 61} 62 return null; 63} 64 65/** 66*67 * comparison between the verification code entered by the front-end and the generated Verification Code 68 * @ author ccg69 * @ param request70 * @ param response71 * @ param captchaCode72 * Created on the afternoon of April 9, January 17, 2017 5: 34: 2373 */74 @ RequestMapping ("checkCaptchaCode") 75 public void checkCaptchaCode (HttpServletRequest request, HttpServletResponse response, @ RequestParam ("captchaCode") String captchaCode) {76 System. out. println ("Enter verification code =" + captchaCode); 77 78 response. setCharacterEncoding ("UTF-8"); 79 response. setHeader ("Pragma", "No-cache"); 80 response. setHeader ("Cache-Control", "no-cache"); 81 response. setDateHeader ("Expires", 0); 82 83 String generateCode = (String) request. getSession (). getAttribute (Constants. KAPTCHA_SESSION_KEY); 84 String result = ""; 85 if (generateCode. equals (captchaCode) {86 result = "verified"; 87} else {88 result = "input error"; 89} 90 PrintWriter out = null; 91 try {92 out = response. getWriter (); 93} catch (IOException e) {94 e. printStackTrace (); 95} 96 out. print (result); 97 out. flush (); 98} 99}
4. Front-end Page code
1 <% @ page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> 2 <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" http://www.w3.org/TR/html4/loose.dtd "> 3 

You need to note that jquery. min. js is referenced

5. Running Effect
 

 

Configurable Google Captcha

1 kaptcha. if the border of border is set to true by default, we can set yes, no 2 kaptcha. border. the color border Color is color by default. BLACK 3 kaptcha. border. the default width of the thickness border is 1 4 kaptcha. producer. the default impl verification code generator is defakapkaptcha 5 kaptcha. textproducer. the default value of impl Verification Code text generator is defatexttextcreator 6 kaptcha. textproducer. char. the content range of string Verification Code text characters is abcde2345678gfynmnpwx 7 kaptcha by default. textproducer. char. length Verification Code text character length is 5 8 kaptcha by default. textproducer. font. names Verification Code text Font style default new Font ("Arial", 1, fontSize), new Font ("Courier", 1, fontSize) 9 kaptcha. textproducer. font. size Verification Code text character size: 40 10 kaptcha by default. textproducer. font. color Verification Code text character Color is color by default. BLACK 11 kaptcha. textproducer. char. the space Verification Code text Character spacing is 2 12 kaptcha by default. noise. the default value of impl Verification Code noise generation object is DefaultNoise 13 kaptcha. noise. color Verification Code noise Color is color by default. BLACK 14 kaptcha. obscurificator. the default impl Verification Code style engine is WaterRipple 15 kaptcha. word. the default value of impl Verification Code text character rendering is DefaultWordRenderer 16 kaptcha. background. the default value of impl Verification Code background generator is DefaultBackground 17 kaptcha. background. clear. from verification code background Color gradual default is Color. LIGHT_GRAY 18 kaptcha. background. clear. to Verification Code background Color gradual default is Color. WHITE 19 kaptcha. image. width Verification Code the image width defaults to 200 20 kaptcha. image. height Verification Code Image height: 50 by default

 

If you have any questions, please leave a message ~

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: info-contact@alibabacloud.com 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.