In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail how to achieve the login CAPTCHA function of java. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
Login verification code
Login verification is a function that most login systems will use, and there are many ways of verification, such as login verification code, login verification bar and jigsaw puzzle drag block, and so on. Here we will talk about the example of entering login verification code to achieve. First of all, kaptcha is an open source verification code implementation library, using this library can be very convenient to achieve the function of verification code.
1. Add dependency
Add a kaptcha dependent package under the pom file
Com.github.axet kaptcha 0.0.9
two。 Add configuration
Create a new config package, create a kaptcha configuration class under the package, and configure some of the generated properties of the CAPTCHA.
KaptchaConfig.java
/ * * @ author: yzy * @ Date: 2020-6-11 10:41 * @ Description: configuration of CAPTCHA * / @ Configurationpublic class CaptchaConfig {@ Bean public DefaultKaptcha producer () {Properties properties = new Properties (); properties.put ("kaptcha.border", "no"); properties.put ("kaptcha.textproducer.font.color", "black"); properties.put ("kaptcha.textproducer.char.space", "5") Config config = new Config (properties); DefaultKaptcha defaultKaptcha = new DefaultKaptcha (); defaultKaptcha.setConfig (config); return defaultKaptcha;}}
3. Generate code
Create a new controller, provide the API related to the system login, and add the CAPTCHA generation interface to it.
LoginController.java
/ * * @ author: yzy * @ Date: 10:58 * @ Description on 2020-6-11: log in to the controller * / @ RestControllerpublic class LoginController {@ Resource private Producer producer / * * @ Description: CAPTCHA generation API * @ Author: yzy * @ Date: 11:00 * @ Param: response * @ Param: request * @ Return: void * @ Exception * * / @ RequestMapping (value = "/ captcha.jpg", method = RequestMethod.GET) public void captcha (HttpServletResponse response) HttpServletRequest request) {/ * Cache-Control specifies the caching mechanism that requests and responses follow * no-store: used to prevent important information from being unintentionally published. Sending in a request message causes both the request and response messages to be cached. * no-cache: indicates that request or response messages cannot be cached * / response.setHeader ("Cache-Control", "no-store,no-cache"); / / sets the output stream content format to picture format .image / jpeg, which is used to generate image random code response.setContentType ("image/jpeg"); / / generate text verification code String text = producer.createText () / / generate image verification code BufferedImage image = producer.createImage (text); / / Save the verification code to session request.getSession (). SetAttribute (Constants.KAPTCHA_SESSION_KEY,text); ServletOutputStream outputStream = null; try {outputStream = response.getOutputStream (); ImageIO.write (image, "jpg", outputStream) } catch (IOException e) {e.printStackTrace ();} IOUtils.closeQuietly (outputStream);}}
Test interface
After the compilation is successful, visit http://localhost:8010/swagger-ui.html and go to the swagger test page. The test result is shown in the figure:
So it's done!
This is the end of this article on "how to achieve login CAPTCHA function in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
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.