


Introduction to usage examples of verification code combined with springboot when logging in
In a web applicationVerification code is 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 our user 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
<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> <exclusions> <exclusion> <artifactId>javax.servlet-api</artifactId> <groupId>javax.servlet</groupId> </exclusion> </exclusions> </dependency>
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; } }
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
<bean id="captchaProducer" class="com.google.code.kaptcha.impl.DefaultKaptcha"> <property name="config"> <bean class="com.google.code.kaptcha.util.Config"> <constructor-arg> <props> <prop key="kaptcha.border">yes</prop> <prop key="kaptcha.border.color">105,179,90</prop> <prop key="kaptcha.textproducer.font.color">blue</prop> <prop key="kaptcha.image.width">125</prop> <prop key="kaptcha.image.height">45</prop> <prop key="kaptcha.textproducer.font.size">45</prop> <prop key="kaptcha.session.key">code</prop> <prop key="kaptcha.textproducer.char.length">4</prop> <prop key="kaptcha.textproducer.font.names">宋体,楷体,微软雅黑</prop> </props> </constructor-arg> </bean> </property> </bean>
Among them, the attribute parameters in the construction method can be set according to your own needs.
Configuration file has 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; }
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:
<p style="float: left;"> <i><img src="/static/imghwm/default1.png" data-src="code/captcha-image" class="lazy" style="max-width:90%" id="codeImg" alt="点击更换" title="点击更换" /></i> </p>
If you want to change, add a click event, and then clear the previous redis The corresponding cached 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!

Start Spring using IntelliJIDEAUltimate version...

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

Java...

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software