当前位置: 移动技术网 > IT编程>开发语言>Java > 阿里云短信验证~JAVA后台

阿里云短信验证~JAVA后台

2019年03月26日  | 移动技术网IT编程  | 我要评论

maven :中的 pom.xml添加

<dependency>
<groupid>com.aliyun</groupid>
<artifactid>aliyun-java-sdk-core</artifactid>
<version>4.0.6</version>
</dependency>
<dependency>
<groupid>com.aliyun</groupid>
<artifactid>aliyun-java-sdk-dysmsapi</artifactid>
<version>1.1.0</version>
</dependency>

/******  上面的是jar包  *******/

package com.atguigu.crud.utils;

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;

public class alymessage {
//产品名称:云通信短信api产品,开发者无需替换
static final string product="dysmsapi";
//产品域名,开发者无需替换
static final string domain = "dysmsapi.aliyuncs.com";

// todo 此处需要替换成开发者自己的ak(在阿里云访问控制台寻找),下面举个例子
static final string accesskeyid = "accesskeyid";
static final string accesskeysecret = "accesskeysecret";

//短信控制台中的签名和模板的id
static final string signname ="xxxx";
static final string templatecode ="sms_xxxxxxxxx";

/**
* 阿里云短信验证
* @param phonenumber 手机号码
* @param code 4~6位的验证码
* @return
* @throws clientexception
*/
public static sendsmsresponse sendsms(string phonenumber,string code) 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);
//组装请求对象-具体描述见控制台-文档部分内容
sendsmsrequest request=new sendsmsrequest();
//必填:待发送手机号
request.setphonenumbers(phonenumber);
//必填:短信签名-可在短信控制台中找到举个例子
request.setsignname(signname);
//必填:短信模板id-可在短信控制台中找到,是id不是名字,举个例子
request.settemplatecode(templatecode);
//可选:模板中的变量替换json串,如模板内容为"亲爱的${name},您的验证码为${code}"时,此处的值为--必填,与模板相对应
//request.settemplateparam("{\"name\":\"tom\", \"code\":\"123\"}");
request.settemplateparam("{\"code\":\""+code+"\"}");
sendsmsresponse sendsmsresponse=acsclient.getacsresponse(request);
return sendsmsresponse;
}
}

/*************/

public map<string,object> testshow(@requestbody map<string,object> reqbody) throws clientexception{
date d = new date();
simpledateformat sdf = new simpledateformat("yyyymmddhhmmsss");

//获取 100000~999999 中的随机数
int flag = new random().nextint(899999)+100000;

//获取时间格式没有其他符号只有数字
string datenowstr = sdf.format(d);

//转换为字符串
string code=flag +"";

//调用封装好的代码,进行发送短信
sendsmsresponse response = alymessage.sendsms(reqbody.get("phonenumber").tostring(),code);

//返回成功

if(response.getcode() != null && response.getcode().equals("ok")) {
reqbody.put("type", "ok");
return reqbody;
}
reqbody.put("type", "fail");
return reqbody;
}

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网