使用java访问Azure documentDB

时间:2014-10-30 13:46:47

标签: java azure azure-cosmosdb

任何人都可以使用java发布一些代码片段来访问Azure DocumentDB吗?

我能够理解如何键入要签名的字符串               StringToSign = VERB +“\ n”+ ResourceType +“\ n”+ ResourceId +“\ n”+ x-ms-date +“\ n”+
                             日期+“\ n”;

1 个答案:

答案 0 :(得分:0)

这是基于http://msdn.microsoft.com/en-us/library/azure/dn783368.aspx中的JavaScript示例的Java代码段:

public static String generateAuthHeader(String verb, String resourceId,
        String resourceType, String date, String masterKeyBase64)
        throws Exception {

    // Decode the master key and setup the MAC object for signing.
    byte[] masterKeyBytes = Base64.decodeBase64(masterKeyBase64.getBytes());
    Mac mac = Mac.getInstance("HMACSHA256");
    mac.init(new SecretKeySpec(masterKeyBytes, "HMACSHA256"));

    // Build the unsigned authorization string.
    String stringToSign = verb + "\n"
            + resourceType + "\n"
            + resourceId + "\n"
            + date + "\n"
            + "\n";

    // Sign and encode the authorization string.
    String signature = Base64.encodeBase64String(mac.doFinal(stringToSign.toLowerCase().getBytes()));

    // Generate the authorization header.
    String authHeader = URLEncoder.encode("type=master&ver=1.0&sig=" + signature, "UTF-8");

    return authHeader;
}

我使用Apache Commons Codec进行Base64编码/解码。

顺便说一句,我对涉及DocumentDB和Java的项目非常感兴趣...如果您需要帮助,请随时联系我(andrl {at} microsoft.com) /你的项目!

编辑:自问题得到解答以来,已发布了DocumentDB Java SDK。查看:https://github.com/Azure/azure-documentdb-java