点击上方蓝色字体,选择“标星公众号”
优质文章,第一时间送达
关注公众号后台回复pay或mall获取实战项目资料视频
作者 :classabcd
来源 :blog.csdn.net/classabcd/article/details/82464582
1、构造手机验证码:使用random对象生成要求的随机数作为验证码,例如4位验证码:1000~9999之间随机数;
2、使用接口向短信平台发送手机号和验证码数据,然后短信平台再把验证码发送到制定手机号上,接口参数一般包括:目标手机号,随机验证码(或包含失效时间),平台接口地址,平台口令;
3、保存接口返回的信息(一般为json文本数据,然后需转换为json对象格式);
4、将手机号--验证码、操作时间存入Session中,作为后面验证使用;
5、接收用户填写的验证码及其他数据;
6、对比提交的验证码与Session中的验证码是否一致,同时判断提交动作是否在有效期内;
7、验证码正确且在有效期内,请求通过,处理相应的业务。
一,首先添加一个jar包,工具类会用到。
commons-codeccommons-codec1.11
二、我这里只是编写一个简单的短信验证功能,要是用其他的语音验证。。等等需要去秒滴云官方下载文档,下面是编写的一个config文档,专门存放一些参数
三、编写http请求工具类
public class HttpUtil
{/*** 构造通用参数timestamp、sig和respDataType** @return*/public static String createCommonParam(){// 时间戳SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");String timestamp = sdf.format(new Date());// 签名String sig = DigestUtils.md5Hex(Config.ACCOUNT_SID + Config.AUTH_TOKEN + timestamp);return "×tamp=" + timestamp + "&sig=" + sig + "&respDataType=" + Config.RESP_DATA_TYPE;}/*** post请求** @param url* 功能和操作* @param body* 要post的数据* @return* @throws IOException*/public static String post(String url, String body){System.out.println("url:" + System.lineSeparator() + url);System.out.println("body:" + System.lineSeparator() + body);String result = "";try{OutputStreamWriter out = null;BufferedReader in = null;URL realUrl = new URL(url);URLConnection conn = realUrl.openConnection();// 设置连接参数conn.setDoOutput(true);conn.setDoInput(true);conn.setConnectTimeout(5000);conn.setReadTimeout(20000);conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// 提交数据out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");out.write(body);out.flush();// 读取返回数据in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));String line = "";boolean firstLine = true; // 读第一行不加换行符while ((line = in.readLine()) != null){if (firstLine){firstLine = false;} else{result += System.lineSeparator();}result += line;}} catch (Exception e){e.printStackTrace();}return result;}/*** 回调测试工具方法** @param url* @param reqStr* @return*/public static String postHuiDiao(String url, String body){String result = "";try{OutputStreamWriter out = null;BufferedReader in = null;URL realUrl = new URL(url);URLConnection conn = realUrl.openConnection();// 设置连接参数conn.setDoOutput(true);conn.setDoInput(true);conn.setConnectTimeout(5000);conn.setReadTimeout(20000);// 提交数据out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");out.write(body);out.flush();// 读取返回数据in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));String line = "";boolean firstLine = true; // 读第一行不加换行符while ((line = in.readLine()) != null){if (firstLine){firstLine = false;} else{result += System.lineSeparator();}result += line;}} catch (Exception e){e.printStackTrace();}return result;}
}
四、生成四位数的方法
public static String runNumber() {String str&#61;"ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";StringBuilder sb&#61;new StringBuilder(4);for(int i&#61;0;i<4;i&#43;&#43;){char ch&#61;str.charAt(new Random().nextInt(str.length()));sb.append(ch);}System.out.println(sb.toString());String code &#61; sb.toString();return code;
}
五、执行方法execute()&#xff0c;便会发送成功
public class IndustrySMS
{private static String operation &#61; "/industrySMS/sendSMS";private static String accountSid &#61; Config.ACCOUNT_SID;private static String to &#61; "15342349382";private static String smsContent &#61; "【小陶科技】登录验证码&#xff1a;{"&#43;runNumber().toString()&#43;"}&#xff0c;如非本人操作&#xff0c;请忽略此短信。";/*** 验证码通知短信*/public static void execute(){String tmpSmsContent &#61; null;try{tmpSmsContent &#61; URLEncoder.encode(smsContent, "UTF-8");}catch(Exception e){}String url &#61; Config.BASE_URL &#43; operation;String body &#61; "accountSid&#61;" &#43; accountSid &#43; "&to&#61;" &#43; to &#43; "&smsContent&#61;" &#43; tmpSmsContent&#43; HttpUtil.createCommonParam();// 提交请求String result &#61; HttpUtil.post(url, body);System.out.println("result:" &#43; System.lineSeparator() &#43; result);
}
往期推荐
被警告了&#xff01;
配置 Nginx 的神器
Spring Boot中6种 注入 Bean 的方式
Spring Boot &#43; GraphQL 才是 API 的未来&#xff01;
一位前同事猝死了
面试限流、熔断、高可用&#xff0c;好多人一脸懵&#xff01;
干货分享&#xff1a;扫码关注下面的公众号获取面试资料&#43;项目实战资料&#xff08;电商/聚合支付&#xff09;