

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 使用 Amazon MQ for RabbitMQ 的 HTTP 身分驗證和授權
<a name="rabbitmq-http-tutorial"></a>

本教學說明如何使用外部 HTTP 伺服器設定 Amazon MQ for RabbitMQ 代理程式的 HTTP 身分驗證和授權。

**注意**  
HTTP 身分驗證外掛程式僅適用於 Amazon MQ for RabbitMQ 第 4 版及更新版本。

**Topics**
+ [設定 HTTP 身分驗證和授權的先決條件](#rabbitmq-http-tutorial-prerequisites)
+ [使用 CLI 在 RabbitMQ 中設定 HTTP AWS 身分驗證](#rabbitmq-http-tutorial-configure-cli)

## 設定 HTTP 身分驗證和授權的先決條件
<a name="rabbitmq-http-tutorial-prerequisites"></a>

您可以部署 [AWS Amazon MQ for RabbitMQ HTTP 身分驗證整合的 CDK 堆疊](https://github.com/aws-samples/amazon-mq-samples/blob/main/rabbitmq-samples/rabbitmq-http-sample/)，藉此設定本教學課程中所需的 AWS 資源。

此 CDK 堆疊會自動建立所有必要 AWS 的資源，包括 HTTP 身分驗證伺服器、憑證和 IAM 角色。如需堆疊所建立資源的完整清單，請參閱套件 README。

如果您要手動設定資源，而不是使用 CDK 堆疊，請確保在 Amazon MQ for 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 '{}'
   ```