

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 对适用于 RabbitMQ 的亚马逊 MQ 使用 HTTP 身份验证和授权
<a name="rabbitmq-http-tutorial"></a>

本教程介绍如何使用外部 HTTP 服务器为 Amazon MQ for RabbitMQ 代理配置 HTTP 身份验证和授权。

**注意**  
HTTP 身份验证插件仅适用于适用于 RabbitMQ 版本 4 及更高版本的亚马逊 MQ。

**Topics**
+ [配置 HTTP 身份验证和授权的先决条件](#rabbitmq-http-tutorial-prerequisites)
+ [使用 CLI 在 RabbitMQ 中配置 HTTP 身份验证 AWS](#rabbitmq-http-tutorial-configure-cli)

## 配置 HTTP 身份验证和授权的先决条件
<a name="rabbitmq-http-tutorial-prerequisites"></a>

您可以通过部署[适用于 RabbitMQ 的 Amazon MQ 的AWS CDK 堆栈来设置本教程中所需的 AWS 资源 HTTP 身份](https://github.com/aws-samples/amazon-mq-samples/blob/main/rabbitmq-samples/rabbitmq-http-sample/)验证集成。

此 CDK 堆栈会自动创建所有必要的 AWS 资源，包括 HTTP 身份验证服务器、证书和 IAM 角色。有关堆栈创建的资源的完整列表，请参阅软件包自述文件。

如果您是手动设置资源而不是使用 CDK 堆栈，请确保在您的 Amazon MQ 上为 RabbitMQ 代理配置 HTTP 身份验证之前，请确保您有同等的基础架构。

### 设置 Amazon MQ 的先决条件
<a name="rabbitmq-http-tutorial-prerequisite-cli"></a>

AWS CLI 版本 >= 2.28.23，使得在创建代理期间添加用户名和密码成为可选的。

## 使用 CLI 在 RabbitMQ 中配置 HTTP 身份验证 AWS
<a name="rabbitmq-http-tutorial-configure-cli"></a>

此过程使用 AWS CLI 来创建和配置必要的资源。在以下过程中，请确保将占位符值替换为其实际值。

1. 使用 `create-configuration` AWS CLI 命令创建新配置，如以下示例所示。

   ```
   aws mq create-configuration \
     --name "rabbitmq-http-config" \
     --engine-type "RABBITMQ" \
     --engine-version "4.2"
   ```

   此命令返回类似于以下示例的响应。

   ```
   {
       "Arn": "arn:aws:mq:us-west-2:123456789012:configuration:c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca",
       "AuthenticationStrategy": "simple",
       "Created": "2025-07-17T16:03:01.759943+00:00",
       "Id": "c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca",
       "LatestRevision": {
           "Created": "2025-07-17T16:03:01.759000+00:00",
           "Description": "Auto-generated default for rabbitmq-http-config on RabbitMQ 4.2",
           "Revision": 1
       },
       "Name": "rabbitmq-http-config"
   }
   ```

1. 创建一个名为的配置文件`rabbitmq.conf`以使用 HTTP 作为身份验证和授权方法，如以下示例所示。将模板中的所有占位符值（标有`${...}`）替换为已部署的 AWS CDK 先决条件堆栈输出或等效基础架构中的实际值。

   ```
   auth_backends.1 = cache
   auth_backends.2 = http
   auth_cache.cached_backend = http
   
   # HTTP authentication settings
   # For more information, see https://github.com/rabbitmq/rabbitmq-auth-backend-http
   
   # FIXME: Replace the ${...} placeholders with actual values
   # from your deployed prerequisite CDK stack outputs.
   auth_http.http_method = post
   auth_http.user_path = ${HttpServerUserPath}
   auth_http.vhost_path = ${HttpServerVhostPath}
   auth_http.resource_path = ${HttpServerResourcePath}
   auth_http.topic_path = ${HttpServerTopicPath}
   
   # TLS/HTTPS configuration
   auth_http.ssl_options.verify = verify_peer
   auth_http.ssl_options.sni = test.amazonaws.com
   
   # AWS integration for secure credential retrieval
   # For more information, see https://github.com/amazon-mq/rabbitmq-aws
   
   # Replace the ${...} placeholders with actual ARN values
   # from your deployed prerequisite CDK stack outputs.
   aws.arns.assume_role_arn = ${AmazonMqAssumeRoleArn}
   aws.arns.auth_http.ssl_options.cacertfile = ${CaCertArn}
   ```

1. 使用 `update-configuration` AWS CLI 命令更新配置。使用步骤 3 中的配置 ID。

   ```
   aws mq update-configuration \
     --configuration-id "<c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca>" \
     --data "$(cat rabbitmq.conf | base64 --wrap=0)"
   ```

   此命令返回类似于以下示例的响应。

   ```
   {
       "Arn": "arn:aws:mq:us-west-2:123456789012:configuration:c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca",
       "Created": "2025-07-17T16:57:04.520931+00:00",
       "Id": "c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca",
       "LatestRevision": {
           "Created": "2025-07-17T16:57:39.172000+00:00",
           "Revision": 2
       },
       "Name": "rabbitmq-http-config",
       "Warnings": []
   }
   ```

1. 使用 HTTP 配置创建代理。使用前面步骤中的配置 ID 和修订版号。

   ```
   aws mq create-broker \
     --broker-name "rabbitmq-http-test-1" \
     --engine-type "RABBITMQ" \
     --engine-version "4.2" \
     --host-instance-type "mq.m7g.large" \
     --deployment-mode "SINGLE_INSTANCE" \
     --logs '{"General": true}' \
     --publicly-accessible \
     --configuration '{"Id": "<c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca>","Revision": <2>}'
   ```

   此命令返回类似于以下示例的响应。

   ```
   {
       "BrokerArn": "arn:aws:mq:us-west-2:123456789012:broker:rabbitmq-http-test-1:b-2a1b5133-a10c-49d2-879b-8c176c34cf73",
       "BrokerId": "b-2a1b5133-a10c-49d2-879b-8c176c34cf73"
   }
   ```

1. 使用 `describe-broker` AWS CLI 命令验证代理的状态是否从`CREATION_IN_PROGRESS`转换为。`RUNNING`

   ```
   aws mq describe-broker \
     --broker-id "<b-2a1b5133-a10c-49d2-879b-8c176c34cf73>"
   ```

   此命令返回类似于以下示例的响应。`config_managed`身份验证策略表明代理使用 HTTP 身份验证方法。

   ```
   {
       "AuthenticationStrategy": "config_managed",
       ...,
       "BrokerState": "RUNNING",
       ...
   }
   ```

1. 使用先决条件 CDK 堆栈创建的测试用户之一验证 RabbitMQ 访问权限

   ```
   # FIXME: Replace ${RabbitMqHttpAuthElbStack.ConsoleUserPasswordArn} with the actual ARN from your deployed prerequisite CDK stack outputs
   CONSOLE_PASSWORD=$(aws secretsmanager get-secret-value \
     --secret-id ${RabbitMqHttpAuthElbStack.ConsoleUserPasswordArn} \
     --query 'SecretString' --output text)
   
   # FIXME: Replace BrokerConsoleURL with the actual ConsoleURL retrieved by
   # calling describe-broker for the broker created above
   # Call management API /api/overview (should succeed)
   curl -u RabbitMqConsoleUser:$CONSOLE_PASSWORD \
     https://${BrokerConsoleURL}/api/overview
   
   # Try to create a vhost (should fail - console user only has management permissions)
   curl -u RabbitMqConsoleUser:$CONSOLE_PASSWORD \
     -X PUT https://${BrokerConsoleURL}/api/vhosts/test-vhost \
     -H "Content-Type: application/json" \
     -d '{}'
   ```