Azure ServiceBus无效签名

时间:2018-11-08 17:14:37

标签: java node.js azure azureservicebus azure-servicebus-topics

我创建使用shared-access-signature npm模块生成的SAS令牌。当我尝试使用Azure SDK连接到servicebus时,出现以下错误。

com.microsoft.azure.servicebus.primitives.ServiceBusException: 
Error{condition=com.microsoft:auth-failed, description='InvalidSignature:
The token has an invalid signature.', info=null}

我的SAS令牌看起来像这样

SharedAccessSignature sr=https%3A%2F%2Fmy-servicebus-dev.servicebus.windows.net%2F&sig=somesig%2idonotdisclose0e1g%3D&se=1541700607.155&skn=RootManageSharedAccessKey

如何使用SAS令牌?应该是整个字符串还是只是SharedAccessSignature以外的部分?

请帮助。我没头绪了。

1 个答案:

答案 0 :(得分:0)

  

如何使用SAS令牌?应该是整个字符串还是   仅是SharedAccessSignature以外的部分?

根据我的测试,答案是肯定的。您需要将参数作为整个字符串传递,包括SharedAccessSignature sr=

我使用您提供的npm lib生成sas令牌。

var sas = require('shared-access-signature');

var url = 'https://******.servicebus.windows.net/BasicQueue';
var sharedAccessKeyName = 'RootManageSharedAccessKey';
var sharedAccessKey = '***';
var currentDate = new Date();
var expiry = currentDate.getTime() / 1000 + 3600; // We require expiry time in seconds since epoch.

var sas = require('shared-access-signature');
var signature = sas.generateServiceBusSignature(url, sharedAccessKeyName, sharedAccessKey, expiry);
console.log(signature);

然后在下面的Java代码中使用它:

import com.microsoft.azure.servicebus.Message;
import com.microsoft.azure.servicebus.QueueClient;
import com.microsoft.azure.servicebus.ReceiveMode;
import com.microsoft.azure.servicebus.primitives.ConnectionStringBuilder;
import com.microsoft.azure.servicebus.primitives.ServiceBusException;
public class SendMessages {
    private static String entityPath = "BasicQueue";

    private static String namespaceName = "***";

    private static String sharedAccessSingature = "SharedAccessSignature sr=https%3A%2F%2F***.servicebus.windows.net%2FBasicQueue&sig=ewu7ZwgPgDVSHBZiYB7paBp94KuMtby%2BiwK0fDJ5GLM%3D&se=1541735153.139&skn=RootManageSharedAccessKey";


    public static void main(String[] args) throws ServiceBusException, InterruptedException {

        QueueClient sendClient = new QueueClient(new ConnectionStringBuilder(namespaceName, entityPath, sharedAccessSingature), ReceiveMode.PEEKLOCK);

        String test = "test for 111";

        Message message = new Message(test);

        sendClient.sendAsync(message).thenRunAsync(() -> {
            System.out.printf("\n\tMessage acknowledged: Id = %s", message.getMessageId());
        });

        System.out.println("send success");
    }
}