This article example shares the specific code of vue+springboot to implement the login verification code for your reference. The specific content is as follows First look at the effect picture Add verification code html to the login page Add kaptcha dependency in the backend pom file <dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version> </dependency> Create KaptchaConfig tool class package com.brilliance.module.system.controller.util; import com.google.code.kaptcha.impl.DefaultKaptcha; import com.google.code.kaptcha.util.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Properties; @Configuration public class KaptchaConfig { @Bean public DefaultKaptcha getDefaultKaptcha() { DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); Properties properties = new Properties(); // Image width properties.setProperty("kaptcha.image.width", "180"); // Image height properties.setProperty("kaptcha.image.height", "50"); // Picture border properties.setProperty("kaptcha.border", "yes"); // Border color properties.setProperty("kaptcha.border.color", "105,179,90"); // Font color properties.setProperty("kaptcha.textproducer.font.color", "blue"); // font size properties.setProperty("kaptcha.textproducer.font.size", "40"); //session key properties.setProperty("kaptcha.session.key", "code"); // Verification code length properties.setProperty("kaptcha.textproducer.char.length", "4"); // font properties.setProperty("kaptcha.textproducer.font.names", "Songti, Kaiti, Microsoft YaHei"); Config config = new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; } } In the Controller, the generated verification code is stored in redis for later verification (the configuration and dependencies of redis can be found on Baidu) @RestController @RequestMapping("/api/user") @Api(tags = "System User Management") public class SysUserController extends AbstractController { @Autowired private SysUserService sysUserService; @Autowired private DefaultKaptcha defaultKaptcha; @Autowired RedisTemplate redisTemplate; @GetMapping("/createImageCode") public void createImageCode(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setHeader("Cache-Control", "no-store, no-cache"); response.setContentType("image/jpeg"); // Generate text verification code String text = defaultKaptcha.createText(); // Generate image verification code BufferedImage image = defaultKaptcha.createImage(text); // Here we use redis to cache the value of the verification code and set the expiration time to 60 seconds redisTemplate.opsForValue().set("imageCode",text,60, TimeUnit.SECONDS); ServletOutputStream out = response.getOutputStream(); ImageIO.write(image, "jpg", out); out.flush(); out.close(); } After generation, the verification code needs to be verified on the login interface to see if it is correct. Add a request judgment on the outer layer of the login button to verify whether the verification code entered is correct, and prompt an error message based on the return value @PostMapping("/compareCode") public RESULT compareCode(@RequestBody String verificationCode) { if(!redisTemplate.hasKey("imageCode")) { return RESULT.error(500,"Verification code has expired"); } String code = redisTemplate.opsForValue().get("imageCode").toString(); if(StringUtils.equals(verificationCode,code)) { return RESULT.ok(); } else { return RESULT.error(500,"Incorrect verification code input"); } } The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM. You may also be interested in:
|
<<: Solution to the problem that mysql cannot start after modifying the default path of the database
>>: Detailed tutorial on installing mysql-8.0.13 (zip installation) on windows 10 system
The so-called connection limit in Nginx is actual...
Table of contents Requirement Description Problem...
In Linux, we usually use the mv command to rename...
Preface Tomcat is an excellent Java container, bu...
Preface The sleep system function in MySQL has fe...
Table of contents 1. Setup 1. The first parameter...
Table of contents K8S Advanced Features Advanced ...
Table of contents 1. What is recursion? 2. Solve ...
1. Conclusion Syntax: limit offset, rows Conclusi...
Table of contents background How does element-ui&...
Find the installation directory of VirtualBox. Th...
When I switched my primary operating system from ...
Preface We need to retrieve certain data that mee...
Nginx can use its reverse proxy function to imple...
Copy code The code is as follows: <!DOCTYPE ht...