HTTP协议及签名怎么实现?(4)
参数说明:
- accessSecret:你的AccessKeyId对应的秘钥AccessSecret,特别说明:POP要求需要后面多加一个“&”字符,即accessSecret + “&”
- stringToSign:即第三步生成的待签名请求串
签名后的结果打印如下:
- [backcolor=transparent]zJDF[backcolor=transparent]+[backcolor=transparent]Lrzhj[backcolor=transparent]/[backcolor=transparent]ThnlvIToysFRq6t4[backcolor=transparent]=
第五步:增加签名结果到请求参数中,发送请求
注意:签名也要做特殊URL编码
- [backcolor=transparent]String[backcolor=transparent] [backcolor=transparent]Signature[backcolor=transparent] [backcolor=transparent]=[backcolor=transparent] specialUrlEncode[backcolor=transparent]([backcolor=transparent]sign[backcolor=transparent]);[backcolor=transparent]// zJDF%2BLrzhj%2FThnlvIToysFRq6t4%3D
最终完整的GET请求HTTP为:
- [backcolor=transparent]http[backcolor=transparent]:[backcolor=transparent]//dysmsapi.aliyuncs.com/?Signature=zJDF%2BLrzhj%2FThnlvIToysFRq6t4%3D&AccessKeyId=testId&Action=SendSms&Format=XML&OutId=123&PhoneNumbers=15300000001&RegionId=cn-hangzhou&SignName=%E9%98%BF%E9%87%8C%E4%BA%91%E7%9F%AD%E4%BF%A1%E6%B5%8B%E8%AF%95%E4%B8%93%E7%94%A8&SignatureMethod=HMAC-SHA1&SignatureNonce=45e25e9b-0a6f-4070-8c85-2956eda1b466&SignatureVersion=1.0&TemplateCode=SMS_71390007&TemplateParam=%7B%22customer%22%3A%22test%22%7D&Timestamp=2017-07-12T02%3A42%3A19Z&Version=2017-05-25
三、附加完整的Java签名Demo代码
- [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]SignDemo[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] main[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent][][backcolor=transparent] args[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]throws[backcolor=transparent] [backcolor=transparent]Exception[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] accessKeyId [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"testId"[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] accessSecret [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"testSecret"[backcolor=transparent];
- [backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]text[backcolor=transparent].[backcolor=transparent]SimpleDateFormat[backcolor=transparent] df [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]text[backcolor=transparent].[backcolor=transparent]SimpleDateFormat[backcolor=transparent]([backcolor=transparent]"yyyy-MM-dd'T'HH:mm:ss'Z'"[backcolor=transparent]);
- [backcolor=transparent] df[backcolor=transparent].[backcolor=transparent]setTimeZone[backcolor=transparent]([backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]SimpleTimeZone[backcolor=transparent]([backcolor=transparent]0[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"GMT"[backcolor=transparent]));[backcolor=transparent]// 这里一定要设置GMT时区
- [backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]Map[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent]>[backcolor=transparent] paras [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]HashMap[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent]>();
- [backcolor=transparent] [backcolor=transparent]// 1. 系统参数
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignatureMethod"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"HMAC-SHA1"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignatureNonce"[backcolor=transparent],[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]UUID[backcolor=transparent].[backcolor=transparent]randomUUID[backcolor=transparent]().[backcolor=transparent]toString[backcolor=transparent]());
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"AccessKeyId"[backcolor=transparent],[backcolor=transparent] accessKeyId[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignatureVersion"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"1.0"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Timestamp"[backcolor=transparent],[backcolor=transparent] df[backcolor=transparent].[backcolor=transparent]format[backcolor=transparent]([backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]Date[backcolor=transparent]()));
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Format"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"XML"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 2. 业务API参数
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Action"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"SendSms"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Version"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"2017-05-25"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"RegionId"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"cn-hangzhou"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"PhoneNumbers"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"15300000001"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignName"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"阿里云短信测试专用"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"TemplateParam"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"{\"customer\":\"test\"}"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"TemplateCode"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"SMS_71390007"[backcolor=transparent]);
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"OutId"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"123"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 3. 去除签名关键字Key
- [backcolor=transparent] [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]containsKey[backcolor=transparent]([backcolor=transparent]"Signature"[backcolor=transparent]))
- [backcolor=transparent] paras[backcolor=transparent].[backcolor=transparent]remove[backcolor=transparent]([backcolor=transparent]"Signature"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 4. 参数KEY排序
- [backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]TreeMap[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent]>[backcolor=transparent] sortParas [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]TreeMap[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent]>();
- [backcolor=transparent] sortParas[backcolor=transparent].[backcolor=transparent]putAll[backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 5. 构造待签名的字符串
- [backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]util[backcolor=transparent].[backcolor=transparent]Iterator[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent]>[backcolor=transparent] it [backcolor=transparent]=[backcolor=transparent] sortParas[backcolor=transparent].[backcolor=transparent]keySet[backcolor=transparent]().[backcolor=transparent]iterator[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]StringBuilder[backcolor=transparent] sortQueryStringTmp [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]StringBuilder[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]while[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]it[backcolor=transparent].[backcolor=transparent]hasNext[backcolor=transparent]())[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] key [backcolor=transparent]=[backcolor=transparent] it[backcolor=transparent].[backcolor=transparent]next[backcolor=transparent]();
- [backcolor=transparent] sortQueryStringTmp[backcolor=transparent].[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]"&"[backcolor=transparent]).[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]specialUrlEncode[backcolor=transparent]([backcolor=transparent]key[backcolor=transparent])).[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]"="[backcolor=transparent]).[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]specialUrlEncode[backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]get[backcolor=transparent]([backcolor=transparent]key[backcolor=transparent])));
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] sortedQueryString [backcolor=transparent]=[backcolor=transparent] sortQueryStringTmp[backcolor=transparent].[backcolor=transparent]substring[backcolor=transparent]([backcolor=transparent]1[backcolor=transparent]);[backcolor=transparent]// 去除第一个多余的&符号
- [backcolor=transparent] [backcolor=transparent]StringBuilder[backcolor=transparent] stringToSign [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]StringBuilder[backcolor=transparent]();
- [backcolor=transparent] stringToSign[backcolor=transparent].[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]"GET"[backcolor=transparent]).[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]"&"[backcolor=transparent]);
- [backcolor=transparent] stringToSign[backcolor=transparent].[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]specialUrlEncode[backcolor=transparent]([backcolor=transparent]"/"[backcolor=transparent])).[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]"&"[backcolor=transparent]);
- [backcolor=transparent] stringToSign[backcolor=transparent].[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]specialUrlEncode[backcolor=transparent]([backcolor=transparent]sortedQueryString[backcolor=transparent]));
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] sign [backcolor=transparent]=[backcolor=transparent] sign[backcolor=transparent]([backcolor=transparent]accessSecret [backcolor=transparent]+[backcolor=transparent] [backcolor=transparent]"&"[backcolor=transparent],[backcolor=transparent] stringToSign[backcolor=transparent].[backcolor=transparent]toString[backcolor=transparent]());
- [backcolor=transparent] [backcolor=transparent]// 6. 签名最后也要做特殊URL编码
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] signature [backcolor=transparent]=[backcolor=transparent] specialUrlEncode[backcolor=transparent]([backcolor=transparent]sign[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]get[backcolor=transparent]([backcolor=transparent]"SignatureNonce"[backcolor=transparent]));
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"\r\n=========\r\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]get[backcolor=transparent]([backcolor=transparent]"Timestamp"[backcolor=transparent]));
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"\r\n=========\r\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]sortedQueryString[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"\r\n=========\r\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]stringToSign[backcolor=transparent].[backcolor=transparent]toString[backcolor=transparent]());
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"\r\n=========\r\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]sign[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"\r\n=========\r\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]signature[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"\r\n=========\r\n"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]// 最终打印出合法GET请求的URL
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"http://dysmsapi.aliyuncs.com/?Signature="[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] signature [backcolor=transparent]+[backcolor=transparent] sortQueryStringTmp[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] specialUrlEncode[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent] value[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]throws[backcolor=transparent] [backcolor=transparent]Exception[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] java[backcolor=transparent].[backcolor=transparent]net[backcolor=transparent].[backcolor=transparent]URLEncoder[backcolor=transparent].[backcolor=transparent]encode[backcolor=transparent]([backcolor=transparent]value[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"UTF-8"[backcolor=transparent]).[backcolor=transparent]replace[backcolor=transparent]([backcolor=transparent]"+"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"%20"[backcolor=transparent]).[backcolor=transparent]replace[backcolor=transparent]([backcolor=transparent]"*"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"%2A"[backcolor=transparent]).[backcolor=transparent]replace[backcolor=transparent]([backcolor=transparent]"%7E"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"~"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] sign[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent] accessSecret[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] stringToSign[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]throws[backcolor=transparent] [backcolor=transparent]Exception[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] javax[backcolor=transparent].[backcolor=transparent]crypto[backcolor=transparent].[backcolor=transparent]Mac[backcolor=transparent] mac [backcolor=transparent]=[backcolor=transparent] javax[backcolor=transparent].[backcolor=transparent]crypto[backcolor=transparent].[backcolor=transparent]Mac[backcolor=transparent].[backcolor=transparent]getInstance[backcolor=transparent]([backcolor=transparent]"HmacSHA1"[backcolor=transparent]);
- [backcolor=transparent] mac[backcolor=transparent].[backcolor=transparent]init[backcolor=transparent]([backcolor=transparent]new[backcolor=transparent] javax[backcolor=transparent].[backcolor=transparent]crypto[backcolor=transparent].[backcolor=transparent]spec[backcolor=transparent].[backcolor=transparent]SecretKeySpec[backcolor=transparent]([backcolor=transparent]accessSecret[backcolor=transparent].[backcolor=transparent]getBytes[backcolor=transparent]([backcolor=transparent]"UTF-8"[backcolor=transparent]),[backcolor=transparent] [backcolor=transparent]"HmacSHA1"[backcolor=transparent]));
- [backcolor=transparent] [backcolor=transparent]byte[backcolor=transparent][][backcolor=transparent] signData [backcolor=transparent]=[backcolor=transparent] mac[backcolor=transparent].[backcolor=transparent]doFinal[backcolor=transparent]([backcolor=transparent]stringToSign[backcolor=transparent].[backcolor=transparent]getBytes[backcolor=transparent]([backcolor=transparent]"UTF-8"[backcolor=transparent]));
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] sun[backcolor=transparent].[backcolor=transparent]misc[backcolor=transparent].[backcolor=transparent]BASE64Encoder[backcolor=transparent]().[backcolor=transparent]encode[backcolor=transparent]([backcolor=transparent]signData[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent]}
- [backcolor=transparent]`
收起
猫饭先生
2017-10-25 14:03:15
1476
0
0
条回答
写回答
取消
提交回答