添加邮箱发送验证码的业务实现
This commit is contained in:
parent
4c15dfef9e
commit
57ca64b0f7
@ -1,12 +1,4 @@
|
||||
spring:
|
||||
cache:
|
||||
redis:
|
||||
# 指定存活时间(ms)
|
||||
time-to-live: 86400000
|
||||
# 指定前缀
|
||||
use-key-prefix: true
|
||||
# 是否缓存空值,可以防止缓存穿透
|
||||
cache-null-values: true
|
||||
data:
|
||||
# redis 配置
|
||||
redis:
|
||||
|
@ -16,3 +16,11 @@ oauth:
|
||||
dysms:
|
||||
appId: appId
|
||||
appSecret: appSecret
|
||||
|
||||
spring:
|
||||
mail:
|
||||
host: smtp.qq.com
|
||||
# 邮箱地址
|
||||
username: username
|
||||
# 授权码
|
||||
password: password
|
||||
|
@ -50,6 +50,13 @@ spring:
|
||||
cache:
|
||||
# 指定缓存类型 jcache 本地缓存 redis 缓存
|
||||
type: redis
|
||||
redis:
|
||||
# 指定存活时间(ms)
|
||||
time-to-live: 86400000
|
||||
# 指定前缀
|
||||
use-key-prefix: true
|
||||
# 是否缓存空值,可以防止缓存穿透
|
||||
cache-null-values: true
|
||||
mvc:
|
||||
pathmatch:
|
||||
matching-strategy: ANT_PATH_MATCHER
|
||||
|
@ -1,7 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>ruoyi-oauth</artifactId>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
@ -22,6 +21,21 @@
|
||||
<groupId>com.ruoyi</groupId>
|
||||
<artifactId>ruoyi-oauth-common</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>jakarta.mail</groupId>
|
||||
<artifactId>jakarta.mail-api</artifactId>
|
||||
<version>1.6.7</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>jakarta.mail</artifactId>
|
||||
<version>1.6.7</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -0,0 +1,19 @@
|
||||
package com.ruoyi.oauth.email.controller;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.ruoyi.common.annotation.Anonymous;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.oauth.email.service.IMailService;
|
||||
|
||||
@RestController
|
||||
@Anonymous
|
||||
@RequestMapping("/mail")
|
||||
public class MailController extends BaseController {
|
||||
|
||||
@Autowired
|
||||
IMailService serviceImpl;
|
||||
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.ruoyi.oauth.email.service;
|
||||
|
||||
import com.ruoyi.oauth.common.service.OauthVerificationCodeService;
|
||||
|
||||
public interface IMailService extends OauthVerificationCodeService {
|
||||
public boolean sendMimeMail(String email, String code);
|
||||
}
|
@ -0,0 +1,153 @@
|
||||
package com.ruoyi.oauth.email.service.impl;
|
||||
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.mail.SimpleMailMessage;
|
||||
import org.springframework.mail.javamail.JavaMailSenderImpl;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.ruoyi.common.core.domain.entity.SysUser;
|
||||
import com.ruoyi.common.core.domain.model.LoginUser;
|
||||
import com.ruoyi.common.exception.ServiceException;
|
||||
import com.ruoyi.common.utils.CacheUtils;
|
||||
import com.ruoyi.framework.web.service.TokenService;
|
||||
import com.ruoyi.framework.web.service.UserDetailsServiceImpl;
|
||||
import com.ruoyi.oauth.common.enums.OauthVerificationUse;
|
||||
import com.ruoyi.oauth.email.service.IMailService;
|
||||
import com.ruoyi.system.service.ISysUserService;
|
||||
|
||||
@Service
|
||||
public class MailServiceImpl implements IMailService {
|
||||
|
||||
public String CACHE_NAME = "mail_codes";
|
||||
|
||||
@Autowired
|
||||
private JavaMailSenderImpl mailSender;
|
||||
@Autowired
|
||||
private ISysUserService userService;
|
||||
@Autowired
|
||||
private TokenService tokenService;
|
||||
@Autowired
|
||||
private UserDetailsServiceImpl userDetailsServiceImpl;
|
||||
private static final Logger log = LoggerFactory.getLogger(MailServiceImpl.class);
|
||||
// application.properties配置的值
|
||||
@Value("${spring.mail.username}")
|
||||
private String from;
|
||||
|
||||
public boolean beforeSendCode(String email, OauthVerificationUse use) throws Exception {// 1.查验手机号是否存在,分辨登录和删除用户以及注册用户
|
||||
boolean haveEmailFlag = userService.selectUserByEmail(email) != null;
|
||||
if ((use.equals(OauthVerificationUse.LOGIN) || use.equals(OauthVerificationUse.DISABLE)
|
||||
|| use.equals(OauthVerificationUse.RESET_PASSWORD)) && !haveEmailFlag) {
|
||||
throw new ServiceException("该邮箱未绑定用户");
|
||||
} else if ((use.equals(OauthVerificationUse.REGISTER) || use.equals(OauthVerificationUse.RESET_PHONE))
|
||||
&& haveEmailFlag) {
|
||||
throw new ServiceException("该邮箱已绑定用户");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 给前端输入的邮箱,发送验证码
|
||||
*
|
||||
* @param email
|
||||
* @param session
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public boolean sendMimeMail(String email, String code) {
|
||||
try {
|
||||
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
|
||||
simpleMailMessage.setSubject("验证码邮件"); // 主题
|
||||
simpleMailMessage.setText("您收到的验证码是:" + code); // 内容
|
||||
simpleMailMessage.setFrom(from); // 发件人
|
||||
simpleMailMessage.setTo(email); // 收件人
|
||||
mailSender.send(simpleMailMessage); // 发送
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 随机生成6位数的验证码
|
||||
*
|
||||
* @return String code
|
||||
*/
|
||||
public static String generateRandomString(int n) {
|
||||
String characters = "0123456789"; // ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
|
||||
StringBuilder result = new StringBuilder();
|
||||
Random random = new Random();
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
int index = random.nextInt(characters.length());
|
||||
result.append(characters.charAt(index));
|
||||
}
|
||||
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendCode(String email, String code, OauthVerificationUse use) throws Exception {
|
||||
// 限制短信一分钟只能发送一次短信
|
||||
if (CacheUtils.hasKey(CACHE_NAME, email + use.getValue())) {
|
||||
throw new ServiceException("请在1分钟后再发送短信");
|
||||
}
|
||||
|
||||
try {
|
||||
sendMimeMail(email, code);
|
||||
CacheUtils.put(CACHE_NAME, email + use.getValue(), code, 1, TimeUnit.MINUTES);
|
||||
log.info("发送邮箱验证码成功:{ phone: " + email + " code:" + code + "}");
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
log.error("发送邮箱验证码异常:" + email);
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String checkCode(String email, String code, OauthVerificationUse use) throws Exception {
|
||||
String cachedCode = CacheUtils.get(use.getValue(), use.getValue() + email, String.class); // 从缓存中获取验证码
|
||||
CacheUtils.remove(use.getValue(), use.getValue() + email);
|
||||
boolean haveEmailFlag = userService.selectUserByEmail(email) != null;
|
||||
if (use.equals(OauthVerificationUse.LOGIN) && haveEmailFlag) {// 登录校验
|
||||
if (code.equals(cachedCode)) {
|
||||
SysUser sysUser = userService.selectUserByEmail(email);
|
||||
LoginUser loginUser = (LoginUser) userDetailsServiceImpl.createLoginUser(sysUser);
|
||||
return tokenService.createToken(loginUser);
|
||||
} else {
|
||||
throw new ServiceException("验证码错误");
|
||||
}
|
||||
} else if (use.equals(OauthVerificationUse.REGISTER) && !haveEmailFlag) {// 注册校验
|
||||
if (code.equals(cachedCode)) {
|
||||
return Boolean.toString(true);
|
||||
} else {
|
||||
throw new ServiceException("验证码错误");
|
||||
}
|
||||
} else if (use.equals(OauthVerificationUse.DISABLE) && haveEmailFlag) {// 注销校验
|
||||
if (code.equals(cachedCode)) {
|
||||
return Boolean.toString(true);
|
||||
} else {
|
||||
throw new ServiceException("验证码错误");
|
||||
}
|
||||
} else if (use.equals(OauthVerificationUse.RESET_PASSWORD) && haveEmailFlag) {// 重置密码校验
|
||||
if (code.equals(cachedCode)) {
|
||||
return Boolean.toString(true);
|
||||
} else {
|
||||
throw new ServiceException("验证码错误");
|
||||
}
|
||||
} else if (use.equals(OauthVerificationUse.RESET_PHONE) && !haveEmailFlag) {// 重置账号校验
|
||||
if (code.equals(cachedCode)) {
|
||||
return Boolean.toString(true);
|
||||
} else {
|
||||
throw new ServiceException("验证码错误");
|
||||
}
|
||||
}
|
||||
return Boolean.toString(false);
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package com.ruoyi.oauth.phone.service;
|
||||
|
||||
import com.ruoyi.oauth.common.enums.OauthVerificationUse;
|
||||
import com.ruoyi.oauth.common.service.OauthVerificationCodeService;
|
||||
|
||||
/**
|
||||
* 手机号认证Servcie
|
||||
@ -8,14 +8,7 @@ import com.ruoyi.oauth.common.enums.OauthVerificationUse;
|
||||
* @author zlh
|
||||
* @date 2024-04-16
|
||||
*/
|
||||
public interface DySmsService {
|
||||
public interface DySmsService extends OauthVerificationCodeService {
|
||||
public String doLogin(String phone);
|
||||
|
||||
public boolean sendCode(String phone, String code, OauthVerificationUse use) throws Exception;
|
||||
|
||||
public String checkCode(String phone, String code, OauthVerificationUse use) throws Exception;
|
||||
|
||||
// public String doenroll(String phone,String username, String password);
|
||||
|
||||
|
||||
}
|
||||
|
@ -16,7 +16,6 @@ import com.ruoyi.common.utils.CacheUtils;
|
||||
import com.ruoyi.framework.web.service.TokenService;
|
||||
import com.ruoyi.framework.web.service.UserDetailsServiceImpl;
|
||||
import com.ruoyi.oauth.common.enums.OauthVerificationUse;
|
||||
import com.ruoyi.oauth.common.service.OauthVerificationCodeService;
|
||||
import com.ruoyi.oauth.phone.enums.DySmsTemplate;
|
||||
import com.ruoyi.oauth.phone.service.DySmsService;
|
||||
import com.ruoyi.oauth.phone.utils.DySmsUtil;
|
||||
@ -29,7 +28,7 @@ import com.ruoyi.system.service.ISysUserService;
|
||||
* @date 2024-04-16
|
||||
*/
|
||||
@Service
|
||||
public class DySmsServiceImpl implements DySmsService, OauthVerificationCodeService {
|
||||
public class DySmsServiceImpl implements DySmsService {
|
||||
|
||||
@Autowired
|
||||
private DySmsUtil dySmsUtil;
|
||||
@ -79,6 +78,7 @@ public class DySmsServiceImpl implements DySmsService, OauthVerificationCodeServ
|
||||
@Override
|
||||
public String checkCode(String phone, String code, OauthVerificationUse use) throws Exception {
|
||||
String cachedCode = CacheUtils.get(use.getValue(), use.getValue() + phone, String.class); // 从缓存中获取验证码
|
||||
CacheUtils.remove(use.getValue(), use.getValue() + phone);
|
||||
boolean havePhoneFlag = userService.selectUserByPhone(phone) != null;
|
||||
if (use.equals(OauthVerificationUse.LOGIN) && havePhoneFlag) {// 登录校验
|
||||
if (code.equals(cachedCode)) {
|
||||
|
@ -10,6 +10,7 @@ import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
@ -22,6 +23,7 @@ import com.ruoyi.pay.domain.PayOrder;
|
||||
import com.ruoyi.pay.sqb.config.SqbConfig;
|
||||
|
||||
@Service
|
||||
@ConditionalOnProperty(prefix = "pay.sqb", name = "enabled", havingValue = "true")
|
||||
public class SQBServiceImpl {
|
||||
@Autowired
|
||||
private SqbConfig sqbConfig;
|
||||
|
@ -11,8 +11,7 @@ import com.ruoyi.common.core.domain.entity.SysUser;
|
||||
*
|
||||
* @author ruoyi
|
||||
*/
|
||||
public interface SysUserMapper
|
||||
{
|
||||
public interface SysUserMapper {
|
||||
/**
|
||||
* 根据条件分页查询用户列表
|
||||
*
|
||||
@ -56,11 +55,19 @@ public interface SysUserMapper
|
||||
/**
|
||||
* 通过手机号查询用户
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param phone 手机号
|
||||
* @return 用户对象信息
|
||||
*/
|
||||
public SysUser selectUserByPhone(String phone);
|
||||
|
||||
/**
|
||||
* 通过邮箱查询用户
|
||||
*
|
||||
* @param email 邮箱
|
||||
* @return 用户对象信息
|
||||
*/
|
||||
public SysUser selectUserByEmail(String email);
|
||||
|
||||
/**
|
||||
* 新增用户信息
|
||||
*
|
||||
|
@ -58,6 +58,14 @@ public interface ISysUserService {
|
||||
*/
|
||||
public SysUser selectUserByPhone(String phone);
|
||||
|
||||
/**
|
||||
* 通过邮箱查询用户
|
||||
*
|
||||
* @param userName 用户名
|
||||
* @return 用户对象信息
|
||||
*/
|
||||
public SysUser selectUserByEmail(String email);
|
||||
|
||||
/**
|
||||
* 根据用户ID查询用户所属角色组
|
||||
*
|
||||
|
@ -124,7 +124,7 @@ public class SysUserServiceImpl implements ISysUserService {
|
||||
/**
|
||||
* 通过手机号查询用户
|
||||
*
|
||||
* @param userId 用户ID
|
||||
* @param phone 用户ID
|
||||
* @return 用户对象信息
|
||||
*/
|
||||
@Override
|
||||
@ -132,6 +132,17 @@ public class SysUserServiceImpl implements ISysUserService {
|
||||
return userMapper.selectUserByPhone(phone);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过邮箱查询用户
|
||||
*
|
||||
* @param email 用户名
|
||||
* @return 用户对象信息
|
||||
*/
|
||||
@Override
|
||||
public SysUser selectUserByEmail(String email){
|
||||
return userMapper.selectUserByEmail(email);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询用户所属角色组
|
||||
*
|
||||
|
@ -72,10 +72,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
<if test="phonenumber != null and phonenumber != ''">
|
||||
AND u.phonenumber like concat('%', #{phonenumber}, '%')
|
||||
</if>
|
||||
<if test="params.beginTime != null and params.beginTime != ''"><!-- 开始时间检索 -->
|
||||
<if test="params.beginTime != null and params.beginTime != ''"> <!-- 开始时间检索 -->
|
||||
AND date_format(u.create_time,'%y%m%d') >= date_format(#{params.beginTime},'%y%m%d')
|
||||
</if>
|
||||
<if test="params.endTime != null and params.endTime != ''"><!-- 结束时间检索 -->
|
||||
<if test="params.endTime != null and params.endTime != ''"> <!-- 结束时间检索 -->
|
||||
AND date_format(u.create_time,'%y%m%d') <= date_format(#{params.endTime},'%y%m%d')
|
||||
</if>
|
||||
<if test="deptId != null and deptId != 0">
|
||||
@ -135,6 +135,11 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
where u.phonenumber = #{phone}
|
||||
</select>
|
||||
|
||||
<select id="selectUserByEmail" parameterType="String" resultMap="SysUserResult">
|
||||
<include refid="selectUserVo"/>
|
||||
where u.email = #{email}
|
||||
</select>
|
||||
|
||||
<select id="checkUserNameUnique" parameterType="String" resultMap="SysUserResult">
|
||||
select user_id, user_name from sys_user where user_name = #{userName} and del_flag = '0' limit 1
|
||||
</select>
|
||||
|
Loading…
Reference in New Issue
Block a user