添加邮箱接口
This commit is contained in:
parent
5a4fe2aea3
commit
db7ffa2f78
|
@ -5,11 +5,13 @@ import com.example.catchTheLetters.model.dto.RegisterDto;
|
||||||
import com.example.catchTheLetters.entity.User;
|
import com.example.catchTheLetters.entity.User;
|
||||||
import com.example.catchTheLetters.model.vo.UserVo;
|
import com.example.catchTheLetters.model.vo.UserVo;
|
||||||
import com.example.catchTheLetters.service.AuthService;
|
import com.example.catchTheLetters.service.AuthService;
|
||||||
|
import com.example.catchTheLetters.service.EmailService;
|
||||||
import com.example.catchTheLetters.utils.R;
|
import com.example.catchTheLetters.utils.R;
|
||||||
import io.swagger.annotations.Api;
|
import io.swagger.annotations.Api;
|
||||||
import io.swagger.annotations.ApiOperation;
|
import io.swagger.annotations.ApiOperation;
|
||||||
import io.swagger.annotations.ApiParam;
|
import io.swagger.annotations.ApiParam;
|
||||||
import jakarta.annotation.Resource;
|
import jakarta.annotation.Resource;
|
||||||
|
import jakarta.validation.constraints.Email;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@ -27,6 +29,9 @@ public class AuthController {
|
||||||
@Resource
|
@Resource
|
||||||
private AuthService authService;
|
private AuthService authService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private EmailService emailService;
|
||||||
|
|
||||||
@ApiOperation(value = "用户登录")
|
@ApiOperation(value = "用户登录")
|
||||||
@PostMapping("/login")
|
@PostMapping("/login")
|
||||||
public R<String> login(@RequestBody LoginDto loginDto) {
|
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) {
|
public R<UserVo> update(@RequestBody User user, @RequestHeader("token") String token) {
|
||||||
return authService.update(user, 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -99,4 +99,5 @@ public class LevelController {
|
||||||
public R<Long> version() {
|
public R<Long> version() {
|
||||||
return R.ok(versionService.getVersion("level"));
|
return R.ok(versionService.getVersion("level"));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
|
@ -70,4 +70,6 @@ public interface LevelService {
|
||||||
*/
|
*/
|
||||||
boolean update(Level level);
|
boolean update(Level level);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -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("验证失败,请重试");
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,13 @@
|
||||||
spring:
|
spring:
|
||||||
|
mail:
|
||||||
|
password: hvhxwetpqqqqddhd
|
||||||
|
host: smtp.qq.com
|
||||||
|
username: 2960474346@qq.com
|
||||||
|
properties:
|
||||||
|
mail:
|
||||||
|
smtp:
|
||||||
|
ssl:
|
||||||
|
enable: true
|
||||||
application:
|
application:
|
||||||
name: catch-the-letters
|
name: catch-the-letters
|
||||||
data:
|
data:
|
||||||
|
|
Loading…
Reference in New Issue