在SPringBoot生成验证码

        1.引入依赖,这个依赖中包含了生成验证码的工具类

        
        
            cn.hutool
            hutool-all
            5.3.9
        

        2.编写配置类

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());
        }
 }

在SPringBoot生成验证码_第1张图片

        3.重启该项目,打开浏览器访问该接口,就可以看到我们生成的验证码

在SPringBoot生成验证码_第2张图片

         4.获取该验证码来进行判断,一个用户输入的,一个用工具类生成的

    @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);
    }

        

        在SPringBoot项目中用于返回响应结果的工具类

import java.io.Serializable;

/**
 * 响应结果类
 * @param  响应数据的类型
 */
public class ResponseResult 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  ResponseResult getResponseResult() {
        return new ResponseResult<>(OK, null, null);
    }

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

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

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

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

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

        用这个工具类来统一给客户端返回结果在SPringBoot生成验证码_第3张图片

 

你可能感兴趣的:(spring,boot,spring,boot,java,spring)