发送邮箱验证码功能初步实现

pull/2/head
2991692032 2 months ago
parent 5c148bb642
commit 98c9cd64b9

@ -9,6 +9,7 @@
"preview": "vite preview" "preview": "vite preview"
}, },
"dependencies": { "dependencies": {
"@vue/shared": "^3.5.13",
"axios": "^1.8.3", "axios": "^1.8.3",
"element-plus": "^2.9.7", "element-plus": "^2.9.7",
"vee-validate": "^4.15.0", "vee-validate": "^4.15.0",

@ -49,6 +49,12 @@
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>
<version>5.8.16</version> <version>5.8.16</version>
</dependency> </dependency>
<!-- 邮箱服务-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
</dependencies> </dependencies>
<dependencyManagement> <dependencyManagement>
<dependencies> <dependencies>

@ -1,20 +1,24 @@
package com.unilife.controller; package com.unilife.controller;
import com.unilife.common.result.Result; import com.unilife.common.result.Result;
import com.unilife.model.dto.EmailDTO;
import com.unilife.model.dto.LogDTO; import com.unilife.model.dto.LogDTO;
import com.unilife.model.dto.LoginDTO; import com.unilife.model.dto.LoginDTO;
import com.unilife.service.UserService; import com.unilife.service.UserService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@Api(tags = "用户管理") @Api(tags = "用户管理")
@RestController @RestController
@RequestMapping("/users") @RequestMapping("/users")
@Slf4j
public class UserController { public class UserController {
@Autowired @Autowired
@ -29,4 +33,14 @@ public class UserController {
@ApiOperation(value = "用户登录") @ApiOperation(value = "用户登录")
@PostMapping("login") @PostMapping("login")
public Result login(@RequestBody LogDTO logDTO) { return userService.login(logDTO); } public Result login(@RequestBody LogDTO logDTO) { return userService.login(logDTO); }
@ApiOperation(value = "获取邮箱验证码")
@PostMapping("code")
public Result getCode(@RequestBody EmailDTO emailDto){
String email=emailDto.getEmail();
log.debug("收到的原始邮箱: {}", email);
return userService.sendVerificationCode(email);
}
} }

@ -0,0 +1,12 @@
package com.unilife.model.dto;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class EmailDTO {
String email;
}

@ -9,4 +9,6 @@ import com.unilife.model.entity.User;
public interface UserService { public interface UserService {
Result register(LoginDTO loginDTO); Result register(LoginDTO loginDTO);
Result login(LogDTO logDTO); Result login(LogDTO logDTO);
Result sendVerificationCode(String email);
} }

@ -1,6 +1,7 @@
package com.unilife.service.impl; package com.unilife.service.impl;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.RandomUtil;
import com.unilife.common.result.Result; import com.unilife.common.result.Result;
import com.unilife.mapper.UserMapper; import com.unilife.mapper.UserMapper;
import com.unilife.model.dto.LogDTO; import com.unilife.model.dto.LogDTO;
@ -9,11 +10,18 @@ import com.unilife.model.entity.User;
import com.unilife.model.vo.LogVO; import com.unilife.model.vo.LogVO;
import com.unilife.model.vo.LoginVO; import com.unilife.model.vo.LoginVO;
import com.unilife.service.UserService; import com.unilife.service.UserService;
import com.unilife.utils.RegexUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
@Slf4j @Slf4j
@Component @Component
@Service @Service
@ -22,8 +30,19 @@ public class UserServiceImpl implements UserService {
@Autowired @Autowired
private UserMapper userMapper; private UserMapper userMapper;
@Autowired
private JavaMailSender mailSender;
@Value("${spring.mail.username}")
private String from;
@Override @Override
public Result register(LoginDTO loginDTO) { public Result register(LoginDTO loginDTO) {
if(loginDTO.getEmail().equals("") || loginDTO.getPassword().equals("")) {
return Result.error(400,"邮箱或密码不能为空");
}
User user = new User(); User user = new User();
BeanUtil.copyProperties(loginDTO,user); BeanUtil.copyProperties(loginDTO,user);
userMapper.insert(user); userMapper.insert(user);
@ -48,4 +67,60 @@ public class UserServiceImpl implements UserService {
getuser.getAvatar(), getuser.getRole(), getuser.getIsVerified(), getuser.getStatus()); getuser.getAvatar(), getuser.getRole(), getuser.getIsVerified(), getuser.getStatus());
return Result.success(logVO); return Result.success(logVO);
} }
@Override
public Result sendVerificationCode(String email) {
//1.校验邮箱是否合法
boolean emailInvalid = RegexUtils.isEmailInvalid(email);
if(emailInvalid){
return Result.error(400,"邮箱格式不正确");
}
//2.生成随机验证码
String code = RandomUtil.randomNumbers(6);
log.debug("成功生成验证码,邮箱{},验证码{}", email, code);
//3.发送验证码到邮箱
try {
//构建邮件
MimeMessage message=mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setFrom(from);
helper.setTo(email);
helper.setSubject("UniLife - 登录验证码");
String content = "<div style=\"font-family: Arial, sans-serif; max-width: 600px; margin: 0 auto; padding: 20px; border: 1px solid #eee; border-radius: 5px;\">" +
"<h2 style=\"color: #333;\">您好!</h2>" +
"<p>感谢您使用UniLife平台。您的验证码是</p>" +
"<div style=\"background-color: #f5f5f5; padding: 10px; text-align: center; font-size: 24px; font-weight: bold; letter-spacing: 5px; margin: 20px 0;\">" +
code +
"</div>" +
"<p>此验证码将在10分钟内有效。</p>" +
"<p>如果您没有请求此验证码,请忽略此邮件。</p>" +
"<p style=\"margin-top: 30px; font-size: 12px; color: #888;\">" +
"这是一封自动生成的邮件,请勿直接回复。" +
"</p></div>";
helper.setText(content, true);
//4.发送邮件
mailSender.send(message);
}catch (MessagingException e){
log.error("邮件发送失败");
return Result.error(400,"邮件发送失败");
}
//4.存储随机产生的验证码
//TODO
return Result.success(200,"验证码已发送");
}
} }

@ -0,0 +1,21 @@
package com.unilife.utils;
public class RegexPatterns {
/**
*
*/
public static final String PHONE_REGEX = "^1([38][0-9]|4[579]|5[0-3,5-9]|6[6]|7[0135678]|9[89])\\d{8}$";
/**
*
*/
public static final String EMAIL_REGEX = "^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
/**
* 4~32线
*/
public static final String PASSWORD_REGEX = "^\\w{4,32}$";
/**
* , 6
*/
public static final String VERIFY_CODE_REGEX = "^[a-zA-Z\\d]{6}$";
}

@ -0,0 +1,39 @@
package com.unilife.utils;
import cn.hutool.core.util.StrUtil;
public class RegexUtils {
/**
*
* @param phone
* @return true:false
*/
public static boolean isPhoneInvalid(String phone){
return mismatch(phone, RegexPatterns.PHONE_REGEX);
}
/**
*
* @param email
* @return true:false
*/
public static boolean isEmailInvalid(String email){
return mismatch(email, RegexPatterns.EMAIL_REGEX);
}
/**
*
* @param code
* @return true:false
*/
public static boolean isCodeInvalid(String code){
return mismatch(code, RegexPatterns.VERIFY_CODE_REGEX);
}
// 校验是否不符合正则格式
private static boolean mismatch(String str, String regex){
if (StrUtil.isBlank(str)) {
return true;
}
return !str.matches(regex);
}
}

@ -1,11 +1,25 @@
server: server:
port: 8087 port: 8084
spring: spring:
datasource: datasource:
url: jdbc:mysql://localhost:3306/UniLife?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8 url: jdbc:mysql://localhost:3306/UniLife?useSSL=false&serverTimezone=UTC&characterEncoding=UTF-8
username: root username: root
password: zhong20050428 password: 123456
driver-class-name: com.mysql.cj.jdbc.Driver driver-class-name: com.mysql.cj.jdbc.Driver
mail:
host: smtp.163.com
port: 465
username: c2991692032@163.com
password: VPq5u3NcAAqtG9GT
properties:
mail:
smtp:
auth: true
ssl:
enable: true
socketFactory:
port: 465
class: javax.net.ssl.SSLSocketFactory
knife4j: knife4j:
enable: true enable: true
openapi: openapi:
@ -28,3 +42,6 @@ mybatis:
mapper-locations: classpath:mappers/*.xml mapper-locations: classpath:mappers/*.xml
configuration: configuration:
map-underscore-to-camel-case: true map-underscore-to-camel-case: true
logging:
level:
com.unilife: debug

Loading…
Cancel
Save