Captcha验证码使用,算术,中文,数字

该博客介绍如何在Springboot项目中结合Vue.js实现Captcha验证码功能。内容涉及pom.xml配置、Java后端代码实现以及Vue前端的转码处理和页面展示,最后展示了验证码的实现效果。

这里使用的是Springboot+vue的布置
pom.xml

<!--验证码-->
    <dependency>
      <groupId>com.github.whvcse</groupId>
      <artifactId>easy-captcha</artifactId>
      <version>1.6.2</version>
    </dependency>

Java使用代码

//Result 是自定义的提交类
 @GetMapping("/captcha")
  public Result captchak(HttpServletResponse response) throws IOException {
    ServletOutputStream outputStream = response.getOutputStream();
    //算术验证码 数字加减乘除. 建议2位运算就行:captcha.setLen(2);
    //ArithmeticCaptcha captcha = new ArithmeticCaptcha(120, 40);
    // 中文验证码
    ChineseCaptcha captcha = new ChineseCaptcha(120, 40);
    // 英文与数字验证码
    // SpecCaptcha captcha = new SpecCaptcha(120, 40);
    //英文与数字动态验证码
    //  GifCaptcha captcha = new GifCaptcha(120, 40);
    // 中文动态验证码
    //ChineseGifCaptcha captcha = new ChineseGifCaptcha(120, 40);
    // 几位数运算,默认是两位
    captcha.setLen(2);
    // 获取运算的结果
    String result = captcha.text();
    redisUtil.set("captchaImg", result, 50);//存储在redis中用来验证
    System.out.println(result);
    captcha.out(outputStream);
    return Result.succ(captcha.out(outputStream));
  }

VUE
vue使用的时侯要进行转码

export function getCaptcha() {
  return request({
    url: 'http://127.0.0.1:8088/captcha',//请求方法
    method: 'get',
    responseType: 'blob',//主要做用
  })
}

页面

//页面显示
el-image :src="captchaImg" class="captchaImg" @click="getCaptcha"></el-image>

//引用封装的方法
import {getCaptcha} from "../axios";

//调用方法进行转码
getCaptcha() {
      getCaptcha().then(res => {
        const myBlob = new window.Blob([res.data], {type: 'image/jpeg'}) //转码
        this.captchaImg = window.URL.createObjectURL(myBlob);//转成路劲链接
        this.ruleForm.captchaImg = ''
      })
    }

效果图
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值