JavaWeb之短信发送验证码

2018-08-14  本文已影响0人  伍陆柒_

需要jar包
commons-codec-1.9.jar

目前手机注册登录是时下非常流行的一种交互方式,出售短信包的平台以及对应的API有很多,这里只以其中一个为例,秒嘀

image.png

Config.java

package test;

public class Config{
    public static final String BASE_URL = "https://api.miaodiyun.com/20150822";
    // 此参数在开发者注册后系统自动生成,去对应平台登录进行查看
    public static final String ACCOUNT_SID = "91252829202f42ccb380369b886a481b";
    // 此参数在开发者注册后系统自动生成,去对应平台登录进行查看
    public static final String AUTH_TOKEN = "424a8c02ba794b3aabad9eb00c3e4db7";

    public static final String RESP_DATA_TYPE = "json";
}

HttpUtil.java

package test;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.codec.digest.DigestUtils;

public class HttpUtil
{
    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 "&timestamp=" + timestamp + "&sig=" + sig + "&respDataType=" + Config.RESP_DATA_TYPE;
    }

    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;
    }

    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;
    }
}

IndustrySMS.java

package test;

import java.net.URLEncoder;

import test.Config;
import test.HttpUtil;

public class IndustrySMS
{
    private static String operation = "/industrySMS/sendSMS";

    private static String accountSid = Config.ACCOUNT_SID;
    // 要发送到的手机号码
    private static String to = "13386895356";
    // 对应模板信息,验证码可以选择随机生成
    private static String smsContent = "【xxxxx】您的信验证码为123456,请在5分钟内使用";

    /**
     * 发送信息的方法
     */
    public static void execute()
    {
        String tmpSmsContent = null;
        try{
          tmpSmsContent = URLEncoder.encode(smsContent, "UTF-8");
        }catch(Exception e){
          
        }
        String url = Config.BASE_URL + operation;
        String body = "accountSid=" + accountSid + "&to=" + to + "&smsContent=" + tmpSmsContent
            + HttpUtil.createCommonParam();

        String result = HttpUtil.post(url, body);
        System.out.println("result:" + System.lineSeparator() + result);
    }
}

Test.java

package test;

public class Test{
    public static void main(String[] args){
        IndustrySMS.execute();
    }
}

验证思路

将手机验证码存入session中,利用ajax请求获取发送的验证码,前台页面启动定时器,在60s内可以与从后台取得的验证码进行对比验证,超时后,将获取到的验证码清空

上一篇下一篇

猜你喜欢

热点阅读