如何使用REST API(ARM)访问Azure存储表

时间:2016-07-28 11:53:32

标签: java azure azure-storage azure-table-storage

通过查看以下链接,不清楚签名应包含什么或如何形成规范字符串以创建可使用HMAC-SH256算法加密的签名。

https://msdn.microsoft.com/en-us/library/azure/dd179428.aspx

我正在使用以下网址

获取https://mystorageaccount.table.core.windows.net/Tables

接头:

Authorization   SharedKeyLite mystorrageaccount:<<encrypted signature>>
x-ms-date   Thu 28 Jul 2016 11:19:33 GMT

获取以下错误:

  

服务器无法验证请求。确保价值   正确形成授权标头,包括签名。

1 个答案:

答案 0 :(得分:0)

@Prit,请参阅下面的代码,以生成表存储的共享密钥精简作为参考。

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.commons.codec.binary.Base64;

String secret = "<storage-account-key>";
// Date for string to sign
Calendar calendar = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss 'GMT'", Locale.US);
sdf.setTimeZone(TimeZone.getTimeZone("GMT"));
String date = sdf.format(calendar.getTime());
// canonicalizedResource, such as "/testaccount1/Tables"
String canonicalizedResource = "<Canonicalized-Resource>";
String stringToSign = date + "\n" + canonicalizedResource;
System.out.println(stringToSign);
// HMAC-SHA@%^
Mac sha256HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
sha256HMAC.init(secretKey);
String hash = Base64.encodeBase64String(sha256HMAC.doFinal(stringToSign.getBytes()));
System.out.println(hash);
相关问题