java 阿里云接口实现发送短信验证码

此刻自己做的小项目中,需要用到手机发送短信验证码实现注册功能,于是就去阿里云注册了账号,并实现随机发送验证码的功能

第一步:在阿里云官网登录注册   已有支付宝或淘宝的账号可以直接登录,最后需要实名认证和绑定邮箱

第二步:在这个网址上  https://www.aliyun.com/product/sms?spm=5176.doc55324.765261.224.SSoWpM  申请短信签名,后台需要审核,差不多几分钟就好

第三步:在这里申请短信模板,同样需要后台审核。

第四补:都通过后,在自己主页创建acceesskeys,创建一个access Key ID 和一个 access  Key secret,

最后在步骤网址上下载两个jar包,

aliyun-java-sdk-core-3.2.3.jar  和   aliyun-java-sdk-dysmsapi-1.0.0.jar

里面有demojava代码,改一些参数后,运行如果有错参考这个地址

https://help.aliyun.com/document_detail/55324.html?spm=5176.sms-sign.form.2.856da5aHTCYvi

具体步骤这个网址都有   https://help.aliyun.com/document_detail/55284.html?spm=5176.product44282.6.553.7xEXM0

 1 package com.demo;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 import com.aliyuncs.DefaultAcsClient;
 7 import com.aliyuncs.IAcsClient;
 8 import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsRequest;
 9 import com.aliyuncs.dysmsapi.model.v20170525.QuerySendDetailsResponse;
10 import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
11 import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
12 import com.aliyuncs.exceptions.ClientException;
13 import com.aliyuncs.http.MethodType;
14 import com.aliyuncs.profile.DefaultProfile;
15 import com.aliyuncs.profile.IClientProfile;
16 
17 public class SmsUtils {
18 
19     /**
20      * @param args
21      */
22     public static void main(String[] args) {
23         try {
24             SmsUtils smsUtils = new SmsUtils();
25             String telphoneString = "13996932841";
26             smsUtils.sendSms(telphoneString);
27         } catch (ClientException e) {
28             e.printStackTrace();
29         }
30 
31     }
32     
33     //随机验证码
34     private int code;
35     public int getCode(){
36         return code;
37     }
38     public void setCode(){
39         code = (int)(Math.random()*9999)+1000;  //每次调用生成一次四位数的随机数
40     }
41     
42     //短信API产品名称
43     static final String product="Dysmsapi";
44     //短信API产品域名
45     static final String domain="dysmsapi.aliyuncs.com";
46     
47     static final String accessKeyId = "LTAIof9JVAdAOQDB";
48     static final String accessKeySecret = "Niq2vDxvbKziwJh3unko3yWWss1LzY";
49 
50     public  SendSmsResponse sendSms(String telphone) throws ClientException{
51         //设置超时时间
52         System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
53         System.setProperty("sun.net.client.defaultReadTimeout", "10000");
54         //初始化ascClient
55         IClientProfile profile=DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
56         DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou",product, domain);
57         IAcsClient acsClient=new DefaultAcsClient(profile);
58         
59         //组装请求对象
60         SendSmsRequest request=new SendSmsRequest();
61         //使用post提交
62         request.setMethod(MethodType.POST);
63         //待发送的手机号
64         request.setPhoneNumbers(telphone);
65         //短信签名
66         request.setSignName("星星");
67         //短信模板ID
68         request.setTemplateCode("SMS_99325057");
69         //验证码
70         SmsUtils sms = new SmsUtils();
71         sms.setCode();
72         String codetemp = sms.getCode()+"";
73         System.out.println("code:        "+codetemp);
74         /*
75          * 可选:模板中的变量替换JSON串,
76          * 如模板内容为"亲爱的${name},您的验证码为${code}"时,
77          * 此处的值为{"name":"Tom","code":"1454"}
78          *     反斜杠为转义字符,使得输出双引号
79          */
80         request.setTemplateParam("{"name":"Tom", "code":""+codetemp+""}");
81         //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
82         //request.setOutId("1454");
83         SendSmsResponse response=acsClient.getAcsResponse(request);
84         
85         if(response.getCode() != null && response.getCode().equals("OK")) {
86             //请求成功
87             System.out.println("发送成功!");
88         }else {
89             System.out.println("发送失败!");
90         }
91         return response;
92     }
93 }

我在中间盖了一下验证码,让方法每次调用就重新随机生成验证码。

结果如下:

原文地址:https://www.cnblogs.com/nn369/p/7590955.html