1.了解阿里云的权限操作
进入AccessKey管理
选择子用户
创建用户组和用户
先创建用户组,建好再进行权限分配
添加短信管理权限
创建用户
创建好后的id和密码在此处下载可以得到
2.开通阿里云短信服务
进行申请,配置短信模板
阿里云短信API文档
短信服务API概览及参考_短信服务(SMS)-阿里云帮助中心
3.测试编码
3.1创建springboot项目导入依赖
<!--阿里云sdk-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
dome测试代码
package com.yang.sms_dome;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.HashMap;
@SpringBootTest
class SmsDomeApplicationTests {
@Test
void contextLoads() {
// 连接阿里云
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou","自己的keyid", "自己的密钥");
IAcsClient client = new DefaultAcsClient(profile);
// 构建请求
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
// 这些内容不要动,是人家阿里巴巴弄出来的,咱不用管
request.setDomain("dysmsapi.aliyuncs.com");//不要动
request.setVersion("2017-05-25");//不要动
// 自己的内容,此处 SendSms 为发送验证码
request.setAction("SendSms");
//自定义的参数(手机号,验证码,签名,模板! )
//这是我的内容,请结合你的情况修改为你的东西!!!
request.putQueryParameter("PhoneNumbers", "手机号");
request.putQueryParameter("SignName", "阿里云短信测试");//阿里云专用短信测试签名
request.putQueryParameter("TemplateCode", "SMS_154950909");//阿里云专用短信测试模板
//构建短信验证码
HashMap<String,Object> map=new HashMap<>();
map.put("code",1877);
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
try {
CommonResponse response = client.getCommonResponse(request);
// 在控制台上打印出返回信息
System.out.println(response.getData());
} catch (ClientException e) {
e.printStackTrace();
}
}
}
controller类
package com.yang.sms_dome.controller;
import com.aliyuncs.utils.StringUtils;
import com.yang.sms_dome.service.SendSmsService;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@RestController
@CrossOrigin //跨域
public class SendSmsController {
@Autowired
private SendSmsService sendSmsService;
@Autowired
private RedisTemplate<String,String> redisTemplate;
@GetMapping("/send/{phone}")
public String code(@PathVariable("phone") String phone){
String code = redisTemplate.opsForValue().get(phone);
if (StringUtils.isNotEmpty(code)){
return phone+code+"已存在,未过期";
}
//生成验证码并且存储到redis中
code = generateVerificationCode(4);
HashMap<String, Object> map = new HashMap<>();
map.put("code",code);
boolean flag = sendSmsService.sendSms(phone, "SMS_154950909", map);
if (flag){
redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
return phone+":"+code+"发送成功!";
}
return "发送失败";
}
public static String generateVerificationCode(int length) {
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
int digit = random.nextInt(10); // 生成 0 到 9 之间的随机数字
sb.append(digit);
}
return sb.toString();
}
}
service层
package com.yang.sms_dome.service;
import java.util.Map;
public interface SendSmsService {
boolean sendSms(String phoneNum, String templateCode, Map<String,Object> code);
}
serviceImpl层
package com.yang.sms_dome.serviceImpl;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.yang.sms_dome.service.SendSmsService;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class SendSmsServiceImpl implements SendSmsService {
@Override
public boolean sendSms(String phoneNum, String templateCode, Map<String, Object> code) {
// 连接阿里云
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou","自己的keyid", "自己的密钥");
IAcsClient client = new DefaultAcsClient(profile);
// 构建请求
CommonRequest request = new CommonRequest();
request.setMethod(MethodType.POST);
// 这些内容不要动,是人家阿里巴巴弄出来的,咱不用管
request.setDomain("dysmsapi.aliyuncs.com");//不要动
request.setVersion("2017-05-25");//不要动
// 自己的内容,此处 SendSms 为发送验证码
request.setAction("SendSms");
//自定义的参数(手机号,验证码,签名,模板! )
//这是我的内容,请结合你的情况修改为你的东西!!!
request.putQueryParameter("PhoneNumbers", phoneNum);
request.putQueryParameter("SignName", "阿里云短信测试");//阿里云专用短信测试签名
request.putQueryParameter("TemplateCode", templateCode);//阿里云专用短信测试模板
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(code));
try {
CommonResponse response = client.getCommonResponse(request);
// 在控制台上打印出返回信息
System.out.println(response.getData());
return response.getHttpResponse().isSuccess();
} catch (ClientException e) {
e.printStackTrace();
}
return false;
}
}
yaml配置文件
server:
port: 8080
spring:
redis:
host: 127.0.0.1
port: 6379
参考视频:【狂神说】通俗易懂的阿里云短信业务实战教程_哔哩哔哩_bilibili
代码仓库(私有):https://gitee.com/yzh-ch/sms_test