邮箱验证码

This commit is contained in:
wuchunlei
2023-12-25 15:37:24 +08:00
parent b19d04f09d
commit f9d95718cd
4 changed files with 91 additions and 6 deletions

View File

@@ -92,7 +92,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
<version>1.6.2</version>
</dependency>
<dependency>
<groupId>cn.songxinqiang</groupId>
<artifactId>com.baidu.ueditor</artifactId>

View File

@@ -0,0 +1,45 @@
package com.peanut.common.utils;
import javax.mail.Authenticator;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
public class MailUtil {
private final static String mailName = "fengzidushu@163.com";
private final static String mailToken = "GJIPUCQLQYTMDMPH";
public static void sendMail(String title,String content,String receiveAddress) throws Exception{
//smtp服务器
Properties pros=new Properties();
pros.put("mail.smtp.host", "smtp.163.com");//主机名
pros.put("mail.smtp.port", "25");//主机端口号
pros.put("mail.smtp.auth", "true");//是否需要用户认证
pros.put("mail.smtp.starttls.enable", "true");//启用TLS加密
//创建会话
Session session=Session.getInstance(pros,new Authenticator(){
@Override
protected javax.mail.PasswordAuthentication getPasswordAuthentication(){
return new javax.mail.PasswordAuthentication(mailName, mailToken);
}
});
// System.out.println(session);
//设置debug模式便于调试
session.setDebug(true);
MimeMessage message=new MimeMessage(session);
//邮件标题
message.setSubject(title);
//邮件内容(文本)
message.setText(content,"utf-8");
//设置发送方地址
message.setFrom(new InternetAddress(mailName));
//设置收件方地址
message.setRecipient(MimeMessage.RecipientType.TO,new InternetAddress(receiveAddress));
//发送
Transport.send(message);
}
}

View File

@@ -3,7 +3,6 @@ package com.peanut.modules.book.controller;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.TimeUnit;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.druid.util.StringUtils;
@@ -13,6 +12,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.peanut.common.utils.MD5Utils;
import com.peanut.common.utils.MailUtil;
import com.peanut.modules.book.entity.*;
import com.peanut.modules.book.service.*;
import com.peanut.modules.book.to.PageIdDto;
@@ -26,12 +26,10 @@ import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.peanut.common.utils.PageUtils;
import com.peanut.common.utils.R;
/**
*
*
@@ -217,6 +215,32 @@ public class MyUserController {
return R.ok();
}
/**
* 常规注册 发送邮箱验证码
*/
@RequestMapping("/getMailCaptcha")
public R getMailCaptcha(String email) throws Exception {
//验证一分钟内是否已经发过
String redisCode = redisTemplate.opsForValue().get("RegistEMail" + email);
if (!StringUtils.isEmpty(redisCode)) {
long l = Long.parseLong(redisCode.split("_")[1]);
if (System.currentTimeMillis() - l < 60000) {
//60s 内不能再发
return R.error(500,"邮箱验证码频率过高,请稍后再试!");
}
}
//生成随机五位数
Random random = new Random();
String code = random.nextInt(99999) + "";
String timeCode = code + "_"+System.currentTimeMillis();
//redis 缓存验证码
redisTemplate.opsForValue().set("RegistEMail"+email,timeCode,5, TimeUnit.MINUTES);
//发送
MailUtil.sendMail("疯子读书邮箱验证码",code,email);
return R.ok();
}
@RequestMapping("/register")
public R register(@RequestParam("tel") String tel,
@RequestParam("code") String code,
@@ -298,8 +322,13 @@ public class MyUserController {
public R login(@RequestParam("phone") String phone,
@RequestParam("password") String password) {
MyUserEntity user = userService.getBaseMapper().selectOne(new QueryWrapper<MyUserEntity>().eq("tel", phone));
LambdaQueryWrapper<MyUserEntity> wrapper = new LambdaQueryWrapper();
if (phone.contains("@")) {
wrapper.eq(MyUserEntity::getEmail,phone);
}else {
wrapper.eq(MyUserEntity::getTel,phone);
}
MyUserEntity user = userService.getBaseMapper().selectOne(wrapper);
if (user == null) {
return R.error(500,"用户不存在!");
}
@@ -616,6 +645,9 @@ public class MyUserController {
}
//
// /**
// * @Description: app微信登陆

View File

@@ -48,6 +48,10 @@ public class MyUserEntity implements Serializable {
* 电话
*/
private String tel;
/**
* 邮箱
*/
private String email;
/**
* 密码
*/