添加邮箱接口

This commit is contained in:
ETHEREAL 2024-06-08 17:38:49 +08:00
parent 5a4fe2aea3
commit db7ffa2f78
6 changed files with 122 additions and 0 deletions

View File

@ -5,11 +5,13 @@ import com.example.catchTheLetters.model.dto.RegisterDto;
import com.example.catchTheLetters.entity.User;
import com.example.catchTheLetters.model.vo.UserVo;
import com.example.catchTheLetters.service.AuthService;
import com.example.catchTheLetters.service.EmailService;
import com.example.catchTheLetters.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import jakarta.annotation.Resource;
import jakarta.validation.constraints.Email;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
@ -27,6 +29,9 @@ public class AuthController {
@Resource
private AuthService authService;
@Resource
private EmailService emailService;
@ApiOperation(value = "用户登录")
@PostMapping("/login")
public R<String> login(@RequestBody LoginDto loginDto) {
@ -67,4 +72,17 @@ public class AuthController {
public R<UserVo> update(@RequestBody User user, @RequestHeader("token") String token) {
return authService.update(user, token);
}
@ApiOperation(value = "发送邮件")
@PostMapping("/email")
public R<String> emailSend(@RequestHeader("token") String token, String email) {
return emailService.sendEmail(email);
}
@ApiOperation(value = "验证邮箱")
@PostMapping("/verify-email")
public R<String> emailVerify(@RequestHeader("token") String token, String email, String code) {
return emailService.verifyEmail(email, code);
}
}

View File

@ -99,4 +99,5 @@ public class LevelController {
public R<Long> version() {
return R.ok(versionService.getVersion("level"));
}
}

View File

@ -0,0 +1,14 @@
package com.example.catchTheLetters.service;
import com.example.catchTheLetters.utils.R;
public interface EmailService {
/**
* 发送邮件
* @param email
* @return
*/
R<String> sendEmail(String email);
R<String> verifyEmail(String email,String emailCode);
}

View File

@ -70,4 +70,6 @@ public interface LevelService {
*/
boolean update(Level level);
}

View File

@ -0,0 +1,78 @@
package com.example.catchTheLetters.service.impl;
import com.example.catchTheLetters.service.EmailService;
import com.example.catchTheLetters.utils.R;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.util.Date;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@Service
public class EmailServiceImpl implements EmailService {
/**
* 注入邮件工具类
*/
@Resource
JavaMailSenderImpl javaMailSender;
String str = "^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$";
Pattern pattern = Pattern.compile(str);
@Resource
RedisServiceImpl redisService;
@Value("${spring.mail.username}")
private String sendMailer;
@Override
public R<String> sendEmail(String email) {
try {
if (!StringUtils.hasText(email)) {
return R.fail("邮箱为空");
}
Matcher m = pattern.matcher(email);
if (!m.matches()){
return R.fail("格式错误,请重试");
}
//生成邮箱验证码
String emailCode = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase().substring(3, 9);
//新建邮件对象
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(javaMailSender.createMimeMessage(), true);
//邮件发信人
mimeMessageHelper.setFrom(sendMailer);
//邮件收信人
mimeMessageHelper.setTo(email);
//邮件内容
mimeMessageHelper.setText("你的验证码为:" + emailCode);
//邮件信息
mimeMessageHelper.setSentDate(new Date());
//发送邮件
javaMailSender.send(mimeMessageHelper.getMimeMessage());
//将验证码和注册邮箱存入redis
redisService.set(email,emailCode);
return R.ok( "发送成功");
}catch (Exception e){
e.printStackTrace();
return R.fail("邮箱异常,请重试");
}
}
@Override
public R<String> verifyEmail(String email, String emailCode) {
String verify = redisService.get(emailCode).toString();
if (verify.equals(email)){
return R.ok("验证通过");
}
return R.fail("验证失败,请重试");
}
}

View File

@ -1,4 +1,13 @@
spring:
mail:
password: hvhxwetpqqqqddhd
host: smtp.qq.com
username: 2960474346@qq.com
properties:
mail:
smtp:
ssl:
enable: true
application:
name: catch-the-letters
data: