通过第三方接口发送短信验证码/短信通知(推荐)


需求:将首次交付密码为公共默认密码的方式改为点击入职功能,用短信方式发送系统自动生成的八位含数字、大小写字母和特殊符号生成的密码。短信发送服务由云通信http://www.yuntongxun.com/提供。

随机密码生成方法:

/**
* 生成随即密码
* @author chao.gao
* @param pwd_len 生成的密码的总长度
* @return 密码的字符串
*/
public static String genRandomNum(int pwd_len) {
// String re="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&]).{10,}";
String regex = "^(?![0-9]+$)(?![a-zA-Z]+$)[A-Za-z0-9@#$%]{8,16}$";
//35是因为数组是从0开始的,26个字母+10个数字
final int maxNum = 26;
int i; //生成的随机数
int count = 0; //生成的密码的长度
char[] str = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k',
'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
'x', 'y', 'z'};
char[] upChar = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
'X', 'Y', 'Z'};
char[] numChar = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
char[] speChar = {'!', '@', '#', '$', '%'};
StringBuffer pwd = new StringBuffer("");
Random r = new Random();
while (count < 2) {
//生成随机数,取绝对值,防止生成负数,
i = Math.abs(r.nextInt(maxNum)); //生成的数最大为36-1
if (i >= 0 && i < str.length) {
pwd.append(str[i]);
count++;
}
}
count=0;
while (count < 2) {
//生成随机数,取绝对值,防止生成负数,
i = Math.abs(r.nextInt(7)); //生成的数最大为7-1
if (i >= 0 && i < upChar.length) {
pwd.append(upChar[i]);
count++;
}
}
count=0;
while (count < 2) {
//生成随机数,取绝对值,防止生成负数,
i = Math.abs(r.nextInt(maxNum)); //生成的数最大为10-1
if (i >= 0 && i < numChar.length) {
pwd.append(numChar[i]);
count++;
}
}
count=0;
while (count < 2) {
//生成随机数,取绝对值,防止生成负数,
i = Math.abs(r.nextInt(maxNum)); //生成的数最大为10-1
if (i >= 0 && i < speChar.length) {
pwd.append(speChar[i]);
count++;
}
}
return pwd.toString();
}

发送短信接口:(jar包见附件)

参考:

https://www.yuntongxun.com/doc/rest/sms/3_2_2_3.html

public class SDKTestSendTemplateSMS {
public static void main(String[] args) {
HashMap<String, Object> result = null; 
CCPRestSDK restAPI = new CCPRestSDK();
restAPI.init("app.cloopen.com", "8883");
// 初始化服务器地址和端口,生产环境配置成app.cloopen.com,端口是8883. 
restAPI.setAccount("accountSid", "accountToken");
// 初始化主账号名称和主账号令牌,登陆云通讯网站后,可在"控制台-应用"中看到开发者主账号ACCOUNT SID和 
主账号令牌AUTH TOKEN。
restAPI.setAppId("AppId");
// 初始化应用ID,如果是在沙盒环境开发,请配置"控制台-应用-测试DEMO"中的APPID。
//如切换到生产环境,请使用自己创建应用的APPID
result = restAPI.sendTemplateSMS("号码1,号码2等","模板Id" ,new String[]{"模板内容1","模板内容2"});
System.out.println("SDKTestGetSubAccounts result=" + result); 
if("000000".equals(result.get("statusCode"))){
//正常返回输出data包体信息(map)
HashMap<String,Object> data = (HashMap<String, Object>) result.get("data");
Set<String> keySet = data.keySet();
for(String key:keySet){ 
Object object = data.get(key); 
System.out.println(key +" = "+object); 
}
}else{
//异常返回输出错误码和错误信息
System.out.println("错误码=" + result.get("statusCode") +" 错误信息= "+result.get("statusMsg"));
}
}
}

以上所述是小编给大家介绍的通过第三方接口发送短信验证码/短信通知(推荐),希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对phpstudy网站的支持!


« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3