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

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

随机密码生成方法:

/***生成随即密码*@authorchao.gao*@parampwd_len生成的密码的总长度*@return密码的字符串*/publicstaticStringgenRandomNum(intpwd_len){//Stringre="(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&]).{10,}";Stringregex="^(?![0-9]+$)(?![a-zA-Z]+$)[A-Za-z0-9@#$%]{8,16}$";//35是因为数组是从0开始的,26个字母+10个数字finalintmaxNum=26;inti;//生成的随机数intcount=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={'!','@','#','$','%'};StringBufferpwd=newStringBuffer("");Randomr=newRandom();while(count<2){//生成随机数,取绝对值,防止生成负数,i=Math.abs(r.nextInt(maxNum));//生成的数最大为36-1if(i>=0&&i<str.length){pwd.append(str[i]);count++;}}count=0;while(count<2){//生成随机数,取绝对值,防止生成负数,i=Math.abs(r.nextInt(7));//生成的数最大为7-1if(i>=0&&i<upChar.length){pwd.append(upChar[i]);count++;}}count=0;while(count<2){//生成随机数,取绝对值,防止生成负数,i=Math.abs(r.nextInt(maxNum));//生成的数最大为10-1if(i>=0&&i<numChar.length){pwd.append(numChar[i]);count++;}}count=0;while(count<2){//生成随机数,取绝对值,防止生成负数,i=Math.abs(r.nextInt(maxNum));//生成的数最大为10-1if(i>=0&&i<speChar.length){pwd.append(speChar[i]);count++;}}returnpwd.toString();}

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

参考:

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

publicclassSDKTestSendTemplateSMS{publicstaticvoidmain(String[]args){HashMap<String,Object>result=null;CCPRestSDKrestAPI=newCCPRestSDK();restAPI.init("app.cloopen.com","8883");//初始化服务器地址和端口,生产环境配置成app.cloopen.com,端口是8883.restAPI.setAccount("accountSid","accountToken");//初始化主账号名称和主账号令牌,登陆云通讯网站后,可在"控制台-应用"中看到开发者主账号ACCOUNTSID和主账号令牌AUTHTOKEN。restAPI.setAppId("AppId");//初始化应用ID,如果是在沙盒环境开发,请配置"控制台-应用-测试DEMO"中的APPID。//如切换到生产环境,请使用自己创建应用的APPIDresult=restAPI.sendTemplateSMS("号码1,号码2等","模板Id",newString[]{"模板内容1","模板内容2"});System.out.println("SDKTestGetSubAccountsresult="+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(Stringkey:keySet){Objectobject=data.get(key);System.out.println(key+"="+object);}}else{//异常返回输出错误码和错误信息System.out.println("错误码="+result.get("statusCode")+"错误信息="+result.get("statusMsg"));}}}

附件:http://down.51cto.com/data/2368103