天天看点

利用阿里大于开发短信验证码

public class message
{

	//公参
	static String serverUrl = "https://eco.taobao.com/router/rest"; //正式环境
    static String appKey = "";//APP证书密钥
    static String appSecret = "";  //app安全码
    static String apiSendMethod = "alibaba.aliqin.fc.sms.num.send";  // 短信发送
    static String apiQueryMethod = "alibaba.aliqin.fc.sms.num.query";  //短信发送记录查询
    static String smsTemplateCode = "S";  //短信模板ID
    static String signName = ""; //短信签名
    static String signHmac = "hmac"; //加密方式
    static String signMd5 = "md5"; //加密方式
    static String sign = null;  // 全部参数的hmac或md5的校验码
    static String mobiles = "";  //传入多个号码,以英文逗号分隔
    static String smsParams = "{name:'xxx',code:'xx'}"; //给短信模版中的变量赋值
      
   public static void main(String[] args) throws IOException, ApiException
    {
    	TaobaoClient client = new DefaultTaobaoClient(serverUrl, appKey, appSecret);
    	AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
        Map<String, String> params = new HashMap<>();
        params.put("app_key", appKey);
        params.put("format", "json");
        params.put("method", apiSendMethod);
        params.put("rec_num", mobiles);
        params.put("sign_method", signHmac);
        params.put("timestamp", getTimeStamp());
        params.put("v", "2.0");
        params.put("sms_free_sign_name", signName);
        params.put("sms_param", smsParams);
        params.put("sms_template_code", smsTemplateCode);
        params.put("sms_type", "normal");
        sign = signTopRequest(params, appSecret, signHmac);
        System.out.println(sign);
        //代理端口设置
        System.setProperty("http.proxySet", "true");  
	System.setProperty("http.proxyHost","");  
	System.setProperty("http.proxyPort","8080");
	System.setProperty("https.proxyHost","");
	System.setProperty("https.proxyPort","8080"); 
        req.setSmsType("normal");
        req.setSmsFreeSignName(signName);
        System.out.println(smsParams);
        req.setSmsParamString(smsParams);
        req.setRecNum(mobiles);
        req.setSmsTemplateCode(smsTemplateCode);
        AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
        System.out.println(rsp.getBody());
       
    }

  

    //时间戳
    public static String getTimeStamp()
    {
        long curTime = new Date().getTime();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
        return df.format(curTime).toString();
    }
    
    public static String signTopRequest(Map<String, String> params, String secret, String signMethod) throws IOException
    {
        //检查参数是否已经排序
        String[] keys = params.keySet().toArray(new String[0]);
        Arrays.sort(keys);

        //把所有参数名和参数值串在一起
        StringBuilder query = new StringBuilder();
        if (Constants.SIGN_METHOD_MD5.equals(signMethod))
        {
            query.append(secret);
        }
        for (String key : keys)
        {
            String value = params.get(key);
            if (StringUtils.areNotEmpty(key, value))
            {
                query.append(key).append(value);
            }
        }

        // 使用MD5/HMAC加密
        byte[] bytes;
        if (Constants.SIGN_METHOD_HMAC.equals(signMethod))
        {
            bytes = encryptHMAC(query.toString(), secret);
        } else
        {
            query.append(secret);
            bytes = encryptMD5(query.toString());
        }

        // 第四步:把二进制转化为大写的十六进制
        return byte2hex(bytes);
    }

    public static byte[] encryptHMAC(String data, String secret) throws IOException
    {
        byte[] bytes = null;
        try
        {
            SecretKey secretKey = new SecretKeySpec(secret.getBytes(Constants.CHARSET_UTF8), "HmacMD5");
            Mac mac = Mac.getInstance(secretKey.getAlgorithm());
            mac.init(secretKey);
            bytes = mac.doFinal(data.getBytes(Constants.CHARSET_UTF8));
        } catch (GeneralSecurityException gse)
        {
            throw new IOException(gse.toString());
        }
        return bytes;
    }

    public static byte[] encryptMD5(String data) throws IOException
    {
        return encryptMD5(data.getBytes(Constants.CHARSET_UTF8).toString());
    }

    public static String byte2hex(byte[] bytes)
    {
        StringBuilder sign = new StringBuilder();
        for (int i = 0; i < bytes.length; i++)
        {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (hex.length() == 1)
            {
                sign.append("0");
            }
            sign.append(hex.toUpperCase());
        }
        return sign.toString();
    }
}