阿里云短信服务
1,阿里云用户权限操作
1.1、找到后台放在个人头像上面 选择 AccessKey 管理
1.2、选择 子用户
1.3、创建用户组
1.4、给用户组添加权限
然后就可以看到你的权限里面多了一个sms的短信权限
1.5、创建用户
注意!注意!注意点击确认后只可以看到一次密码返回就看不到了
注意!注意!注意点击确认后只可以看到一次密码返回就看不到了
注意!注意!注意点击确认后只可以看到一次密码返回就看不到了
1.6、把用户加入到用户组
2、开通阿里云短信服务
2.1、搜索短信服务
2.1、添加模板
文章来源:https://www.toymoban.com/news/detail-401175.html
2.3、添加签名
文章来源地址https://www.toymoban.com/news/detail-401175.html
3、编写代码
3.1、创建一个springboot项目
3.2、导入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.10</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.3、model
package org.kang.aliyundxfw.model;
import lombok.Data;
import java.util.Map;
/**
* @author 康康
* @create 2021-10-30 14:42
* @explain
*/
@Data
public class SendSms {
/**
* 手机号
*/
private String PhoneNumbers;
/**
*模板
*/
private final String templateCode="SMS_227260007";
/**
* 签名
*/
private final String SignName="康康科技";
/**
* 验证码
*/
private Map<String,Object> code;
}
3.4、Service
package org.kang.aliyundxfw.service;
import org.kang.aliyundxfw.model.SendSms;
/**
* @author 康康
* @create 2021-10-30 14:39
* @explain
*/
public interface SendSmsService {
boolean send(SendSms sendSms);
}
3.4.1、impl
package org.kang.aliyundxfw.service.impl;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.kang.aliyundxfw.model.SendSms;
import org.kang.aliyundxfw.service.SendSmsService;
import org.springframework.stereotype.Service;
/**
* @author 康康
* @create 2021-10-30 15:07
* @explain
*/
@Service
public class SendSmsServiceImpl implements SendSmsService {
@Override
public boolean send(SendSms sendSms) {
//连接阿里云 第二个参数是 AccessKey的id 第三个参数是 AccessKey的密码
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI5tQHFJoDijGxRYgoDZYM", "LRn4x2XHvwuOCNn6lYWrZxYPROmXsh");
//构建请求
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();
request.setSysMethod(MethodType.POST);
request.setSysDomain("dysmsapi.aliyuncs.com");
request.setSysVersion("2017-05-25");
request.setSysAction("SendSms");
//自定义参数 (手机号,模板,签名)
request.putQueryParameter("PhoneNumbers",sendSms.getPhoneNumbers());
request.putQueryParameter("TemplateCode",sendSms.getTemplateCode());
request.putQueryParameter("TemplateParam", JSON.toJSONString(sendSms.getCode()));
try {
CommonResponse response = client.getCommonResponse(request);
System.out.println(response.getData());
//返回成功还是失败
return response.getHttpResponse().isSuccess();
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
e.printStackTrace();
}
return false;
}
}
3.5、contrlloer
package org.kang.aliyundxfw.contrlloer;
import io.netty.util.internal.StringUtil;
import org.kang.aliyundxfw.model.SendSms;
import org.kang.aliyundxfw.service.SendSmsService;
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.RestController;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* @author 康康
* @create 2021-10-30 15:16
* @explain
*/
@RestController
@CrossOrigin //跨域支持
public class SendSmsContrlloer {
@Autowired
private SendSmsService sendSmsService;
@Autowired
private RedisTemplate<String, String> redisTemplate;
@GetMapping("/send")
public String code(SendSms sendSms) {
//根据手机号到redis里面查看有没有这个手机号
String code = redisTemplate.opsForValue().get(sendSms.getPhoneNumbers());
if (!StringUtil.isNullOrEmpty(code)) {
return sendSms.getPhoneNumbers() + ":" + sendSms.getCode() + "已存在,还没有过期";
}
//随机生成四位数的验证码
code = UUID.randomUUID().toString().substring(0, 4);
Map<String,Object> map=new HashMap<>();
map.put("code",code);
sendSms.setCode(map);
//调用Service的方法
boolean isSend = sendSmsService.send(sendSms);
if(isSend){
//成功就把手机号放到redis里面 设置5分钟过期
redisTemplate.opsForValue().set(sendSms.getPhoneNumbers(),code,5, TimeUnit.SECONDS);
return sendSms.getPhoneNumbers() + ":" + sendSms.getCode() + "发送成功";
}else {
return "发送失败";
}
}
}
到了这里,关于开通阿里云短信服务的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!