springboot+vue简单验证码

1.通过controller映射到html中验证码元素

 @RequestMapping("/login")
    public String tologin(Model model,HttpServletRequest request){
        Random r = new Random();
        String str = "";
        for(int i = 0;i<4;i++){
            str+=r.nextInt(10);
        }
        HttpSession session = request.getSession();
        session.setAttribute("yzm",str);
        model.addAttribute("yzm",str);
        return "login";
    }

2.在html中写入验证码

<input type="text" class="input input-big" name="code" placeholder="填写右侧的验证码" data-validate="required:请填写右侧的验证码" v-model="yzm"/><span th:text="${yzm}"></span>

2.在vue中data里面加入元素

springboot+vue简单验证码_第1张图片
springboot+vue简单验证码_第2张图片
3.判断验证码

String yzm = request.getParameter("yzm");//验证码的
if(!yzm.equals(session.getAttribute("yzm"))){//验证码的
            data.put("success",3);
            data.put("info","验证码不正确");
            return data;
        }

你可能感兴趣的:(Java,框架学习)