In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to achieve CAPTCHA in SpringBoot+kaptcha". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "how to achieve SpringBoot+kaptcha CAPTCHA" bar!
1. Basic usage
Kaptcha is a very old CAPTCHA generation tool. How old is it? Can be traced back to 2006.
After so many years, it is not only not lonely but also used by many people, which is enough to show its vitality and is worth studying.
For convenience, let's demonstrate its usage through a whole Spring Boot project.
First create a new Spring Boot project, and then add the dependency of kaptcha, as follows:
Com.github.penggle kaptcha 2.3.2
Next, we just need to provide a Bean that configures Kaptcha, as follows:
@ Configurationpublic class KaptchaConfig {@ Bean (name = "captchaProducer") public DefaultKaptcha getKaptchaBean () {DefaultKaptcha defaultKaptcha = new DefaultKaptcha (); Properties properties = new Properties (); / / if there is a border default to true, we can set yes,no properties.setProperty (KAPTCHA_BORDER, "yes") / / CAPTCHA text character color defaults to Color.BLACK properties.setProperty (KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black"); / / CAPTCHA image width defaults to 200properties.setProperty (KAPTCHA_IMAGE_WIDTH, "160"); / / CAPTCHA image height defaults to 50 properties.setProperty (KAPTCHA_IMAGE_HEIGHT," 60 ") / / CAPTCHA text character size defaults to 40 properties.setProperty (KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38"); / / KAPTCHA_SESSION_KEY properties.setProperty (KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode"); / / CAPTCHA text character length defaults to 5 properties.setProperty (KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4") / / the font style of CAPTCHA text defaults to new Font ("Arial", 1, fontSize), new Font ("Courier", 1, fontSize) properties.setProperty (KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier") / / Image style com.google.code.kaptcha.impl.WaterRipple fisheye com.google.code.kaptcha.impl.FishEyeGimpy shadow com.google.code.kaptcha.impl.ShadowGimpy properties.setProperty (KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy"); Config config = new Config (properties); defaultKaptcha.setConfig (config); return defaultKaptcha;}}
Configure each attribute value of the CAPTCHA image in DefaultKaptcha. There are comments in the code about the meaning of each attribute, so I won't say any more.
Next, we return the verification code image in the API, as follows:
AutowiredDefaultKaptcha defaultKaptcha;@GetMapping ("/ img") public void getKaptcha (HttpServletResponse resp) throws IOException {String text = defaultKaptcha.createText (); BufferedImage image = defaultKaptcha.createImage (text); ImageIO.write (image, "jpg", resp.getOutputStream ());}
Here I write the picture to the front end in the form of an IO stream, of course, I can also convert it into a Base64 string and return it to the front end, which is also OK.
Wait, something seems to be missing!
We didn't store the generated CAPTCHA text in session, so we can't verify it when we log in later. Some friends may say, "isn't that easy?" Can't you just save it in the interface?
NONONO!
You see, when we configure DefaultKaptcha bean, there is a line of properties.setProperty (KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode"); which means that the generated CAPTCHA text is automatically stored in session, and the KEY of session is kaptchaCode. But in the actual test, you will find that the above code does not store the text generated by the CAPTCHA in session.
The reason is that the Kaptcha tool actually provides a Servlet to generate the verification code image. If we directly use the verification code Servlet provided by the tool itself, the above configuration will only take effect. In Spring Boot, if you want to configure the Servlet provided by Kaptcha itself, you can do the following:
@ BeanServletRegistrationBean kaptchaServlet () {ServletRegistrationBean bean = new ServletRegistrationBean (); bean.setServlet (new KaptchaServlet ()); bean.addUrlMappings ("/ img"); Properties properties = new Properties (); / / We can set yes,no properties.setProperty (KAPTCHA_BORDER, "yes") by ourselves if there is a frame default to true; / / the text character color of the verification code defaults to Color.BLACK properties.setProperty (KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black"). / / CAPTCHA image width defaults to 200properties.setProperty (KAPTCHA_IMAGE_WIDTH, "160"); / / CAPTCHA image height defaults to 50 properties.setProperty (KAPTCHA_IMAGE_HEIGHT," 60 "); / / CAPTCHA text character size defaults to 40 properties.setProperty (KAPTCHA_TEXTPRODUCER_FONT_SIZE," 38 ") / / KAPTCHA_SESSION_KEY properties.setProperty (KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode"); / / CAPTCHA text character length defaults to 5 properties.setProperty (KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4"); / / CAPTCHA text font style defaults to new Font ("Arial", 1, fontSize), new Font ("Courier", 1, fontSize) properties.setProperty (KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier") / / Image style com.google.code.kaptcha.impl.WaterRipple fisheye com.google.code.kaptcha.impl.FishEyeGimpy shadow com.google.code.kaptcha.impl.ShadowGimpy properties.setProperty (KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy"); Map map = new HashMap ((Map) properties); bean.setInitParameters (map); return bean;}
After the project is started, you can see the CAPTCHA image directly by visiting / img, and the text of CAPTCHA will be automatically stored in session. When the user logs in, the text content of the CAPTCHA can be obtained through session.getAttribute ("kaptchaCode").
However, in many cases, the content returned by the CAPTCHA interface is relatively rich, which may be not only pictures, but also other information. Therefore, we can not meet our requirements by directly matching a Servlet. We can only write the verification code interface by ourselves. If we write the verification code image by ourselves, we have to save the verification code image in session, then properties.setProperty (KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode"); configuration is actually useless, and we don't have to add it.
two。 Custom CAPTCHA text
Of course, we can also customize the CAPTCHA text by providing an implementation class of CAPTCHA text, as follows:
Public class KaptchaTextCreator extends DefaultTextCreator {private static final String [] CNUMBERS = "0 Integer result [] CNUMBERS =" 0 Override public String getText () {random.nextInt (10); int y = random.nextInt (10); StringBuilder suChinese = new StringBuilder (10) Int randomoperands = (int) Math.round (Math.random () * 2); if (randomoperands = = 0) {result = x * y; suChinese.append (CNUMBERS [x]); suChinese.append ("*"); suChinese.append (CNUMBERS [y]) } else if (randomoperands = = 1) {if (! (x = = 0) & & y% x = 0) {result = y / x; suChinese.append (CNUMBERS [y]); suChinese.append ("/"); suChinese.append (CNUMBERS [x]);} else {result = x + y SuChinese.append (CNUMBERS [x]); suChinese.append ("+"); suChinese.append (CNUMBERS [y]);}} else if (randomoperands = = 2) {if (x > = y) {result = x-y; suChinese.append (CNUMBERS [x]) SuChinese.append ("-"); suChinese.append (CNUMBERS [y]);} else {result = y-x; suChinese.append (CNUMBERS [y]); suChinese.append ("-"); suChinese.append (CNUMBERS [x]) }} else {result = x + y; suChinese.append (CNUMBERS [x]); suChinese.append ("+"); suChinese.append (CNUMBERS [y]);} suChinese.append ("=? @" + result); return suChinese.toString ();}}
This code is not difficult to understand, and the generated CAPTCHA text is similar to a string like "1" 1 "1" 2.
In the future, with @ as the dividing line, the string content before @ is drawn on the image, and the content after @ is stored in session, which can be compared with the content uploaded by users.
Of course, we also need to add the following properties when configuring the CAPTCHA to modify the providing class of the CAPTCHA text:
Properties.setProperty (KAPTCHA_TEXTPRODUCER_IMPL, "org.javaboy.tienchin.framework.config.KaptchaTextCreator")
After the configuration is completed, you can use this CAPTCHA directly in the interface in the future. When using it, you should split the generated CAPTCHA text and process it. Part of it is used for drawing and part of it is stored in session.
At this point, I believe you have a deeper understanding of "SpringBoot+kaptcha how to achieve CAPTCHA", might as well come to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.