一篇文章教你实现发送阿里云短信验证码
2020-10-08 本文已影响0人
onnoA
1. 登录阿里云,并生成用户及用户组
登录阿里云跳转地址
image imageimage image创建用户组
image创建用户
image将创建好的用户添加进用户组
2. 为用户组分配短信服务权限
image3. 创建短信模板及签名
image imageimage image添加签名
==注意: 添加签名与短信模板创建均需要进行审核==
4. 短信验证码代码
Service
接口层
public interface IMessageService {
public boolean sendMessage(String phoneNumber, String templateCode, Map<String, Object> verifyCode);
}
Service
接口实现层
@Service
public class IMessageServiceImpl implements IMessageService {
@Override
public boolean sendMessage(String phoneNumber, String templateCode, Map<String, Object> verifyCode) {
DefaultProfile profile = DefaultProfile.getProfile("ch-hangzhou", "LTAI4GB2hzCnTpqTD8PGrJNn", "secret秘钥");
DefaultAcsClient 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", phoneNumber);
// 签名名称
request.putQueryParameter("SignName", "onnoA");
// 模板code
request.putQueryParameter("TemplateCode", templateCode);
// 验证码
request.putQueryParameter("TemplateParam", JSONObject.toJSONString(verifyCode));
try {
CommonResponse response = client.getCommonResponse(request);
return response.getHttpResponse().isSuccess();
} catch (ClientException e) {
// 短信发送失败
throw ServiceException.SMS_SEND_FAIL;
}
}
}