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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how SpringBoot combined with kaptcha CAPTCHA tool is, the content is concise and easy to understand, can definitely brighten your eyes, through the detailed introduction of this article, I hope you can get something.
First, add kaptcha dependencies to pom.xml
Com.github.penggle kaptcha javax.servlet-api javax.servlet
Second, create a CaptchaConfig class in the project and configure the CAPTCHA.
@ Configurationpublic class CaptchaConfig {@ Bean (name = "captchaProducer") public DefaultKaptcha getKaptchaBean () {DefaultKaptcha defaultKaptcha = new DefaultKaptcha (); Properties properties = new Properties (); / / whether there is a border properties.setProperty (KAPTCHA_BORDER, "no"); / / CAPTCHA text character color properties.setProperty (KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black") / / CAPTCHA text character size properties.setProperty (KAPTCHA_TEXTPRODUCER_FONT_SIZE, "38"); / / KAPTCHA_SESSION_KEY properties.setProperty (KAPTCHA_SESSION_CONFIG_KEY, "kaptchaCode"); / / CAPTCHA text character length properties.setProperty (KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4") / / text font style properties.setProperty (KAPTCHA_TEXTPRODUCER_FONT_NAMES, "Arial,Courier"); / / Image style properties.setProperty (KAPTCHA_OBSCURIFICATOR_IMPL, "com.google.code.kaptcha.impl.ShadowGimpy"); Config config = new Config (properties); defaultKaptcha.setConfig (config); return defaultKaptcha;}}
Next, write the Controller class to generate the CAPTCHA.
@ Controllerpublic class CaptchaController {private static final Logger log = LoggerFactory.getLogger (CaptchaController.class); @ Resource (name = "captchaProducer") / / bean private Producer captchaProducer; @ RequestMapping ("/ captcha") public ModelAndView captcha (HttpServletRequest request, HttpServletResponse response) {ServletOutputStream out = null; try {HttpSession session = request.getSession () 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 capStr = null; String code = null; BufferedImage bi = null; capStr = code = captchaProducer.createText (); bi = captchaProducer.createImage (capStr) Log.info ("CAPTCHA:" + code); session.setAttribute (Constants.KAPTCHA_SESSION_KEY, code); out = response.getOutputStream (); ImageIO.write (bi, "jpg", out); out.flush () } catch (Exception e) {e.printStackTrace ();} finally {try {if (out! = null) {out.close () } catch (IOException e) {e.printStackTrace ();}} return null;}}
Then, write the page code, and this example uses layui.
Finally, run the project, check the results, and verify that the CAPTCHA on the page is consistent with the actual output CAPTCHA.
Log output:
INFO c.p.a.w.c.CaptchaController-[captcha,47]-CAPTCHA: the above content of pfcd is what SpringBoot combines with kaptcha CAPTCHA tool. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.