Amazon SNS支持基本的HTTP摘要认证和HTTP基本认证,以帮助保护消息发布的安全性。以下是如何在Amazon SNS中实现这些认证方法的步骤:
import boto3
# Create an SNS client
sns = boto3.client('sns')
# Create a topic
response = sns.create_topic(Name='my-topic')
# Get the topic ARN
topic_arn = response['TopicArn']
# Create a subscription
response = sns.subscribe(TopicArn=topic_arn, Protocol='email', Endpoint='example@example.com')
# Print subscription ARN
print(response['SubscriptionArn'])
2.设置创建的主题的方法是使用HTTP基本认证还是HTTP摘要认证。要设置此设置,请使用以下代码:
import boto3
# Create an SNS client
sns = boto3.client('sns')
# Set security attributes of a topic
sns.set_topic_attributes(
TopicArn='arn:aws:sns:us-west-2:123456789012:my-topic',
AttributeName='HttpEndpoint. AuthenticationType',
AttributeValue='BASIC' # or 'DIGEST'
)
3.在订阅时使用摘要或基本身份验证。要使用基本或摘要身份验证设置订阅,请使用以下代码:
import boto3
# Create an SNS client
sns = boto3.client('sns')
# Create a subscription
sns.subscribe(
TopicArn='arn:aws:sns:us-west-2:123456789012:my-topic',
Protocol='http',
Endpoint='http://example.com/endpoint',
Attributes={
'RawMessageDelivery': 'true',
'DeliveryPolicy': json.dumps({
"http": {
"defaultHealthyRetryPolicy": {"numRetries": 5},
"authentication": {
"type": "Basic", # or 'Digest'
"username": "",
"password": ""
}
}
})
}