mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
添加使用阿里云平台发送验证码短信的方法
--pom.xml 添加短信发送sdk --Constant.java 添加发送短信的参数枚举 --ALiYunSmsService.java 阿里云实现短信方法 --SmsService.java 短信方法定义
This commit is contained in:
@@ -28,6 +28,8 @@
|
||||
<captcha.version>1.6.2</captcha.version>
|
||||
<guava.version>33.0.0-jre</guava.version>
|
||||
<liquibase-core.version>4.20.0</liquibase-core.version>
|
||||
<the-latest-version>4.1.0</the-latest-version>
|
||||
<okio-version>3.4.0</okio-version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -37,6 +39,7 @@
|
||||
<classifier>jakarta</classifier>
|
||||
<version>${shiro.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.shiro</groupId>
|
||||
<artifactId>shiro-spring</artifactId>
|
||||
@@ -201,6 +204,18 @@
|
||||
<groupId>com.fasterxml.jackson.datatype</groupId>
|
||||
<artifactId>jackson-datatype-jsr310</artifactId>
|
||||
</dependency>
|
||||
<!-- 阿里云短信sdk -->
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>dysmsapi20170525</artifactId>
|
||||
<version>${the-latest-version}</version>
|
||||
</dependency>
|
||||
<!-- 阿里云短信sdk使用的okio:1.15.0版本有安全漏洞:GHSA-w33c-445m-f8w7 现在升级成3.4.0安全版本-->
|
||||
<dependency>
|
||||
<groupId>com.squareup.okio</groupId>
|
||||
<artifactId>okio</artifactId>
|
||||
<version>${okio-version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<!-- 阿里云maven仓库 -->
|
||||
|
||||
@@ -146,6 +146,46 @@ public interface Constant {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 系统短信
|
||||
*/
|
||||
enum SysMSMParam {
|
||||
/**
|
||||
* 阿里云授权keyID
|
||||
*/
|
||||
ALIYUN_SMS_ACCESS_KEY_ID("aliyun.sms.access_key_id"),
|
||||
/**
|
||||
* 阿里云授权密钥
|
||||
*/
|
||||
ALIYUN_SMS_ACCESS_KEY_SECRET("aliyun.sms.access_key_secret"),
|
||||
/**
|
||||
*
|
||||
*/
|
||||
ALIYUN_SMS_SIGN_NAME("aliyun.sms.sign_name"),
|
||||
/**
|
||||
* 已删除
|
||||
*/
|
||||
ALIYUN_SMS_SMS_CODE_TEMPLATE_CODE("aliyun.sms.sms_code_template_code"),
|
||||
/**
|
||||
* 已删除
|
||||
*/
|
||||
SYSTEM_SMS_MAX_SEND_COUNT("system.sms.max_send_count"),
|
||||
/**
|
||||
* 是否开启手机注册
|
||||
*/
|
||||
SYSTEM_ENABLE_MOBILE_REGISTER("system.enable_mobile_register");
|
||||
|
||||
private String value;
|
||||
|
||||
SysMSMParam(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据状态
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
package xiaozhi.modules.sms.service;
|
||||
|
||||
/**
|
||||
* 短信服务的方法定义接口
|
||||
*
|
||||
* @author zjy
|
||||
* @since 2025-05-12
|
||||
*/
|
||||
public interface SmsService {
|
||||
|
||||
/**
|
||||
* 发送验证码短信
|
||||
* @param phone 手机号码
|
||||
* @param VerificationCode 验证码
|
||||
*/
|
||||
void sendVerificationCodeSms(String phone, String VerificationCode) ;
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package xiaozhi.modules.sms.service.imp;
|
||||
|
||||
import com.aliyun.dysmsapi20170525.Client;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
||||
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
||||
import com.aliyun.tea.TeaException;
|
||||
import com.aliyun.teaopenapi.models.Config;
|
||||
import com.aliyun.teautil.Common;
|
||||
import com.aliyun.teautil.models.RuntimeOptions;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Service;
|
||||
import xiaozhi.common.constant.Constant;
|
||||
import xiaozhi.modules.sms.service.SmsService;
|
||||
import xiaozhi.modules.sys.service.SysParamsService;
|
||||
|
||||
@Service
|
||||
@AllArgsConstructor
|
||||
@Slf4j
|
||||
public class ALiYunSmsService implements SmsService {
|
||||
private final SysParamsService sysParamsService;
|
||||
|
||||
@Override
|
||||
public void sendVerificationCodeSms(String phone, String VerificationCode) {
|
||||
String SignName = sysParamsService.getValue(Constant.SysMSMParam
|
||||
.ALIYUN_SMS_SIGN_NAME.getValue(),true);
|
||||
String TemplateCode = sysParamsService.getValue(Constant.SysMSMParam
|
||||
.ALIYUN_SMS_SMS_CODE_TEMPLATE_CODE.getValue(),true);
|
||||
try {
|
||||
Client client = createClient();
|
||||
SendSmsRequest sendSmsRequest = new SendSmsRequest()
|
||||
.setSignName(SignName)
|
||||
.setTemplateCode(TemplateCode)
|
||||
.setPhoneNumbers(phone)
|
||||
.setTemplateParam(String.format("{\"code\":\"%s\"}", VerificationCode));
|
||||
RuntimeOptions runtime = new RuntimeOptions();
|
||||
try {
|
||||
// 复制代码运行请自行打印 API 的返回值
|
||||
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
|
||||
System.out.println(sendSmsResponse.getBody().getRequestId());
|
||||
System.out.println(sendSmsResponse.getBody().getRequestId());
|
||||
} catch (TeaException error) {
|
||||
// 错误 message
|
||||
log.error(error.getMessage());
|
||||
// 诊断地址
|
||||
log.error(error.getData().get("Recommend").toString());
|
||||
log.error(Common.assertAsString(error.message));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
// 错误 message
|
||||
log.error(e.getMessage());
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建阿里云连接
|
||||
* @return 返回连接对象
|
||||
* @throws Exception
|
||||
*/
|
||||
private Client createClient() throws Exception {
|
||||
String ACCESS_KEY_ID = sysParamsService.getValue(Constant.SysMSMParam
|
||||
.ALIYUN_SMS_ACCESS_KEY_ID.getValue(),true);
|
||||
String ACCESS_KEY_SECRET = sysParamsService.getValue(Constant.SysMSMParam
|
||||
.ALIYUN_SMS_ACCESS_KEY_SECRET.getValue(),true);
|
||||
Config config = new Config()
|
||||
.setAccessKeyId(ACCESS_KEY_ID)
|
||||
.setAccessKeySecret(ACCESS_KEY_SECRET);
|
||||
// 配置 Endpoint。中国站请使用dysmsapi.aliyuncs.com
|
||||
config.endpoint = "dysmsapi.aliyuncs.com";
|
||||
|
||||
return new Client(config);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user