生成java中的sas令牌以下载azure数据存储容器中的文件

o4hqfura  于 2021-06-30  发布在  Java
关注(0)|答案(2)|浏览(360)

尝试生成sas令牌以访问存储帐户中的某些文件。我使用的方法如下:
https://docs.microsoft.com/en-us/rest/api/eventhub/generate-sas-token
现在,我的问题是,就我的一生而言,我不能让sastoken字符串工作。如果我通过门户(存储帐户中的共享访问签名)生成令牌,我可以通过带有所提供令牌的url访问这些文件。
然而,我还不能通过java使用上面链接的方法编程生成sas令牌。我想我的问题是正在加密的stringtosign。在构造要加密的字符串时,我一直遵循以下示例:
https://docs.microsoft.com/en-us/rest/api/storageservices/constructing-an-account-sas
我所有的努力都导致了:

<AuthenticationErrorDetail>Signature fields not well formed.</AuthenticationErrorDetail>

<AuthenticationErrorDetail>Signature did not match. String to sign used was <insert string details here>

查看对我有效的门户生成的sastoken:
?sv=2017-11-09&ss=f&srt=o&sp=r&se=2018-12-06t22:15:20z&st=2018-12-06t14:15:20z&spr=https&sig=%2bi1tw5d80u%2boaieobh1wjao1p4xvfx4nrzt%2fzwiszy%3d
我好像需要这样一根绳子:

String stringToSign = accountName + "\n" +
                "r\n" +
                "f\n" +
                "o\n" +
                URLEncoder.encode(start, "UTF-8") + "\n" +
                URLEncoder.encode(expiry, "UTF-8") + "\n" +
                "\n" +
                "https\n" +
                azureApiVersion;

其中accountname是来自azure的存储帐户名,start/expiry是开始和到期字符串(即-2018-12-06t22:15:20z),azureapiversion是“2017-11-09”。
然后,我尝试在构造字符串后返回标记,如下所示:

String signature = getHMAC256(key, stringToSign);
        sasToken = "sv=" + azureApiVersion +
                "&ss=f" +
                "&srt=o" +
                "&sp=r" +
                "&se=" +URLEncoder.encode(expiry, "UTF-8") +
                "&st=" + URLEncoder.encode(start, "UTF-8") +
                "&spr=https" +
                "&sig=" + URLEncoder.encode(signature, "UTF-8");

我尝试了url编码,而不是url编码的开始/到期日期以及,以防万一,这是混乱的事情。我错过了什么?

kgqe7b3p

kgqe7b3p1#

要解决的三个问题 getHMAC256 @gaurav提到的方法问题
不编码 expiry 以及 startstringToSign 或者签名不匹配。因为url中的编码部分将由azure存储服务解码以计算预期的签名。
stringToSign ,小姐 \n 之后 azureApiVersion .
这是完整的样品。

public static void GetFileSAS(){

    String accountName = "accountName";
    String key = "accountKey";
    String resourceUrl = "https://"+accountName+".file.core.windows.net/fileShare/fileName";

    String start = "startTime";
    String expiry = "expiry";
    String azureApiVersion = "2017-11-09";

    String stringToSign = accountName + "\n" +
                "r\n" +
                "f\n" +
                "o\n" +
                start + "\n" +
                expiry + "\n" +
                "\n" +
                "https\n" +
                azureApiVersion+"\n";

    String signature = getHMAC256(key, stringToSign);

    try{

        String sasToken = "sv=" + azureApiVersion +
            "&ss=f" +
            "&srt=o" +
            "&sp=r" +
            "&se=" +URLEncoder.encode(expiry, "UTF-8") +
            "&st=" + URLEncoder.encode(start, "UTF-8") +
            "&spr=https" +
            "&sig=" + URLEncoder.encode(signature, "UTF-8");

    System.out.println(resourceUrl+"?"+sasToken);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

private static String getHMAC256(String accountKey, String signStr) {
    String signature = null;
    try {
        SecretKeySpec secretKey = new SecretKeySpec(Base64.getDecoder().decode(accountKey), "HmacSHA256");
        Mac sha256HMAC = Mac.getInstance("HmacSHA256");
        sha256HMAC.init(secretKey);
        signature = Base64.getEncoder().encodeToString(sha256HMAC.doFinal(signStr.getBytes("UTF-8")));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return signature;
}
mzillmmw

mzillmmw2#

我有一个更简单的方法

SharedAccessAccountPolicy sharedAccessAccountPolicy = new SharedAccessAccountPolicy();
sharedAccessAccountPolicy.setPermissionsFromString("racwdlup");
long date = new Date().getTime();
long expiryDate = new Date(date + 8640000).getTime();
sharedAccessAccountPolicy.setSharedAccessStartTime(new Date(date));
sharedAccessAccountPolicy.setSharedAccessExpiryTime(new Date(expiryDate));
sharedAccessAccountPolicy.setResourceTypeFromString("sco");
sharedAccessAccountPolicy.setServiceFromString("bfqt");
String sasToken = "?" + storageAccount.generateSharedAccessSignature(sharedAccessAccountPolicy);

您可以这样获得存储帐户:

private String storageConnectionString = "DefaultEndpointsProtocol=https;AccountName=<storage name>;AccountKey=<your key>;EndpointSuffix=core.windows.net";
storageAccount = CloudStorageAccount.parse(storageConnectionString);

相关问题