阿里云短信验证码发送工具类

阿里云短信验证码接口 


返回url会得到是否发送成功,发送短信验证码的手机号,但是每一分钟只能发送一次,一天最多10条

package com.yunshop.utils;


import javax.servlet.http.HttpSession;

public class Sendsms {
	public String sendmes(String phone,HttpSession session,String mid) throws Exception{
		
	String accessKeyId = "通过阿里云的短信服务获取的ID";    
        String accessSecret = "通过阿里云的短信服务获取的secret";

        java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
        df.setTimeZone(new java.util.SimpleTimeZone(0, "GMT"));// 这里一定要设置GMT时区
        int mobile_code = (int)((Math.random()*9+1)*100000);
        java.util.Map paras = new java.util.HashMap();
        // 1. 系统参数
        paras.put("SignatureMethod", "HMAC-SHA1");
        paras.put("SignatureNonce", java.util.UUID.randomUUID().toString());
        paras.put("AccessKeyId", accessKeyId);
        paras.put("SignatureVersion", "1.0");
        paras.put("Timestamp", df.format(new java.util.Date()));
        paras.put("Format", "XML");

        // 2. 业务API参数
        paras.put("Action", "SendSms");
        paras.put("Version", "2017-05-25");
        paras.put("RegionId", "cn-hangzhou");
        paras.put("PhoneNumbers", phone);    //发送短信手机号
        paras.put("SignName", "短信签名");    //阿里云短信审核通过的短信签名
        paras.put("TemplateParam", "{\"code\":\""+mobile_code+"\"}");//短信模板的变量
        paras.put("TemplateCode", mid);//阿里云短信验证码获得的短信模板id
        paras.put("OutId", "123");

        session.setAttribute("mcode",mobile_code);
	    session.setAttribute("yzmphone",phone);
        
        // 3. 去除签名关键字Key
        if (paras.containsKey("Signature"))
            paras.remove("Signature");

        // 4. 参数KEY排序
        java.util.TreeMap sortParas = new java.util.TreeMap();
        sortParas.putAll(paras);

        // 5. 构造待签名的字符串
        java.util.Iterator it = sortParas.keySet().iterator();
        StringBuilder sortQueryStringTmp = new StringBuilder();
        while (it.hasNext()) {
            String key = it.next();
            sortQueryStringTmp.append("&").append(specialUrlEncode(key)).append("=").append(specialUrlEncode(paras.get(key)));
        }
        String sortedQueryString = sortQueryStringTmp.substring(1);// 去除第一个多余的&符号

        StringBuilder stringToSign = new StringBuilder();
        stringToSign.append("GET").append("&");
        stringToSign.append(specialUrlEncode("/")).append("&");
        stringToSign.append(specialUrlEncode(sortedQueryString));

        String sign = sign(accessSecret + "&", stringToSign.toString());
        // 6. 签名最后也要做特殊URL编码
        String signature = specialUrlEncode(sign);

//        System.out.println(paras.get("SignatureNonce"));
//        System.out.println("\r\n=========\r\n");
//        System.out.println(paras.get("Timestamp"));
//        System.out.println("\r\n=========\r\n");
//        System.out.println(sortedQueryString);
//        System.out.println("\r\n=========\r\n");
//        System.out.println(stringToSign.toString());
//        System.out.println("\r\n=========\r\n");
//        System.out.println(sign);
//        System.out.println("\r\n=========\r\n");
//        System.out.println(signature);
//        System.out.println("\r\n=========\r\n");
//        // 最终打印出合法GET请求的URL
//        System.out.println("http://dysmsapi.aliyuncs.com/?Signature=" + signature + sortQueryStringTmp);	
		  String url1 = "http://dysmsapi.aliyuncs.com/?Signature=" + signature + sortQueryStringTmp;
//		  URL url = new URL(url1);//你要登陆网站的登陆界面的地址. 
//		  HttpURLConnection con = (HttpURLConnection) url.openConnection(); 
//		  con.setDoOutput(true); // POST方式 
//		  con.setRequestMethod("POST");
//		
//		  con.connect();  
		  return url1;
	}
	
	
	public static String specialUrlEncode(String value) throws Exception {
        return java.net.URLEncoder.encode(value, "UTF-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~");
    }

    public static String sign(String accessSecret, String stringToSign) throws Exception {
        javax.crypto.Mac mac = javax.crypto.Mac.getInstance("HmacSHA1");
        mac.init(new javax.crypto.spec.SecretKeySpec(accessSecret.getBytes("UTF-8"), "HmacSHA1"));
        byte[] signData = mac.doFinal(stringToSign.getBytes("UTF-8"));
        return new sun.misc.BASE64Encoder().encode(signData);
    }
}


你可能感兴趣的:(工具类)