Home> Java> javaTutorial> body text

Introduction to usage examples of verification code combined with springboot when logging in

零下一度
Release: 2017-06-17 11:48:06
Original
1642 people have browsed it

In a web applicationVerification codeis a common element. Whether it is preventing robots or crawlers, it has a certain effect. The following article mainly introduces the relevant information about the login verification code kaptcha combined with spring boot usage. Friends in need can refer to it. Let’s take a look together.

Preface

When ouruser logs in, for security reasons, the verification code function will be added. Google's kaptcha is used here; spirngboot is lightweight and independent, making spring-based application development particularly simple. There are many introductions to springboot on the Internet, so I won’t go into details here.

Let’s get back to the point. Let’s talk about the usage of verification code combined with springboot when logging in.

The jar package needed to introduce kaptcha is what I use here. What is maven


 com.github.penggle kaptcha 2.3.2   javax.servlet-api javax.servlet   
Copy after login

is to remove the servlet package that comes with the package. In my personal understanding, springboot is a lightweight micro-architecture built with javaconfig and annotations.

The following is the javaconfig of kapcha


@Configuration public class CaptchaConfig { @Bean(name="captchaProducer") public DefaultKaptcha getKaptchaBean(){ DefaultKaptcha defaultKaptcha=new DefaultKaptcha(); Properties properties=new Properties(); properties.setProperty("kaptcha.border", "yes"); properties.setProperty("kaptcha.border.color", "105,179,90"); properties.setProperty("kaptcha.textproducer.font.color", "blue"); properties.setProperty("kaptcha.image.width", "125"); properties.setProperty("kaptcha.image.height", "45"); properties.setProperty("kaptcha.session.key", "code"); properties.setProperty("kaptcha.textproducer.char.length", "4"); properties.setProperty("kaptcha.textproducer.font.names", "宋体,楷体,微软雅黑"); Config config=new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; } }
Copy after login

The javaconfig of katcha here is equivalent to the bean configuration in springmvc. The following is a solution for the above Bean example of javaconfig's springmvc, for reference


     yes 105,179,90 blue 125 45 45 code 4 宋体,楷体,微软雅黑     
Copy after login

Among them, the attribute parameters in theconstruction methodcan be set according to your own needs.

Configuration filehas been configured, so how to get your own QR code? My understanding is the concept of canvas, and then generate the corresponding canvas from the generated four-digit verification code. Then let the result be written out.

The code is as follows:


@RequestMapping(value = "/captcha-image") public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception { 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 capText = captchaProducer.createText(); System.out.println("capText: " + capText); try { String uuid=UUIDUtils.getUUID32().trim().toString(); redisTemplate.opsForValue().set(uuid, capText,60*5,TimeUnit.SECONDS); Cookie cookie = new Cookie("captchaCode",uuid); response.addCookie(cookie); } catch (Exception e) { e.printStackTrace(); } BufferedImage bi = captchaProducer.createImage(capText); ServletOutputStream out = response.getOutputStream(); ImageIO.write(bi, "jpg", out); try { out.flush(); } finally { out.close(); } return null; }
Copy after login

As the above code, use the verification code and cooike when the user logs in captchacode to achieve uniqueness verification. At first, I considered putting it in the session. After thinking about it at the time, I felt that it was unscientific. For example, if I put the captchacode in the session, there would be one verification code, and then another user would Log in, the previous user is still logging in, and a series of problems will occur at this time. Cookies and redis are used here to handle concurrent login verification of users.

The page is relatively simple to use as follows:


点击更换

Copy after login

If you want to change, add a click event, and then clear the previous redis The correspondingcached data; or when obtaining the verification code, set the life cycle.

The above is the detailed content of Introduction to usage examples of verification code combined with springboot when logging in. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!