前后端分离验证码功能

前端代码

<template>      
<el-form-item prop="captcha">
          <div style="display: flex;">
            <el-input style="width: 220px;" placeholder="请输入验证码" size="medium" prefix-icon="el-icon-lock"
                      v-model="user.captcha"></el-input>
            <img :src="captureUrl" @click="getCapture" style="width: 130px;height: 35px;margin-left: 10px"/>
          </div>
        </el-form-item>
</template>
export default {
  data() {
    return {
      //验证码
      captchaKey: '',
      captureUrl: '',
    }
  }


 created() {
    this.captchaKey = Math.random();
    this.captureUrl = "http://localhost:8090/capture/chars?key=" + this.captchaKey;
  },

    //点击图片,刷新验证码
 getCapture() {
    this.captchaKey = Math.random();
    this.captureUrl = "http://localhost:8090/capture/chars?key=" + this.captchaKey;
 }

登陆请求携带验证码的key

     login() {
      this.$refs['userForm'].validate((valid) => {
            if (valid) {  // 表单校验合法
              this.request.post("/user/login?key=" + this.captchaKey, this.user).then(res => {
                    // console.log(res)
                    if (res.code == 200) {
                      this.$router.push("/")
                      this.$message.success("登陆成功")
                      // 将数据存到浏览器中
                      localStorage.setItem("user", JSON.stringify(res.data))
                      localStorage.setItem("menuList", JSON.stringify(res.data.menuList))
                    } else {
                      this.$message.error(res.msg)
                      this.captchaKey = Math.random();
                      this.captureUrl = "http://localhost:8090/capture/chars?key=" + this.captchaKey;
                      this.user.captcha = ''
                    }
                  }
              )
            }
          }
      );
    },

引入依赖

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

获取验证码信息

此处使用了redis,用来存储验证码信息,也可定义全局的map对象,用来存储信息

package com.zam.web.controller;

import com.wf.captcha.ArithmeticCaptcha;
import com.wf.captcha.SpecCaptcha;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * @Author talent_dog
 * @Date 2024/1/25 15:13
 * @Version 1.0
 */
@RestController
@RequestMapping("/capture")
public class CaptureController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    //验证码
    @GetMapping("/chars")
    public void chars(@RequestParam("key") String key, HttpServletRequest request, HttpServletResponse response) throws IOException {
        //字符验证码
//        SpecCaptcha captcha = new SpecCaptcha(130,35,2);
//        captcha.setCharType(SpecCaptcha.TYPE_NUM_AND_UPPER);
//        //前后端分离系统,session不能使用
//        //存入redis中,便于验证
//        stringRedisTemplate.opsForValue().set(key,captcha.text().toLowerCase(),60, TimeUnit.SECONDS);
//        CaptchaUtil.out(captcha,request,response);

        //算数验证码
        ArithmeticCaptcha captcha = new ArithmeticCaptcha(130, 35, 3);
        captcha.setLen(2);
//        captcha.getArithmeticString(); //获取运算公式
        String value = captcha.text();  //获取结果
        stringRedisTemplate.opsForValue().set(key, value, 60, TimeUnit.SECONDS);
        CaptchaUtil.out(captcha, request, response);
    }
}

用户对象中创建captcha属性,直接获取前端输入的验证码

进行校验

  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

talent_smile

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值