完善短信封装
This commit is contained in:
@@ -0,0 +1,42 @@
|
||||
package org.jiayunet.constant;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.jiayunet.constant.sms.SmsVariableAllows;
|
||||
import org.jiayunet.constant.sms.UniversalSmsVariable;
|
||||
|
||||
/**
|
||||
* @author zk
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum SmsTemplateEnum {
|
||||
|
||||
/**
|
||||
* 通用验证码
|
||||
*/
|
||||
UNIVERSAL("加鱼宠物", "SMS_480245067", 5, UniversalSmsVariable.class);
|
||||
|
||||
/**
|
||||
* 模板签名
|
||||
*/
|
||||
private final String signName;
|
||||
|
||||
/**
|
||||
* 模板code
|
||||
*/
|
||||
private final String templateCode;
|
||||
|
||||
/**
|
||||
* 仅在验证码短信时配置, 发送短信是将默认使用短信模板+手机号未key, code 的值为value, 且禁止重复发送
|
||||
* NULL:不激活短信有效禁止重发限制
|
||||
* 有效时间 单位分钟
|
||||
*/
|
||||
private final Integer effectiveTime;
|
||||
|
||||
/**
|
||||
* 短信参数对象类
|
||||
*/
|
||||
private final Class<? extends SmsVariableAllows> variableClass;
|
||||
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
package org.jiayunet.constant;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import org.springframework.util.Assert;
|
||||
import org.jiayunet.sms.VerifyCodeAttribute;
|
||||
|
||||
/**
|
||||
* @author zk
|
||||
*/
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum SmsVerifyCodeEnum implements VerifyCodeAttribute {
|
||||
/**
|
||||
* 通用验证码
|
||||
*/
|
||||
UNIVERSAL("加鱼宠物", "SMS_480245067", "universal:code:sms:", 5, false);
|
||||
|
||||
|
||||
/**
|
||||
* 模板签名
|
||||
*/
|
||||
private final String signName;
|
||||
/**
|
||||
* 模板code
|
||||
*/
|
||||
private final String templateCode;
|
||||
/**
|
||||
* redis 名
|
||||
*/
|
||||
private final String redisKeyPre;
|
||||
/**
|
||||
* 有效时间 单位分钟
|
||||
*/
|
||||
private final Integer effectiveTime;
|
||||
/**
|
||||
* 覆盖已存在的redis值
|
||||
*/
|
||||
private final Boolean cover;
|
||||
|
||||
|
||||
public String getRedisKey(String phone) {
|
||||
Assert.hasText(phone, "手机号码不能为空");
|
||||
return this.redisKeyPre + phone;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package org.jiayunet.constant.sms;
|
||||
|
||||
/**
|
||||
* 短信模板参数抽象类
|
||||
*/
|
||||
public abstract class SmsVariableAllows {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package org.jiayunet.constant.sms;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* 通用验证码
|
||||
*/
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
public class UniversalSmsVariable extends SmsVariableAllows{
|
||||
|
||||
/**
|
||||
* 验证码
|
||||
*/
|
||||
private String code;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,231 @@
|
||||
package org.jiayunet.server;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jiayunet.constant.SmsTemplateEnum;
|
||||
import org.jiayunet.constant.sms.SmsVariableAllows;
|
||||
import org.jiayunet.constant.sms.UniversalSmsVariable;
|
||||
import org.jiayunet.sms.AliYunSmsAbility;
|
||||
import org.jiayunet.tool.server.RedisServerTool;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* 短信服务
|
||||
* @author zk
|
||||
*/
|
||||
@Service
|
||||
@Slf4j
|
||||
public class SmsServer {
|
||||
|
||||
private static final String SMS_KEY_PREFIX = "sms:";
|
||||
|
||||
@Autowired
|
||||
private AliYunSmsAbility aliYunSmsAbility;
|
||||
|
||||
@Autowired
|
||||
private RedisServerTool redisServerTool;
|
||||
|
||||
@Autowired
|
||||
private ObjectMapper objectMapper;
|
||||
|
||||
/**
|
||||
* 发送短信(普通短信或验证码短信)。
|
||||
* 根据 {@link SmsTemplateEnum} 中配置的有效期以及 {@code variable}
|
||||
* 是否包含非空的 {@code code} 字段来决定走验证码路径还是普通短信路径。
|
||||
*
|
||||
* @param phone 目标手机号,不能为空
|
||||
* @param template 短信模板枚举,必须与 {@code variable} 的类型匹配
|
||||
* @param variable 短信参数对象,可能是 {@link UniversalSmsVariable}(验证码)或其他实现
|
||||
* @return {@code true} 表示短信发送成功,{@code false} 表示发送失败或前置参数校验未通过
|
||||
*/
|
||||
public boolean send(String phone, SmsTemplateEnum template, SmsVariableAllows variable) {
|
||||
try {
|
||||
Assert.hasText(phone, "手机号不能为空");
|
||||
Assert.notNull(template, "短信模板不能为空");
|
||||
Assert.notNull(variable, "短信参数不能为空");
|
||||
|
||||
// 校验参数类型是否匹配
|
||||
if (!template.getVariableClass().isInstance(variable)) {
|
||||
log.error("短信参数类型不匹配: template={}, expectedClass={}, actualClass={}",
|
||||
template.name(), template.getVariableClass().getName(), variable.getClass().getName());
|
||||
return false;
|
||||
}
|
||||
|
||||
// 判断是否激活验证码模式
|
||||
boolean isVerifyMode = isVerifyCodeMode(template, variable);
|
||||
|
||||
if (isVerifyMode) {
|
||||
return sendVerifyCode(phone, template, (UniversalSmsVariable) variable);
|
||||
} else {
|
||||
return sendNormalSms(phone, template, variable);
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("发送短信异常: phone={}, template={}", phone, template.name(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验验证码是否正确。
|
||||
*
|
||||
* 根据手机号与短信模板生成 Redis 键,从缓存中读取
|
||||
* 先前发送的验证码并与用户提供的 {@code code} 进行比较。
|
||||
* 若缓存不存在或已过期则返回 {@code false},若匹配则返回 {@code true}。
|
||||
*
|
||||
* @param phone 目标手机号,不能为空
|
||||
* @param template 短信模板枚举,对应的验证码在 Redis 中的键会使用其模板码
|
||||
* @param code 待校验的验证码,不能为空
|
||||
* @return {@code true} 表示验证码匹配且未过期,{@code false} 表示不匹配或已失效
|
||||
*/
|
||||
public boolean verify(String phone, SmsTemplateEnum template, String code) {
|
||||
try {
|
||||
Assert.hasText(phone, "手机号不能为空");
|
||||
Assert.notNull(template, "短信模板不能为空");
|
||||
Assert.hasText(code, "验证码不能为空");
|
||||
|
||||
String key = buildVerifyCodeKey(template, phone);
|
||||
String storedCode = redisServerTool.get(key, String.class);
|
||||
|
||||
if (storedCode == null) {
|
||||
log.warn("验证码不存在或已过期: phone={}, template={}", phone, template.name());
|
||||
return false;
|
||||
}
|
||||
|
||||
return code.equals(storedCode);
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error("校验验证码异常: phone={}, template={}", phone, template.name(), e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除已发送的验证码缓存。
|
||||
*
|
||||
* 根据手机号和短信模板生成对应的 Redis 键并删除,以防止后续的验证码校验仍能通过。
|
||||
* 该方法在验证码已使用或需要手动失效时调用。
|
||||
*
|
||||
* @param phone 目标手机号,不能为空
|
||||
* @param template 短信模板枚举,用于确定 Redis 键的前缀
|
||||
*/
|
||||
public void clear(String phone, SmsTemplateEnum template) {
|
||||
try {
|
||||
String key = buildVerifyCodeKey(template, phone);
|
||||
redisServerTool.delete(key);
|
||||
log.info("清除验证码成功: phone={}, template={}", phone, template.name());
|
||||
} catch (Exception e) {
|
||||
log.error("清除验证码异常: phone={}, template={}", phone, template.name(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前请求是否属于验证码模式。
|
||||
*
|
||||
* 判定依据:
|
||||
* 1. {@link SmsTemplateEnum} 必须配置有效时间(effectiveTime),表示该模板用于验证码场景。
|
||||
* 2. 变量对象 {@code variable} 必须拥有 `getCode()` 方法,并且返回值在转换为字符串后非空。
|
||||
*
|
||||
* 通过反射检查 `getCode()` 方法,以避免对具体实现类(如 {@link UniversalSmsVariable})的硬性依赖,
|
||||
* 使得任何实现了同名方法的变量对象都可被识别为验证码变量。
|
||||
*
|
||||
* @param template 短信模板枚举,用于判断是否配置了验证码有效期
|
||||
* @param variable 短信参数对象,需具备可获取验证码的 `getCode()` 方法
|
||||
* @return {@code true} 表示应走验证码发送流程,{@code false} 表示走普通短信流程
|
||||
*/
|
||||
private boolean isVerifyCodeMode(SmsTemplateEnum template, SmsVariableAllows variable) {
|
||||
// 若模板没有配置有效时间,则不属于验证码模式
|
||||
if (template.getEffectiveTime() == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 通过反射检查变量对象是否具有 getCode() 方法,以及返回的值是否非空
|
||||
try {
|
||||
|
||||
java.lang.reflect.Method getCodeMethod = variable.getClass().getMethod("getCode");
|
||||
Object codeObj = getCodeMethod.invoke(variable);
|
||||
|
||||
// 只要 getCode 方法返回的值不为 null 且转换为字符串后非空,即视为验证码模式
|
||||
if (codeObj != null) {
|
||||
String codeStr = String.valueOf(codeObj);
|
||||
return !codeStr.isEmpty();
|
||||
}
|
||||
|
||||
} catch (NoSuchMethodException | IllegalAccessException | java.lang.reflect.InvocationTargetException e) {
|
||||
// 没有 getCode 方法或调用失败,说明不是验证码变量
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送验证码短信。
|
||||
*
|
||||
* 在发送前先检查 Redis 中是否已有未过期的验证码,防止重复发送。
|
||||
* 发送成功后将验证码存入 Redis,键使用模板码与手机号的组合,并设置模板配置的有效期。
|
||||
*
|
||||
* @param phone 目标手机号,不能为空
|
||||
* @param template 短信模板枚举,必须包含验证码相关配置(如有效时间)
|
||||
* @param variable {@link UniversalSmsVariable},其中必须包含非空的 {@code code}
|
||||
* @return {@code true} 表示短信发送及缓存成功,{@code false} 表示发送被拦截或失败
|
||||
*/
|
||||
private boolean sendVerifyCode(String phone, SmsTemplateEnum template, UniversalSmsVariable variable) {
|
||||
String key = buildVerifyCodeKey(template, phone);
|
||||
|
||||
// 检查是否重复发送
|
||||
if (redisServerTool.hasKey(key)) {
|
||||
log.warn("验证码尚未过期,请勿重复发送: phone={}, template={}", phone, template.name());
|
||||
return false;
|
||||
}
|
||||
|
||||
// 转换参数对象为 Map<String, String>
|
||||
Map<String, String> variableMap = objectMapper.convertValue(variable, new com.fasterxml.jackson.core.type.TypeReference<Map<String, String>>() {});
|
||||
|
||||
// 发送短信
|
||||
boolean success = aliYunSmsAbility.sendSms(
|
||||
phone,
|
||||
template.getSignName(),
|
||||
template.getTemplateCode(),
|
||||
variableMap
|
||||
);
|
||||
|
||||
// 发送成功后存储验证码
|
||||
if (success) {
|
||||
redisServerTool.set(key, variable.getCode(), template.getEffectiveTime(), TimeUnit.MINUTES);
|
||||
log.info("验证码已存储: phone={}, template={}, effectiveTime={}分钟",
|
||||
phone, template.name(), template.getEffectiveTime());
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送普通短信
|
||||
*/
|
||||
private boolean sendNormalSms(String phone, SmsTemplateEnum template, SmsVariableAllows variable) {
|
||||
// 转换参数对象为 Map<String, String>
|
||||
Map<String, String> variableMap = objectMapper.convertValue(variable, new com.fasterxml.jackson.core.type.TypeReference<Map<String, String>>() {});
|
||||
|
||||
// 直接发送短信
|
||||
return aliYunSmsAbility.sendSms(
|
||||
phone,
|
||||
template.getSignName(),
|
||||
template.getTemplateCode(),
|
||||
variableMap
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 构建验证码 Redis Key
|
||||
*/
|
||||
private String buildVerifyCodeKey(SmsTemplateEnum template, String phone) {
|
||||
return SMS_KEY_PREFIX + template.getTemplateCode() + ":" + phone;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user