vue+springboot realizes login verification code

vue+springboot realizes login verification code

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:
  • Vue implementation of verification code login method example
  • Vue implements graphic verification code when logging in
  • Vue implements mobile phone verification code login
  • Use vue to realize the registration page effect vue to realize SMS verification code login
  • Vue implements login verification code
  • Vue implements graphic verification code login
  • Vue implements picture verification code when logging in
  • Vue implements mobile phone number and verification code login (60s countdown disabled)
  • Vue implements the verification code function of the login interface
  • vue+Element implements random verification code for login

<<:  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

Recommend

Solution to ElementUI's this.$notify.close() call not working

Table of contents Requirement Description Problem...

How to Rename a Group of Files at Once on Linux

In Linux, we usually use the mv command to rename...

Detailed explanation of special phenomena examples of sleep function in MySQL

Preface The sleep system function in MySQL has fe...

Vue3 Documentation Quick Start

Table of contents 1. Setup 1. The first parameter...

An article to understand the advanced features of K8S

Table of contents K8S Advanced Features Advanced ...

JavaScript recursion detailed

Table of contents 1. What is recursion? 2. Solve ...

MySQL limit performance analysis and optimization

1. Conclusion Syntax: limit offset, rows Conclusi...

Element uses scripts to automatically build new components

Table of contents background How does element-ui&...

Best tools for taking screenshots and editing them in Linux

When I switched my primary operating system from ...

An article to understand the execution process of MySQL query statements

Preface We need to retrieve certain data that mee...

Setting up a proxy server using nginx

Nginx can use its reverse proxy function to imple...

Example of customizing the style of the form file selection box

Copy code The code is as follows: <!DOCTYPE ht...