【Hutool】验证码

引入Hutool

		<!-- hutool工具类-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.3.3</version>
        </dependency>

获得验证码图片 并把captcha写入session

@GetMapping("/getImage")
 public String ImgCode() {
   LineCaptcha captcha = CaptchaUtil.createLineCaptcha(150, 50, 4, 6);
   ByteArrayInputStream imageStream = new ByteArrayInputStream(captcha.getImageBytes());
   WebUtils.getSession().setAttribute("cap", captcha);
   //将验证码写进浏览器
   ServletOutputStream outputStream = null;

   try {
     outputStream = WebUtils.getResponse().getOutputStream();
   } catch (IOException e) {
     e.printStackTrace();
   }
   captcha.write(outputStream);
   return null;
 }

前端请求接口得到验证码

<div class="layui-form-item input-item" id="imgCode">
        <label for="code">验证码</label>
        <input type="text" placeholder="请输入验证码" autocomplete="off" id="code" class="layui-input" style="width: 60%;" name="vername">
        <img src="/login/getImage" style="width:100px;height:40px">
    </div>

在这里插入图片描述

后端处理得到的验证码

@PostMapping("/login")
public Result login(String loginname, String pwd,String vername){
    //验证码
    LineCaptcha captcha = (LineCaptcha)WebUtils.getSession().getAttribute("cap");
    //判断验证码是否输入正确
    boolean verify = captcha.verify(vername);
    if(!verify)
      return Result.LOGIN_ERROR_VERIFY;
     ...
}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值