开发者社区> 问答> 正文

HTTP协议及签名怎么实现?(4)


参数说明:

  1. accessSecret:你的AccessKeyId对应的秘钥AccessSecret,特别说明:POP要求需要后面多加一个“&”字符,即accessSecret + “&”
  2. stringToSign:即第三步生成的待签名请求串

签名后的结果打印如下:
  1. [backcolor=transparent]zJDF[backcolor=transparent]+[backcolor=transparent]Lrzhj[backcolor=transparent]/[backcolor=transparent]ThnlvIToysFRq6t4[backcolor=transparent]=


第五步:增加签名结果到请求参数中,发送请求


注意:签名也要做特殊URL编码
  1. [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为:
  1. [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代码

  1. [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]SignDemo[backcolor=transparent] [backcolor=transparent]{
  2. [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]{
  3. [backcolor=transparent]        [backcolor=transparent]String[backcolor=transparent] accessKeyId [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"testId"[backcolor=transparent];
  4. [backcolor=transparent]        [backcolor=transparent]String[backcolor=transparent] accessSecret [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]"testSecret"[backcolor=transparent];
  5. [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]);
  6. [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时区
  7. [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]>();
  8. [backcolor=transparent]        [backcolor=transparent]// 1. 系统参数
  9. [backcolor=transparent]        paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignatureMethod"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"HMAC-SHA1"[backcolor=transparent]);
  10. [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]());
  11. [backcolor=transparent]        paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"AccessKeyId"[backcolor=transparent],[backcolor=transparent] accessKeyId[backcolor=transparent]);
  12. [backcolor=transparent]        paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignatureVersion"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"1.0"[backcolor=transparent]);
  13. [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]()));
  14. [backcolor=transparent]        paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Format"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"XML"[backcolor=transparent]);
  15. [backcolor=transparent]        [backcolor=transparent]// 2. 业务API参数
  16. [backcolor=transparent]        paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Action"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"SendSms"[backcolor=transparent]);
  17. [backcolor=transparent]        paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"Version"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"2017-05-25"[backcolor=transparent]);
  18. [backcolor=transparent]        paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"RegionId"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"cn-hangzhou"[backcolor=transparent]);
  19. [backcolor=transparent]        paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"PhoneNumbers"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"15300000001"[backcolor=transparent]);
  20. [backcolor=transparent]        paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"SignName"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"阿里云短信测试专用"[backcolor=transparent]);
  21. [backcolor=transparent]        paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"TemplateParam"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"{\"customer\":\"test\"}"[backcolor=transparent]);
  22. [backcolor=transparent]        paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"TemplateCode"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"SMS_71390007"[backcolor=transparent]);
  23. [backcolor=transparent]        paras[backcolor=transparent].[backcolor=transparent]put[backcolor=transparent]([backcolor=transparent]"OutId"[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]"123"[backcolor=transparent]);
  24. [backcolor=transparent]        [backcolor=transparent]// 3. 去除签名关键字Key
  25. [backcolor=transparent]        [backcolor=transparent]if[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent].[backcolor=transparent]containsKey[backcolor=transparent]([backcolor=transparent]"Signature"[backcolor=transparent]))
  26. [backcolor=transparent]            paras[backcolor=transparent].[backcolor=transparent]remove[backcolor=transparent]([backcolor=transparent]"Signature"[backcolor=transparent]);
  27. [backcolor=transparent]        [backcolor=transparent]// 4. 参数KEY排序
  28. [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]>();
  29. [backcolor=transparent]        sortParas[backcolor=transparent].[backcolor=transparent]putAll[backcolor=transparent]([backcolor=transparent]paras[backcolor=transparent]);
  30. [backcolor=transparent]        [backcolor=transparent]// 5. 构造待签名的字符串
  31. [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]();
  32. [backcolor=transparent]        [backcolor=transparent]StringBuilder[backcolor=transparent] sortQueryStringTmp [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]StringBuilder[backcolor=transparent]();
  33. [backcolor=transparent]        [backcolor=transparent]while[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]it[backcolor=transparent].[backcolor=transparent]hasNext[backcolor=transparent]())[backcolor=transparent] [backcolor=transparent]{
  34. [backcolor=transparent]            [backcolor=transparent]String[backcolor=transparent] key [backcolor=transparent]=[backcolor=transparent] it[backcolor=transparent].[backcolor=transparent]next[backcolor=transparent]();
  35. [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])));
  36. [backcolor=transparent]        [backcolor=transparent]}
  37. [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]// 去除第一个多余的&符号
  38. [backcolor=transparent]        [backcolor=transparent]StringBuilder[backcolor=transparent] stringToSign [backcolor=transparent]=[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]StringBuilder[backcolor=transparent]();
  39. [backcolor=transparent]        stringToSign[backcolor=transparent].[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]"GET"[backcolor=transparent]).[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]"&"[backcolor=transparent]);
  40. [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]);
  41. [backcolor=transparent]        stringToSign[backcolor=transparent].[backcolor=transparent]append[backcolor=transparent]([backcolor=transparent]specialUrlEncode[backcolor=transparent]([backcolor=transparent]sortedQueryString[backcolor=transparent]));
  42. [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]());
  43. [backcolor=transparent]        [backcolor=transparent]// 6. 签名最后也要做特殊URL编码
  44. [backcolor=transparent]        [backcolor=transparent]String[backcolor=transparent] signature [backcolor=transparent]=[backcolor=transparent] specialUrlEncode[backcolor=transparent]([backcolor=transparent]sign[backcolor=transparent]);
  45. [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]));
  46. [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]);
  47. [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]));
  48. [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]);
  49. [backcolor=transparent]        [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]sortedQueryString[backcolor=transparent]);
  50. [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]);
  51. [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]());
  52. [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]);
  53. [backcolor=transparent]        [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]sign[backcolor=transparent]);
  54. [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]);
  55. [backcolor=transparent]        [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]signature[backcolor=transparent]);
  56. [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]);
  57. [backcolor=transparent]        [backcolor=transparent]// 最终打印出合法GET请求的URL
  58. [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]);
  59. [backcolor=transparent]    [backcolor=transparent]}
  60. [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]{
  61. [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]);
  62. [backcolor=transparent]    [backcolor=transparent]}
  63. [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]{
  64. [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]);
  65. [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]));
  66. [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]));
  67. [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]);
  68. [backcolor=transparent]    [backcolor=transparent]}
  69. [backcolor=transparent]}
  70. [backcolor=transparent]`

展开
收起
猫饭先生 2017-10-25 14:03:15 1475 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
阿里巴巴HTTP 2.0实践及无线通信协议的演进之路 立即下载
CDN助力企业网站进入HTTPS时代 立即下载
低代码开发师(初级)实战教程 立即下载