You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Service Bus 7.10.0 SAS认证报错:非法连接字符串参数

Azure Service Bus SDK 7.10.0 SAS认证报错:Illegal connection string parameter name: SharedAccessSignature sr

使用Azure Messaging Service Bus Library 7.10.0时,SAK认证的集成测试正常,但SAS认证时抛出异常:

java.lang.IllegalArgumentException: Illegal connection string parameter name: SharedAccessSignature sr

错误原因

SDK的ServiceBusClientBuilder.connectionString()方法要求传入完整的Service Bus连接字符串,但当前代码直接将生成的SAS Token(格式为SharedAccessSignature sr=...)传入,不符合连接字符串的解析规则。完整的Service Bus连接字符串必须包含Endpoint参数,单独的SAS Token缺少该部分,导致SDK解析参数时触发非法参数异常。

解决方案

方案1:构建完整的Service Bus连接字符串

将Endpoint地址与生成的SAS Token拼接成符合格式要求的完整连接字符串,格式为:

Endpoint=sb://{你的命名空间}.servicebus.windows.net/;SharedAccessSignature={生成的SAS Token}

修改getConnectionStringBuilder方法实现拼接:

public static String getConnectionStringBuilder(IAuthenticationProvider authToken) {
    String endpoint = authToken.getEndpointAddress();
    String sasToken = authToken.getServiceBusToken();
    return String.format("Endpoint=%s/;SharedAccessSignature=%s", endpoint, sasToken);
}

方案2:直接使用SAS Token凭证认证(推荐)

跳过connectionString方式,使用SDK的credential方法传入AzureSasCredential,更适配SAS Token的使用场景:

修改createSbReceiver方法:

@Override
public ServiceBusReceiverClient createSbReceiver(String subscriptionName) {
    try {
        AzureSasCredential sasCredential = new AzureSasCredential(authToken.getServiceBusToken());
        // 从Endpoint中提取命名空间,例如从"sb://xxx.servicebus.windows.net"提取"xxx.servicebus.windows.net"
        String namespace = authToken.getEndpointAddress().replace("sb://", "").replaceAll("/$", "");
        
        return new ServiceBusClientBuilder()
                .fullyQualifiedNamespace(namespace)
                .credential(sasCredential)
                .receiver()
                .topicName(authToken.getEntityPath())
                .subscriptionName(subscriptionName)
                .receiveMode(ServiceBusReceiveMode.PEEK_LOCK)
                .buildClient();
    } catch (Throwable exception) {
        throw ExceptionHandler.generateClientCreationException(exception, "Error creating receiver client");
    }
}

额外检查点

  • 确认生成SAS Token时的resourceUri格式正确,应为{命名空间}.servicebus.windows.net/{实体路径}(无需sb://前缀)
  • 确认getHMAC256方法生成的签名为Base64编码且未做URL编码(SDK会自动处理URL编码,重复编码会导致签名无效)

内容的提问来源于stack exchange,提问作者Nikasha Von carstein

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.21 19:27:16