本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 Amazon MQ for RabbitMQ 的 OAuth 2.0 身分驗證和授權
本教學說明如何使用 Amazon Cognito 做為 OAuth 2.0 供應商,為您的 Amazon MQ for RabbitMQ 代理程式設定 OAuth 2.0 身分驗證。
注意
Amazon Cognito 不適用於中國 (北京) 和中國 (寧夏)。
重要
本教學課程專屬於 Amazon Cognito,但您可以使用其他身分提供者 (IdPs)。如需詳細資訊,請參閱 OAuth 2.0 身分驗證範例。
在此頁面
設定 OAuth 2.0 身分驗證的先決條件
您可以部署 AWS CDK 堆疊、Amazon Cognito RabbitMQ OAuth 2 的 Amazon Cognito 堆疊外掛程式,藉此設定本教學課程中所需的 Amazon Cognito
設定 Amazon Cognito 的先決條件
-
透過建立使用者集區來設定 Amazon Cognito 端點。若要這樣做,請參閱 Amazon Cognito 中名為如何使用 OAuth 2.0 的部落格:了解不同的 OAuth 2.0 授予
。 -
在使用者集區
rabbitmq
中建立名為 的資源伺服器,並定義下列範圍:read:all
、configure:all
、write:all
和tag:administrator
。這些範圍將與 RabbitMQ 許可相關聯。如需建立資源伺服器的相關資訊,請參閱《Amazon Cognito 開發人員指南》中的為您的使用者集區 (AWS Management Console) 定義資源伺服器。
-
建立下列應用程式用戶端:
-
類型為 之使用者集區的應用程式用戶端
Machine-to-Machine application
。這是機密用戶端,其用戶端秘密將用於 RabbitMQ AMQP 用戶端。如需應用程式用戶端和建立用戶端的詳細資訊,請參閱應用程式用戶端類型和建立應用程式用戶端。 -
類型為 之使用者集區的應用程式用戶端
Single-page application
。這是公有用戶端,將用於登入 RabbitMQ 管理主控台的使用者。您必須更新此應用程式用戶端,以包含您將在下列程序中建立的 Amazon MQ for RabbitMQ 代理程式端點,做為允許的回呼 URL。如需詳細資訊,請參閱使用 Amazon Cognito 主控台設定受管登入。
-
設定 Amazon MQ 的先決條件
-
執行 bash 指令碼的工作 Docker
安裝,以驗證 OAuth 2.0 設定是否成功。 -
AWS CLI 版本 >=
2.28.23
以在代理程式建立期間選擇性新增使用者名稱和密碼。
使用 設定 Amazon Cognito 的 OAuth 2.0 身分驗證 AWS CLI
下列程序說明如何使用 Amazon Cognito 做為 IdP,為您的 Amazon MQ for RabbitMQ 代理程式設定 OAuth 2.0 身分驗證。 Amazon Cognito IdP 此程序使用 AWS CLI 來建立和設定必要的資源。
在下列程序中,請務必將 configurationID 和 Revision、<c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca>
和 <2>
等預留位置值取代為實際值。
-
使用 create-configuration AWS CLI 命令建立新的組態,如下列範例所示。
aws mq create-configuration \ --name "
rabbitmq-oauth2-config
" \ --engine-type "RABBITMQ" \ --engine-version "3.13"此命令會傳回類似下列範例的回應。
{ "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-oauth2-config on RabbitMQ 3.13", "Revision": 1 }, "Name": "rabbitmq-oauth2-config" } -
建立名為 的組態檔案
rabbitmq.conf
,以使用 OAuth 2.0 做為身分驗證和授權方法,如下列範例所示。auth_backends.1 = oauth2 # FIXME: Update this value with the token signing key URL of your Amazon Cognito user pool. # If you used the AWS CDK stack to deploy Amazon Cognito, this is one of the stack outputs. auth_oauth2.jwks_url =
${RabbitMqOAuth2TestStack.JwksUri}
auth_oauth2.resource_server_id = rabbitmq # Amazon Cognito does not include an audience field in access tokens auth_oauth2.verify_aud = false # Amazon Cognito does not allow * in its custom scopes. Use aliases to translate between Amazon Cognito and RabbitMQ. auth_oauth2.scope_prefix = rabbitmq/ auth_oauth2.scope_aliases.1.alias = rabbitmq/read:all auth_oauth2.scope_aliases.1.scope = rabbitmq/read:*/* auth_oauth2.scope_aliases.2.alias = rabbitmq/write:all auth_oauth2.scope_aliases.2.scope = rabbitmq/write:*/* auth_oauth2.scope_aliases.3.alias = rabbitmq/configure:all auth_oauth2.scope_aliases.3.scope = rabbitmq/configure:*/* # Allow OAuth 2.0 login for RabbitMQ management console management.oauth_enabled = true # FIXME: Update this value with the client ID of your public application client management.oauth_client_id =${RabbitMqOAuth2TestStack.ManagementConsoleAppClientId}
# FIXME: Update this value with the base JWKS URI (without /.well-known/jwks.json) auth_oauth2.issuer =${RabbitMqOAuth2TestStack.Issuer}
management.oauth_scopes = rabbitmq/tag:administrator此組態使用範圍別名
,將 Amazon Cognito 中定義的範圍映射至 RabbitMQ 相容範圍。 -
使用 update-configuration AWS CLI 命令更新組態,如下列範例所示。在此命令中,新增您在此程序步驟 1 的回應中收到的組態 ID。例如
c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca
。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-b600ac8e-8183-4f74-a713-983e59f30e3d", "Created": "2025-07-17T16:57:04.520931+00:00", "Id": "c-b600ac8e-8183-4f74-a713-983e59f30e3d", "LatestRevision": { "Created": "2025-07-17T16:57:39.172000+00:00", "Revision": 2 }, "Name": "rabbitmq-oauth2-config", "Warnings": [] } -
使用您在此程序的步驟 2 中建立的 OAuth 2.0 組態來建立代理程式。若要這樣做,請使用 create-broker AWS CLI 命令,如下列範例所示。在此命令中,分別提供您在步驟 1 和 2 回應中取得的組態 ID 和修訂編號。例如,
c-fa3390a5-7e01-4559-ae0c-eb15b38b22ca
和2
。aws mq create-broker \ --broker-name "rabbitmq-oauth2-broker" \ --engine-type "RABBITMQ" \ --engine-version "3.13" \ --host-instance-type "mq.m7g.large" \ --deployment-mode "CLUSTER_MULTI_AZ" \ --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-oauth2-broker:b-2a1b5133-a10c-49d2-879b-8c176c34cf73", "BrokerId": "b-2a1b5133-a10c-49d2-879b-8c176c34cf73" } -
使用 describe-broker AWS CLI 命令,確認代理程式的狀態從 轉換為
CREATION_IN_PROGRESS
RUNNING
,如下列範例所示。在此命令中,提供您在上一個步驟的結果中取得的代理程式 ID 例如,b-2a1b5133-a10c-49d2-879b-8c176c34cf73
。aws mq describe-broker \ --broker-id "
<b-2a1b5133-a10c-49d2-879b-8c176c34cf73>
"此命令會傳回類似下列範例的回應。下列回應是
describe-broker
命令傳回的完整輸出的縮寫版本。此回應顯示代理程式狀態和用於保護代理程式的身分驗證策略。在此情況下,config_managed
身分驗證策略表示代理程式使用 OAuth 2 身分驗證方法。{ "AuthenticationStrategy": "config_managed", ..., "BrokerState": "RUNNING", ... }
若要使用 OAuth2 登入 RabbitMQ 管理主控台,代理程式端點需要在對應的 Amazon Cognito 應用程式用戶端中新增為有效的回呼 URL。如需詳細資訊,請參閱 Amazon Cognito CDK 堆疊
範例設定中的步驟 5。 -
使用以下
perf-test.sh
指令碼驗證 OAuth 2.0 身分驗證和授權。使用此 bash 指令碼來測試 Amazon MQ for RabbitMQ 代理程式的連線能力。此指令碼會從 Amazon Cognito 取得權杖,並驗證連線是否已正確設定。如果設定成功,您會看到代理程式發佈並取用訊息。
如果收到
ACCESS_REFUSED
錯誤,您可以使用代理程式的 CloudWatch 日誌對組態設定進行疑難排解。您可以在 Amazon MQ 主控台中找到代理程式的 CloudWatch 日誌群組連結。在此指令碼中,您需要提供下列值:
-
CLIENT_ID
和CLIENT_SECRET
:您可以在 Amazon Cognito 主控台的應用程式用戶端頁面上找到這些值。 -
Cognito 網域:您可以在 Amazon Cognito 主控台中找到此項目。在品牌下,選擇網域。在網域頁面上,您可以在資源伺服器區段下找到此值。
-
Amazon MQ 代理程式端點:您可以在 Amazon MQ 主控台代理程式詳細資訊頁面上的連線下找到此值。
#! /bin/bash set -e # Client information ## FIXME: Update this value with the client ID and secret of your confidential application client CLIENT_ID=
${RabbitMqOAuth2TestStack.AmqpAppClientId}
CLIENT_SECRET=${RabbitMqOAuth2TestStack.AmqpAppClientSecret}
# FIXME: Update this value with the domain of your Amazon Cognito user pool RESPONSE=$(curl -X POST${RabbitMqOAuth2TestStack.TokenEndpoint}
\ -H "Content-Type: application/x-www-form-urlencoded" \ -d "grant_type=client_credentials&client_id=${CLIENT_ID}
&client_secret=${CLIENT_SECRET}
&scope=rabbitmq/configure:all rabbitmq/read:all rabbitmq/tag:administrator rabbitmq/write:all") # Extract the access_token from the response. # This token will be passed in the password field when connecting to the broker. # Note that the username is left blank, the field is ignored by the plugin. BROKER_PASSWORD=$(echo ${RESPONSE} | jq -r '.access_token') # FIXME: Update this value with the endpoint of your broker. For example,b-89424106-7e0e-4abe-8e98-8de0dada7630.mq.us-east-1.on.aws
. BROKER_DNS=<broker_dns>
CONNECTION_STRING=amqps://:${BROKER_PASSWORD}@${BROKER_DNS}:5671 # Produce/consume messages using the above connection string QUEUES_COUNT=1 PRODUCERS_COUNT=1 CONSUMERS_COUNT=1 PRODUCER_RATE=1 docker run -it --rm --ulimit nofile=40960:40960 pivotalrabbitmq/perf-test:latest \ --queue-pattern 'test-queue-%d' --queue-pattern-from 1 --queue-pattern-to $QUEUES_COUNT \ --producers $PRODUCERS_COUNT --consumers $CONSUMERS_COUNT \ --id "test${QUEUES_COUNT}q${PRODUCERS_COUNT}p${CONSUMERS_COUNT}c${PRODUCER_RATE}r" \ --uri ${CONNECTION_STRING} \ --flag persistent --rate $PRODUCER_RATE -
使用 Amazon Cognito 設定 OAuth 2.0 和簡易身分驗證
當您使用 OAuth 2.0 身分驗證建立代理程式時,您可以指定下列其中一種身分驗證方法:
-
僅限 OAuth 2.0:若要使用此方法,請勿在建立代理程式時提供使用者名稱和密碼。先前的程序說明如何僅使用 OAuth 2.0 身分驗證方法。
-
OAuth 2.0 和簡單身分驗證:若要使用此方法,請在建立代理程式時提供使用者名稱和密碼。此外,請將
auth_backends.2 = internal
新增至您的代理程式組態,如下列程序所示。
在下列程序中,請務必以其實際值取代預留位置值,例如 <ConfigurationId>
和 <Revision>
。
-
若要使用這兩種身分驗證方法,請建立您的代理程式組態,如下列範例所示。
auth_backends.1 = oauth2 auth_backends.2 = internal # FIXME: Update this value with the token signing key URL of your Amazon Cognito user pool auth_oauth2.jwks_url =
${RabbitMqOAuth2TestStack.JwksUri}
auth_oauth2.resource_server_id = rabbitmq auth_oauth2.verify_aud = false auth_oauth2.scope_prefix = rabbitmq/ auth_oauth2.scope_aliases.1.alias = rabbitmq/read:all auth_oauth2.scope_aliases.1.scope = rabbitmq/read:*/* auth_oauth2.scope_aliases.2.alias = rabbitmq/write:all auth_oauth2.scope_aliases.2.scope = rabbitmq/write:*/* auth_oauth2.scope_aliases.3.alias = rabbitmq/configure:all auth_oauth2.scope_aliases.3.scope = rabbitmq/configure:*/*此組態使用範圍別名
,將 Amazon Cognito 中定義的範圍映射至 RabbitMQ 相容範圍。 -
建立同時使用身分驗證方法的代理程式,如下列範例所示。
aws mq create-broker \ --broker-name "rabbitmq-oauth2-broker-with-internal-user" \ --engine-type "RABBITMQ" \ --engine-version "3.13" \ --host-instance-type "mq.m7g.large" \ --deployment-mode "CLUSTER_MULTI_AZ" \ --logs '{"General": true}' \ --publicly-accessible \ --configuration '{"Id": "
<ConfigurationId>
","Revision":<Revision>
}' \ --users '[{"Username":"<myUser>
","Password":"<myPassword11>
"}]' -
驗證代理程式狀態和設定身分驗證方法的組態是否成功,如使用 Amazon Cognito 設定 OAuth 2.0 身分驗證程序的步驟 5 和 6 所述。