Java中对接阿里云短信能力
1、控制台中获取相关参数
1.1、申请签名,申请模板
1.2、openapi门户中在线联调
填写自己的参数调用即可
2、API对接阿里云
2.1、相关代码
导入依赖:
<!--阿里云短信sdk-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>dysmsapi20170525</artifactId>
<version>2.0.24</version>
</dependency>
#阿里云短信配置
aLiYunSms:
accessKey: xxx
secretKey: xxx
signName: xxx
templateCode: xxx
package com.wondertek.center.util.sms.shiWei;
import cn.hutool.core.lang.RegexPool;
import cn.hutool.core.util.ReUtil;
import cn.hutool.json.JSONUtil;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.dysmsapi20170525.models.SendSmsResponseBody;
import com.wondertek.web.exception.enums.ErrorCodeEnum;
import com.wondertek.web.exception.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class AliYunSmsUtil {
public static String accessKey;
public static String secretKey;
public static String signName;
public static String templateCode;
@Value("${aLiYunSms.accessKey}")
public void setAccessKey(String accessKey) {
AliYunSmsUtil.accessKey = accessKey;
}
@Value("${aLiYunSms.secretKey}")
public void setSecretKey(String secretKey) {
AliYunSmsUtil.secretKey = secretKey;
}
@Value("${aLiYunSms.signName}")
public void setSignName(String signName) {
AliYunSmsUtil.signName = signName;
}
@Value("${aLiYunSms.templateCode}")
public void setTemplateCode(String templateCode) {
AliYunSmsUtil.templateCode = templateCode;
}
/**
* 发送短信
*
* @return
*/
public static SendSmsResponseBody sendSms(String mobile, Long smsCode) {
return sendSmsLocal(accessKey, secretKey, signName, templateCode, mobile, smsCode);
}
/**
* 发送短信
*
* @return
*/
public static SendSmsResponseBody sendSmsLocal(String accessKey, String secretKey, String signName, String templateCode, String mobile, Long smsCode) {
//检查手机号
if (StringUtils.isNotBlank(mobile)) {
if (!ReUtil.isMatch(RegexPool.MOBILE, mobile)) {
throw new BizException(ErrorCodeEnum.ACCOUNT_FORMAT_ERROR);
}
}
try {
com.aliyun.dysmsapi20170525.Client client = AliYunSmsUtil.createClient(accessKey, secretKey);
com.aliyun.dysmsapi20170525.models.SendSmsRequest sendSmsRequest = new com.aliyun.dysmsapi20170525.models.SendSmsRequest().setPhoneNumbers(mobile).setSignName(signName).setTemplateCode(templateCode).setTemplateParam("{\"code\":\"" + smsCode.toString() + "\"}");
com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
// 复制代码运行请自行打印 API 的返回值
SendSmsResponse sendSmsResponse = client.sendSmsWithOptions(sendSmsRequest, runtime);
log.info("阿里云短信响应结果:{}", JSONUtil.toJsonStr(sendSmsResponse.getBody()));
String code = sendSmsResponse.getBody().getCode();
log.info("状态status:{}", code);
if (!"OK".equals(code)) {
throw new BizException(ErrorCodeEnum.SEND_SMS_CONTROL_ERROR.getCode(), sendSmsResponse.getBody().getMessage());
}
log.info("请求ID:{}", sendSmsResponse.getBody().getBizId());
return sendSmsResponse.getBody();
} catch (Exception error) {
// 如有需要,请打印 error
log.error("错误信息:{}", error.getMessage());
throw new BizException(ErrorCodeEnum.SEND_SMS_ERROR);
}
}
/**
* 使用AK&SK初始化账号Client
*
* @param accessKeyId
* @param accessKeySecret
* @return Client
* @throws Exception
*/
private static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
// 必填,您的 AccessKey ID
.setAccessKeyId(accessKeyId)
// 必填,您的 AccessKey Secret
.setAccessKeySecret(accessKeySecret);
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
config.endpoint = "dysmsapi.aliyuncs.com";
return new com.aliyun.dysmsapi20170525.Client(config);
}
public static void main(String[] args_) throws Exception {
sendSmsLocal("xxx", "xxx", "xxx", "xxx", "xxx", 123456L);
}
}