Azure AMQP Service Bus CBS 身份验证

· 功能

sgcWebSockets 2026.1.0 起,Azure AMQP 1.0 支持使用 SAS Token 和 JWT 进行 CBS 身份验证

Azure Service Bus 通过 AMQP 实现基于声明的安全性(CBS),在初始 SASL 握手后对发送方和接收方进行授权。客户端向 $cbs 节点打开管理链路,并发送包含共享访问签名(SAS)token 或 Microsoft Entra ID 颁发的 JSON Web Token(JWT)的 put-token 请求。一旦代理验证 token,授权即在其有效期内缓存,应用程序即可针对队列、主题或订阅创建发送和接收链路。


Azure CBS 工作原理

Azure Service Bus 通过 AMQP 实现 CBS,在初始 SASL 握手后对发送方和接收方进行授权。客户端向 $cbs 节点打开管理链路,并发送包含 SAS token 或 Microsoft Entra ID 颁发的 JWT 的 put-token 请求。一旦代理验证 token,授权即在其有效期内缓存,应用程序即可针对队列、主题或订阅创建发送和接收链路。

sgcWebSockets 2026.1.0 中的 AMQP1 客户端通过两个对 Delphi 开发者友好的辅助方法自动化此流程:


两种方法均需要有效的 AMQP 连接,并接受以下参数:

以下示例演示如何在发送消息前使用 CBS 进行身份验证。

// ... create TCP client
oClient := TsgcWebSocketClient.Create(nil);
oClient.Specifications.RFC6455 := False;
oClient.Host := 'esegece.servicebus.windows.net';
oClient.Host := 'esegece.servicebus.windows.net';
oClient.Port := 5671;
oClient.TLS := True;
// ... create AMQP1 protocol client
oAMQP1 := TsgcWSClient_AMQP1.Create(nil);
oAMQP1.Specifications.RFC6455 := False;
oAMQP1.AMQPOptions.Authentication.AuthType := amqp1authSASLAnonymous;
oAMQP1.Client := oClient;
// ... connect and publish SAS token through CBS
oClient.Active := True;
// ... wait till the connection is active and send the authentication
oAMQP1.CreateAzureCbsSasToken('cbs', 'esegece', 'queue1',
  'RootManageSharedAccessKey', 'BhJ78+w8kMXhS/eE/nBy0cRzodx9tipbi+ASbAXIaH8=',
  3600, 10000, True);

下一个示例专注于使用 JWT 进行 Microsoft Entra ID(Azure AD)身份验证,演示如何通过客户端凭据流请求 token 并在创建发送或接收消息的链路之前将其发布到 $cbs。 

oClient := TsgcWebSocketClient.Create(nil);
oClient.Specifications.RFC6455 := False;
oClient.Host := 'esegece.servicebus.windows.net';
oClient.Host := 'esegece.servicebus.windows.net';
oClient.Port := 5671;
oClient.TLS := True;
// ... create AMQP1 protocol client
oAMQP1 := TsgcWSClient_AMQP1.Create(nil);
oAMQP1.Specifications.RFC6455 := False;
oAMQP1.AMQPOptions.Authentication.AuthType := amqp1authSASLAnonymous;
oAMQP1.Client := oClient;
// ... connect and publish JWT through CBS
oClient.Active := True;
// ... wait till the connection is active and send the authentication
oAMQP1.CreateAzureCbsJWT('cbs', 'esegece', 'queue1',
  '00000000-0000-0000-0000-000000000000', // Tenant ID
  '11111111-1111-1111-1111-111111111111', // Application ID
  'client-secret', 8080, 3600, 10000, True);