阿里云短信业务实现
Posted 前度吖
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了阿里云短信业务实现相关的知识,希望对你有一定的参考价值。
了解阿里云用户权限操作
在阿里云产品中,提供短信服务、oss存储、视频播放、直播等等等功能...... 我们登录个人账号会给我们一个授权码
获取授权码步骤:
添加用户组、用户、权限
得到 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, Map<String,Object> 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~~
以上是关于阿里云短信业务实现的主要内容,如果未能解决你的问题,请参考以下文章