美文网首页
阿里云短信签名机制实现[java]

阿里云短信签名机制实现[java]

作者: 0316 | 来源:发表于2016-10-26 09:49 被阅读0次

    公司要求用阿里云短信进行开发,由于网上教程比较少,先整理下代码,如下:

    import java.io.UnsupportedEncodingException;

    import java.net.URLEncoder;

    import java.security.InvalidKeyException;

    import java.security.NoSuchAlgorithmException;

    import java.util.Arrays;

    import java.util.Calendar;

    import java.util.GregorianCalendar;

    import java.util.HashMap;

    import java.util.Map;

    import javax.crypto.Mac;

    import javax.crypto.spec.SecretKeySpec;

    import sun.misc.BASE64Encoder;

    /**

    * 阿里云短信签名机制

    * @author yuj

    *

    */

    public class SMSSignatureUtil {

    private static final String ACTION = "SingleSendSms";

    private static final String XML = "XML";

    private static final String REGIONID = "cn-guangzhou";

    private static final String SIGNATUREMETHOD = "cn-guangzhou";

    private static final String SIGNATUREVERSION = "1.0";

    private static final String VERSION = "2016-09-27";

    /**

    * 签名结果串-阿里云短信

    * @param accessKeyId - testid

    * @param jsonParams - {\"name\":\"d\",\"name1\":\"d\"}

    * @param recNum - 13098765432

    * @param signName - 标签测试

    * @param signatureNonce - 9e030f6b-03a2-40f0-a6ba-157d44532fd0

    * @param templateCode - SMS_1650053

    * @param timestamp - 2016-10-20T05:37:52Z

    * @return signature - ka8PDlV7S9sYqxEMRnmlBv%2FDoAE%3D

    */

    public static String getSignature(String accessKeyId,String jsonParams,String recNum,

    String signName,String signatureNonce,String templateCode,String timestamp)

    {

    String signature = "";

    try {

    Map parameters = new HashMap();

    parameters.put("AccessKeyId", accessKeyId);

    parameters.put("Action", ACTION);

    parameters.put("Format", XML);

    parameters.put("ParamString", jsonParams);

    parameters.put("RecNum", recNum);

    parameters.put("RegionId", REGIONID);

    parameters.put("SignName", signName);

    parameters.put("SignatureMethod", SIGNATUREMETHOD);

    parameters.put("SignatureNonce", signatureNonce);

    parameters.put("SignatureVersion", SIGNATUREVERSION);

    parameters.put("TemplateCode", templateCode);

    parameters.put("Timestamp", timestamp);

    parameters.put("Version", VERSION);

    // 对参数进行排序

    String[] sortedKeys = (String[]) parameters.keySet().toArray(new String[] {});

    Arrays.sort(sortedKeys);

    final String SEPARATOR = "&";

    // 生成stringToSign字符串

    StringBuilder stringToSign = new StringBuilder();

    stringToSign.append("POST").append(SEPARATOR);

    stringToSign.append(percentEncode("/")).append(SEPARATOR);

    StringBuilder canonicalizedQueryString = new StringBuilder();

    for (String key : sortedKeys) {

    // 这里注意对key和value进行编码

    canonicalizedQueryString.append("&").append(percentEncode(key))

    .append("=").append(percentEncode((String) parameters.get(key)));

    }

    // 这里注意对canonicalizedQueryString进行编码

    stringToSign.append(percentEncode(canonicalizedQueryString.toString()

    .substring(1)));

    final String ALGORITHM = "HmacSHA1";

    final String ENCODING = "UTF-8";

    String key = "testsecret&";

    Mac mac = Mac.getInstance(ALGORITHM);

    mac.init(new SecretKeySpec(key.getBytes(ENCODING), ALGORITHM));

    byte[] signData = mac.doFinal(stringToSign.toString().getBytes(ENCODING));

    signature = new String(new BASE64Encoder().encode(signData));

    signature = percentEncode(signature);

    } catch (InvalidKeyException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    } catch (UnsupportedEncodingException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    } catch (NoSuchAlgorithmException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    } catch (IllegalStateException e) {

    // TODO Auto-generated catch block

    e.printStackTrace();

    }

    return signature;

    }

    private static String percentEncode(String value) throws UnsupportedEncodingException {

    return value != null ? URLEncoder.encode(value, "utf-8").replace("+", "%20").replace("*", "%2A").replace("%7E", "~") : null;

    }

    }

    相关文章

      网友评论

          本文标题:阿里云短信签名机制实现[java]

          本文链接:https://www.haomeiwen.com/subject/reeduttx.html