前言
公司最近项目需要一个手机验证码的功能,任务确定后,倍感亚历山大,以为和第三方对接的都好麻烦,查阿里的API、网上大神写的博客,各种查之后才发现,简单的一塌糊涂,这里想说个问题,不知道其他的攻城狮们是不是和我一样的心里,刚接触个没做过的任务时,会一脸懵里的着急,无从下手的感觉,后来会了,就觉得简单的一*,在这里我说一下自己的体会,遇到任何难点,先理思路、任务拆分、逐个查资料,其实一套下来,就不会那种一脸懵逼的干着急。。。
所需条件
1、阿里云账户
2、开通云通讯中的短信服务
3、申请短信签名和模板
4、创建access_key和access_secret
5、然后就是代码编写
话不啰嗦,直接开始开发步骤
开发步骤
开通短信服务
创建创建access_key和access_secret
申请短信模板和签名
开发步骤
1、创建AliyunConfig类
package com.preread.user.config;
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 java.util.Random;
public class AliyunConfig {
private static final String product = "Dysmsapi";
private static final String domain = "dysmsapi.aliyuncs.com";
private static final String accessKeyId = "你的accessKeyId"; //TODO: 这里要写成你自己生成的
private static final String accessKeySecret = "你的accessKeySecret";//TODO: 这里要写成你自己生成的
public static SendSmsResponse sendSms(String phone) throws ClientException {
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
IAcsClient acsClient = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setPhoneNumbers(phone);
request.setSignName("提前看"); //TODO: 这里是你短信签名
request.setTemplateCode("你的模板code"); //TODO: 这里是你的模板code
request.setTemplateParam("{"code":"" + getMsgCode() + ""}");
// hint 此处可能会抛出异常,注意catch
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals("OK")){
System.out.println("短信发送成功!验证码:" + getMsgCode());
}else {
System.out.println("短信发送失败!");
}
return sendSmsResponse;
}
private static String getMsgCode() {
int n = 6;
StringBuilder code = new StringBuilder();
Random ran = new Random();
for (int i = 0; i < n; i++) {
code.append(Integer.valueOf(ran.nextInt(10)).toString());
}
return code.toString();
}
}
2、controller层调用
@RequestMapping("/smsverification")
public Object SmsVerification(@Param("phone") String phone) {
return userViewService.SmsVerification(phone);
}
3、service层代码
@Override public MapSmsVerification(String phone) { Map map = new HashMap<>(); try { AliyunConfig.sendSms(phone); map.put("code", 200); map.put("msg", "短信验证发送成功"); return map; } catch (ClientException e) { map.put("code", 300); map.put("msg", e.getMessage()); return map; } }
4、集成阿里云SDK
com.aliyun aliyun-java-sdk-core4.1.0 com.aliyun aliyun-java-sdk-dysmsapi1.1.0 joda-time joda-timecommons-codec commons-codec1.7
至此代码阶段OK,可以测试了
效果如下:
到此这篇关于SpringBoot实现阿里云短信接口对接的示例代码的文章就介绍到这了,更多相关SpringBoot 阿里云短信接口对接内容请搜索考高分网以前的文章或继续浏览下面的相关文章希望大家以后多多支持考高分网!



