短信验证(JAVA)

package com.tq.jjb.common.util;

import java.io.IOException;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;

import org.apache.http.HttpResponse;
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;


public class SMSCodeUtil {
  private static final String APP_KEY="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";//网易云信分配的账号
  private static final String APP_SECRET="xxxxxxxxxxxx";//网易云信分配的密钥
  private static final String NONCE="000000";//随机数

  private static final String SENDMSG_URL="https://api.netease.im/sms/sendcode.action";//发送验证码的请求路径URL
  private static final String CHECKCODE_URL="https://api.netease.im/sms/verifycode.action";//校验验证码的请求路径URL

  private static final char[] HEX_DIGITS={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
  private static final String ALGORITHM="SHA";

  //计算并获取验证码
  public static String getVerificationCode(String appSecret,String nonce,String curTime){
    String param = appSecret+nonce+curTime;
    String verificationCode = "";
    try {
      MessageDigest messageDigest=MessageDigest.getInstance(ALGORITHM);
      messageDigest.update(param.getBytes());
      byte[] bytes = messageDigest.digest();

      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]);
      }
      verificationCode = sb.toString();
      return verificationCode;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  //发送短信获取生成的验证码
  public static String sendMessage(String phone) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost post = new HttpPost(SENDMSG_URL);

    String curTime=String.valueOf((new Date().getTime()/1000L));
    String checkSum=SMSCodeUtil.getVerificationCode(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<BasicNameValuePair> nameValuePairs =new ArrayList<BasicNameValuePair>();
    BasicNameValuePair basicNameValuePair = new BasicNameValuePair("mobile",phone);
    nameValuePairs.add(basicNameValuePair);

    post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));

    //执行请求
    HttpResponse response=httpclient.execute(post);
    String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8");

    //判断是否发送成功,发送成功返回true
    String code = JSONObject.fromObject(responseEntity).getString("code");
    if (code.equals("200")){
      return "success";
    }
    return "error";
  }

  //验证短信验证码是否匹配
  public static String checkVerificationCode(String phone,String verificationCode) throws IOException {
    CloseableHttpClient httpclient = HttpClients.createDefault();
    HttpPost post = new HttpPost(CHECKCODE_URL);

    String curTime=String.valueOf((new Date().getTime()/1000L));
    String checkSum=SMSCodeUtil.getVerificationCode(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<BasicNameValuePair> nameValuePairs =new ArrayList<BasicNameValuePair>();
    BasicNameValuePair mobilephone = new BasicNameValuePair("mobile",phone);
    nameValuePairs.add(mobilephone);
    BasicNameValuePair codeverificationCode = new BasicNameValuePair("code",verificationCode);
    nameValuePairs.add(codeverificationCode);

    post.setEntity(new UrlEncodedFormEntity(nameValuePairs,"utf-8"));
    //执行请求
    HttpResponse response=httpclient.execute(post);
    String responseEntity= EntityUtils.toString(response.getEntity(),"utf-8");

    //判断是否发送成功,发送成功返回true
    String code = JSONObject.fromObject(responseEntity).getString("code");
    if (code.equals("200")){
      return "success";
    }
    return "error";
  }

  public static void main(String[] args) throws IOException {

    // //测试getVerificationCode()方法
    // String appSecret="xxxxxxxxxxxx";//网易云信分配的密钥
    // String nonce="000000";//随机数
    // String curTime=String.valueOf((new Date().getTime()/1000L));
    // String aa = MobileMSGUtil.getVerificationCode(appSecret, nonce, curTime);
    // System.out.println(aa);

    // //测试sendMessage()方法
    // String phone = "00000000000";
    // String code = MobileMSGUtil.sendMessage(phone);
    // System.out.println("短信验证码获取的状态"+code);

    // //测试checkVerificationCode()方法
    // String phone = "00000000000";
    // String verificationCode = "0000";
    // String code = MobileMSGUtil.checkVerificationCode(phone,verificationCode);
    // System.out.println("短信验证码匹配的状态"+code);
  }
}

转载于:https://www.cnblogs.com/popovichzhouc/p/8135746.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值