1.获取签名与模板
进入阿里云平台,进入短信服务模块,在以下位置添加签名和模板(格式一定按照要求填写 审批的比较严格)
2.编写模板与签名的枚举类
在上文获取模板与签名成功后,并作为常量放在枚举类中。
public enum DySmsEnum {
LOGIN_TEMPLATE_CODE("SMS_187570276", "自定义软件框架集成百度云", "code");
private String templateCode;
private String signName;
private String keys;
private DySmsEnum(String templateCode, String signName, String keys) {
this.templateCode = templateCode;
this.signName = signName;
this.keys = keys;
}
public String getTemplateCode() {
return templateCode;
}
public void setTemplateCode(String templateCode) {
this.templateCode = templateCode;
}
public String getSignName() {
return signName;
}
public void setSignName(String signName) {
this.signName = signName;
}
public String getKeys() {
return keys;
}
public void setKeys(String keys) {
this.keys = keys;
}
public static DySmsEnum toEnum(String templateCode) {
if (StringUtils.isEmpty(templateCode)) {
return null;
}
for (DySmsEnum item : DySmsEnum.values()) {
if (item.getTemplateCode().equals(templateCode)) {
return item;
}
}
return null;
}
}
3.配置类
这里面需要两个参数accessKeyId,accessKeySecret,需要在以下位置获取。
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.vanpeng.systemportal.modules.dysms.constant.DySmsEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class DySmsHelper {
private final static Logger logger = LoggerFactory.getLogger(DySmsHelper.class);
//产品名称:云通信短信API产品,无需替换
static final String product = "Dysmsapi";
//产品域名,无需替换
static final String domain = "dysmsapi.aliyuncs.com";
//此处需要替换成开发者自己的AK(在阿里云访问控制台寻找 本类需要修改此处)
static String accessKeyId;
static String accessKeySecret;
public static void setAccessKeyId(String accessKeyId) {
DySmsHelper.accessKeyId = accessKeyId;
}
public static void setAccessKeySecret(String accessKeySecret) {
DySmsHelper.accessKeySecret = accessKeySecret;
}
public static String getAccessKeyId() {
return accessKeyId;
}
public static String getAccessKeySecret() {
return accessKeySecret;
}
public boolean sendSms(String phone, JSONObject templateParamJson, DySmsEnum dySmsEnum) throws ClientException {
//可自助调整超时时间
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
//初始化acsClient,暂不支持region化
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
//验证json参数
validateParam(templateParamJson, dySmsEnum);
//组装请求对象-具体描述见控制台-文档部分内容
SendSmsRequest request = new SendSmsRequest();
//必填:待发送手机号
request.setPhoneNumbers(phone);
//必填:短信签名-可在短信控制台中找到
request.setSignName(dySmsEnum.getSignName());
//必填:短信模板-可在短信控制台中找到
request.setTemplateCode(dySmsEnum.getTemplateCode());
//可选:模板中的变量替换JSON串,如模板内容为"亲爱的${name},您的验证码为$[code]"时,此处的值为
request.setTemplateParam(templateParamJson.toJSONString());
//选填-上行短信扩展码(无特殊需求用户请忽略此字段)
//request.setSmsUpExtendCode("90997");
//可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
//request.setOutId("yourOutId");
boolean result = false;
//hint 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
logger.info("短信接口返回的数据----------------");
logger.info("{Code:" + sendSmsResponse.getCode() + ",Message:" + sendSmsResponse.getMessage() + ",RequestId:" + sendSmsResponse.getRequestId() + ",BizId:" + sendSmsResponse.getBizId() + "}");
if ("OK".equals(sendSmsResponse.getCode())) {
result = true;
}
return result;
}
private static void validateParam(JSONObject templateParamJson, DySmsEnum dySmsEnum) {
String keys = dySmsEnum.getKeys();
String[] keyArr = keys.split(",");
for (String item : keyArr) {
if (!templateParamJson.containsKey(item)) {
throw new RuntimeException("模板缺少参数:" + item);
}
}
}
}
4.测试类
使用postman或者其他方式调用即可发送短信。
@RestController
@RequestMapping("/SendSmsControllor")
public class SendSmsControllor {
@Autowired
DySmsHelper dySmsHelper;
@RequestMapping("/sendSms")
public void sendSms() throws ClientException {
JSONObject obj = new JSONObject();
obj.put("code", "1234");
dySmsHelper.sendSms("电话号", obj, DySmsEnum.LOGIN_TEMPLATE_CODE);
}
}
注意:一定要往账户中充钱或者办理套餐,否则短信发不出去的。
到此这篇关于springboot集成阿里云短信的文章就介绍到这了,更多相关springboot阿里云短信内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!