前端登录验证码发送

这篇博客介绍了如何在注册过程中通过Axios发送和验证邮箱验证码。首先创建了一个utils包,然后在其中实现了发送验证码的API接口,利用MailUtils生成和发送随机6位验证码,并将验证码存储到本地文件系统。接着,展示了UI层的代码,包括发送和验证验证码的函数,以及在login.vue中触发这些操作的按钮。最后,更新了路由配置,确保发送和验证验证码的请求能够通过白名单。
摘要由CSDN通过智能技术生成

验证码发送

1.在这里创建utils的包并把下面两个文件复制进去

在这里插入图片描述

2.将下列代码放入该文件

在这里插入图片描述 /**
* 注册时候发送验证码
* 先把注册写在这里,这样写不规范,需要优化
*/
@ApiOperation(“发送验证码”)
@PostMapping("/sendCheckCode")
public void sendCheckCode(@RequestParam String email) throws FileNotFoundException {
System.out.println(“email:” + email);
//发送验证码
MailUtils mailUtils = new MailUtils();
//生成随机6位验证码,并且存入数组
String code = mailUtils.randomCode();
//TODO static 优化
mailUtils.sendMail(email, code, “一个价值千万项目的验证码”);

// 将验证码暂时存放 能走缓存尽量走缓存
File file = new File(“D:\SpringBoot\workSpace\DDD5\ems\src\main\java\com\mt\ams\utils”, “checkCode.txt”);
byte bt[] = new byte[1024];
//邮箱转换为字节流
byte btEmail[] = new byte[1024];
btEmail = email.getBytes();

// 邮箱与验证码形成唯一标识
StringBuffer uniqueFlag = new StringBuffer();
uniqueFlag.append(code);
uniqueFlag.append(",");
uniqueFlag.append(email);
byte[] btUnique = new byte[1024];
btUnique = (uniqueFlag.toString()).getBytes();
System.out.println(uniqueFlag);

    bt = code.getBytes();
    //写入文件
    try {
        FileOutputStream in = new FileOutputStream(file);
        try {

// in.write(bt, 0, bt.length);
in.write(btUnique, 0, btEmail.length);
in.close();
System.out.println(“写入文件成功”);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(“发送成功”);
} finally {
System.out.println(“结束”);
}
}

3.将下面两个文件复制到此处(UI)

在这里插入图片描述

4.在此文件中添加以下代码

在这里插入图片描述 //学生注册时候发送验证码
static sendCheckCode (email) {
const query = {
email
}
return axios({
url: ‘/ams/studentEntity/StudentInfo/sendCheckCode’,
method: ‘POST’,
params: query,
});
}

//学生匹配验证码
static checkCode (code) {
const query = {
code
}
return axios({
url: ‘/ams/studentEntity/StudentInfo/checkCode’,
method: ‘POST’,
params: query,
});
}

5.在login.vue中添加下列代码

在这里插入图片描述<a-button block size="large" type="primary" class="stuRegisterButton"> <router-link to="/stuRegister" style="line-height: 22px;">学生注册 </router-link> </a-button>

6.找到这个文件,并添加以下代码

在这里插入图片描述在这里插入图片描述{ path: '/stuRegister', component: () => import('@/views/login/stuRegister'), meta: {title: '学生注册'} },

7.找到下面的文件,作以下修改

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

8.找到下面的文件,添加白名单

在这里插入图片描述/api/ams/studentEntity/StudentInfo/sendCheckCode,/api/ams/studentEntity/StudentInfo/checkCode,/api/ams/studentEntity/StudentInfo/saveStudentInfo,/api/ams/collegeEntity/College/findColleges,/api/sys/user/userRegister

注意:添加了白名单的函数contrller层中对应函数的权限需要删除

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值