Generate verification code in SpringBoot

        1. Introduce dependencies, which include tool classes for generating verification codes

        <!--引入hutool -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.9</version>
        </dependency>

        2. Write configuration class

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
@RequestMapping("code")
public class CodeController {

        @GetMapping("image")
        public void code(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            // 创建一个验证码  长,宽,字符数,干扰元素个数
            LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100, 6, 15);

            // 将验证码放到session中以供前端获取展示
            String code = lineCaptcha.getCode();//真实验证码
            System.err.println(code);
            req.getSession().setAttribute("code",code);
            // 用流写出去
            lineCaptcha.write(resp.getOutputStream());
        }
 }

        3. Restart the project, open the browser to access the interface, and you can see the verification code we generated

         4. Obtain the verification code to judge, one is input by the user, and the other is generated by the tool class

    @RequestMapping("login")
    public ResponseResult login(@RequestBody User user, HttpServletRequest request){
        String c = (String) request.getSession().getAttribute("code");

        String code = user.getCode();

        if(code==null||code==""){
            System.out.println("验证码不能为空");
            return null;
        }

        if(!c.equals(code)){
            System.out.println("验证码不匹配");
            return null;
        }

        return productFeign.login(user);
    }

        

        Tool class used to return response results in the SpringBoot project

import java.io.Serializable;

/**
 * 响应结果类
 * @param <E> 响应数据的类型
 */
public class ResponseResult<E> implements Serializable {
    /** 操作成功的状态码 */
    public static final int OK = 200;
    /** 状态码 */
    private Integer state;
    /** 状态描述信息 */
    private String message;
    /** 数据 */
    private E data;

    public ResponseResult() {
    }

    public ResponseResult(Integer state, String message) {
        this.state = state;
        this.message = message;
    }

    public ResponseResult(Integer state, String message, E data) {
        this.state = state;
        this.message = message;
        this.data = data;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public E getData() {
        return data;
    }

    public void setData(E data) {
        this.data = data;
    }


    public static <E> ResponseResult<E> getResponseResult() {
        return new ResponseResult<>(OK, null, null);
    }

    public static <E> ResponseResult<E> getResponseResult(Integer state) {
        return new ResponseResult<>(state, null, null);
    }

    public static <E> ResponseResult<E> getResponseResult(String message) {
        return new ResponseResult<>(OK, message, null);
    }

    public static <E> ResponseResult<E> getResponseResult(E data) {
        return new ResponseResult<>(OK, null, data);
    }

    public static <E> ResponseResult<E> getResponseResult(Integer state, String message) {
        return new ResponseResult<>(state, message, null);
    }

    public static <E> ResponseResult<E> getResponseResult(String message, E data) {
        return new ResponseResult<>(OK, message, data);
    }
}

        Use this tool class to uniformly return results to the client

 

Guess you like

Origin blog.csdn.net/weixin_68926017/article/details/131809739