阿里云短信业务实现
阿里 实现 业务 短信
2023-09-27 14:22:48 时间
阿里云短信业务实现
了解阿里云用户权限操作
在阿里云产品中,提供短信服务、oss存储、视频播放、直播等等等功能… 我们登录个人账号会给我们一个授权码
获取授权码步骤:
登录:账号中心 (aliyun.com)
添加用户组、用户、权限
得到 AccessKey
务必将AccessKey保存下来,不要泄露
开通阿里云短信服务
1、控制台搜索短信服务
2、在此处填写模板信息、内容等等
3、审核通过后得到模板code
4、添加签名—公司的名称,注意规则 等待审核通过
编写测试代码
阿里云 OpenAPI 开发者门户 (aliyun.com)
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.4</version>
</dependency>
官方测试类
// This file is auto-generated, don't edit it. Thanks.
package com.aliyun.sample;
import com.aliyun.tea.*;
import com.aliyun.dysmsapi20170525.*;
import com.aliyun.dysmsapi20170525.models.*;
import com.aliyun.teaopenapi.*;
import com.aliyun.teaopenapi.models.*;
public class Sample {
/**
* 使用AK&SK初始化账号Client
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config()
// 您的AccessKey ID
.setAccessKeyId(accessKeyId)
// 您的AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// 访问的域名
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
public static void main(String[] args_) throws Exception {
java.util.List<String> args = java.util.Arrays.asList(args_);
com.aliyun.dysmsapi20170525.Client client = Sample.createClient("accessKeyId", "accessKeySecret");
QuerySendDetailsRequest querySendDetailsRequest = new QuerySendDetailsRequest()
.setResourceOwnerAccount("test")
.setResourceOwnerId(1L)
.setPhoneNumber("test")
.setBizId("test")
.setSendDate("test");
// 复制代码运行请自行打印 API 的返回值
client.querySendDetails(querySendDetailsRequest);
}
}
编写可复用的微服务接口,实现发送
1、导入依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.4</version>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、接口
public interface SendSms {
public boolean send(String phoneNum, String code);
}
3、实现类
import com.alibaba.fastjson.JSON;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class SendSmsImpl implements SendSms {
public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
Config config = new Config();
config.accessKeyId = accessKeyId;
config.accessKeySecret = accessKeySecret;
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
/**
* 阿里云发送短信
*
* @param phoneNum
* @return boolean
*/
@Override
public boolean send(String phoneNum, String code) {
//l、初始化配置对象
com.aliyun.dysmsapi20170525.Client client = null;
try {
client = createClient("AccessKeyId", "AccessKeySecret");
} catch (Exception e) {
//连接异常
e.printStackTrace();
}
SendSmsRequest sendReq = new SendSmsRequest()
.setPhoneNumbers(phoneNum) //手机号
.setTemplateCode("模板")
.setSignName("签名")
.setTemplateParam("{\"code\":\""+code+"\"}");
try {
SendSmsResponse smsResp = client.sendSms(sendReq);
System.out.println(smsResp.getBody());
if (com.aliyun.teautil.Common.equalString(smsResp.body.code, "OK")) {
return true;
}
} catch (Exception e) {
//发送异常
e.printStackTrace();
}
return false;
}
// /**
// * 生成随机数
// *
// * @param num 位数
// * @return map
// */
// public static Map<String, Object> createRandomNum(int num) {
// HashMap<String, Object> map = new HashMap<>();
// String randomNumStr = "";
// for (int i = 0; i < num; i++) {
// int randomNum = (int) (Math.random() * 10);
// randomNumStr += randomNum;
// }
// map.put("code", randomNumStr);
// return map;
// }
}
4、测试
@RestController
public class SmsController {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Autowired
private SendSms sendSms;
@GetMapping("/send/{phone}")
public String code(@PathVariable("phone") String phone) {
//调用发送数据
String code = redisTemplate.opsForValue().get(phone);
if (!StringUtils.isEmpty(code)) {
return phone + ":" + code + "已存在!";
}
//生成随机验证码
code = UUID.randomUUID().toString().substring(0, 6);
boolean isSend = sendSms.send(phone, code);
if (isSend) {
//放入redis,5分钟后过期
redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
return phone + "验证码发送成功";
} else {
return "验证码发送失败";
}
}
}
The End~~
相关文章
- 阿里云的CDN产品到底用流量计费划算还是用带宽计费划算
- ossfs工具将OSS挂载到阿里云linux系统目录例子
- 被裁了!39 岁阿里 P9,攒下 1.5 亿....
- 阿里又出神器 !一招定位线上Bug!
- 阿里云成立13年首次实现年度盈利;博通610亿美元收购VMware;Android 13将采用华为研发的只读文件系统|极客头条
- 6年经验总结:入职阿里,软件测试常见基础题绝不能忘【附答案】
- 在阿里干了6年自动化测试,30岁即将退休的我,告诉你自动化测试工程师有多吃香...
- 阿里加速“新能源”战略布局 集团CTO张建锋兼任阿里云CTO
- 阿里云CTO章文嵩:阿里云强大的数据和计算能力助力企业实现“弯道超车”
- 2014阿里实习生面试题——哈希的原理和java中hashmap怎样实现的
- .Net Core in Docker - 使用阿里云Codepipeline及阿里云容器镜像服务实现持续交付/部署(CD)
- 阿里云双11的红包 不拼智商都不行
- 阿里云输出计算 天猫商家日处理400万订
- 2周实现上云上市,阿里云SaaS上云工具包如何打造新云梯
- 工业智能网关BL110应用之28:实现三菱 PLC FX3U 接入阿里云平台
- 工业智能网关BL110应用之12:实现三菱 PLC FX3S 接入阿里云平台
- 6倍性能差100TB容量,阿里云POLARDB咋实现?
- 阿里JAVA面试题剖析:一般实现分布式锁都有哪些方式?使用 Redis 如何设计分布式锁?使用 zk 来设计分布式锁可以吗?这两种分布式锁的实现方式哪种效率比较高?
- 阿里云API错误中心助力开发更高效