大概流程:
用户输入账号点击获取验证码,验证是否绑定手机号码,绑定则发送验证码用户输入验证码,调用接口验证是否正确返回响应首先我们需要在网易云信注册账号,获取得到App Key和App Secret
需要注意的几个参数:
发送验证码参数
发送验证码状态:短信状态码
短信验证码参数
短信验证码状态:code状态表
项目中需要的jar:
httpclient-4.5.2.jar httpcore-4.4.4.jarJava工具类代码:校验码生成类
package com.sima.toolcode; import java.security.MessageDigest; /** * description : 校验码生成类 */ public class CheckSumBuilder { //计算并获取checkSum public static String getCheckSum(String appSecret,String nonce,String curTime){ return encode("SHA",appSecret+nonce+curTime); } private static String encode(String algorithm,String value){ if(value == null){ return null; } try { MessageDigest messageDigest=MessageDigest.getInstance(algorithm); messageDigest.update(value.getBytes()); return getFormattedText(messageDigest.digest()); } catch (Exception e) { throw new RuntimeException(e); } } private static String getFormattedText(byte[] bytes){ int len=bytes.length; StringBuilder sb=new StringBuilder(len*2); for(int $i=0;$i<len;$i++){ sb.append(HEX_DIGITS[(bytes[$i]>>4)&0x0f]); sb.append(HEX_DIGITS[bytes[$i]&0x0f]); } return sb.toString(); } private static final char[] HEX_DIGITS={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'}; }发送短信工具类
package com.sima.toolcode; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; /** * description : 发送短信工具类 */ public class SendMessage { private static final String SERVER_URL = "https://api.netease.im/sms/sendcode.action";//请求的URL private static final String APP_KEY = "badf8***1ea0c90b";//网易云分配的账号 private static final String APP_SECRET = "1c88e****14e4";//密码 private static final String MOULD_ID="39**766";//模板ID private static final String NONCE = "123456";//随机数 //验证码长度,范围4~10,默认为4 private static final String CODELEN = "6"; public static String sendMsg(String phone) throws ClientProtocolException, IOException{ CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost post = new HttpPost(SERVER_URL); String curTime = String.valueOf((new Date().getTime() / 1000L)); String checkSum = CheckSumBuilder.getCheckSum(APP_SECRET, NONCE, curTime); //设置请求的header post.addHeader("AppKey", APP_KEY); post.addHeader("Nonce", NONCE); post.addHeader("CurTime", curTime); post.addHeader("CheckSum", checkSum); post.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"); //设置请求参数 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("templateid", MOULD_ID)); nameValuePairs.add(new BasicNameValuePair("mobile", phone)); post.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8")); //执行请求 HttpResponse response = httpclient.execute(post); String responseEntity = EntityUtils.toString(response.getEntity(), "utf-8"); return responseEntity; } }检测短信验证码工具类
package com.sima.toolcode; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import com.alibaba.fastjson.JSON; /** * description : 检测短信验证码工具类 */ public class MobileMessageCheck { private static final String SERVER_URL="https://api.netease.im/sms/verifycode.action";//校验验证码的请求路径URL private static final String APP_KEY="badf87f8a7******bd1ea0c90b";//账号 private static final String APP_SECRET="1c8**14e4";//密钥 private static final String NONCE="123456";//随机数 public static String checkMsg(String phone,String sum) throws Exception{ CloseableHttpClient httpclient = HttpClients.createDefault(); HttpPost post = new HttpPost(SERVER_URL); String curTime=String.valueOf((new Date().getTime()/1000L)); String checkSum=CheckSumBuilder.getCheckSum(APP_SECRET,NONCE,curTime); //设置请求的header post.addHeader("AppKey",APP_KEY); post.addHeader("Nonce",NONCE); post.addHeader("CurTime",curTime); post.addHeader("CheckSum",checkSum); post.addHeader("Content-Type","application/x-www-form-urlencoded;charset=utf-8"); //设置请求参数 List<NameValuePair> nameValuePairs =new ArrayList<NameValuePair>(); nameValuePairs.add(new BasicNameValuePair("mobile",phone)); nameValuePairs.add(new BasicNameValuePair("code",sum)); post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8")); //执行请求 HttpResponse response=httpclient.execute(post); String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8"); //判断是否发送成功,发送成功返回true String code= JSON.parseObject(responseEntity).getString("code"); if (code.equals("200")){ return "success"; } return "error"; } }发送返回状态解析
//data为发送验证码返回标识,这里只判断常用的几种状态 var m = data.split(","); var code = m[0].split(":")[1]; obj = (m[2].split(":")[1]).replace("}", ""); if (code == "200") { alert("验证码已发到" + phone + "号码中,请查收"); } else if (code == "315") { alert("您绑定的手机号" + phone + "IP限制;");return; } else if (code == "301") { alert("您绑定的手机号" + phone + "被封禁!");return; } else if (code == "403") { alert("您绑定的手机号" + phone + "非法操作或没有权限!");return; } else if (code == "404") { alert("您绑定的手机号" + phone + "对象不存在!");return; } else if (code == "414") { alert("您绑定的手机号" + phone + "参数错误!");return; } else if (code == "500") { alert("您绑定的手机号" + phone + "服务器内部错误!");return; } else if (code == "408") { alert("您绑定的手机号" + phone + "客户端请求超时!");return; } else if (code == "419") { alert("您绑定的手机号" + phone + "数量超过上限!");return; }以上就是本文的全部内容,希望对大家的学习有所帮助,欢迎评论交流。能get到知识点不要忘了关注点个赞~ 拒绝白嫖从我们做起hh~