问题描述: AWS CloudFront签名的Cookie在使用备用域名上无效。
解决方法: AWS CloudFront签名的Cookie在使用备用域名上无效的原因是,CloudFront的签名是基于特定的域名进行的,如果使用了备用域名,签名就会失效。
要解决这个问题,可以通过在CloudFront配置中启用“CNAME签名”来实现签名Cookie在备用域名上的有效性。下面是一个代码示例:
import boto3
cloudfront = boto3.client('cloudfront')
response = cloudfront.update_distribution(
DistributionConfig={
'Id': 'your-distribution-id',
'DefaultCacheBehavior': {
'ForwardedValues': {
'QueryString': False,
'Cookies': {
'Forward': 'all',
'WhitelistedNames': {
'Quantity': 1,
'Items': ['your-cookie-name']
}
}
},
'ViewerProtocolPolicy': 'redirect-to-https',
'MinTTL': 0,
'TargetOriginId': 'your-target-origin-id',
'ViewerCertificate': {
'CloudFrontDefaultCertificate': True
},
'AllowedMethods': {
'Quantity': 2,
'Items': ['GET', 'HEAD'],
'CachedMethods': {
'Quantity': 2,
'Items': ['GET', 'HEAD']
}
},
'TrustedSigners': {
'Enabled': False,
'Quantity': 0
},
'ForwardedValues': {
'Headers': {
'Quantity': 0
},
'Cookies': {
'Forward': 'none'
},
'QueryString': False
},
'MaxTTL': 31536000,
'Compress': False,
'DefaultTTL': 86400,
'LambdaFunctionAssociations': {
'Quantity': 0
},
'PathPattern': '*',
'SmoothStreaming': False
},
'Enabled': True,
'CallerReference': 'your-caller-reference',
'Origins': {
'Quantity': 1,
'Items': [
{
'Id': 'your-target-origin-id',
'DomainName': 'your-target-domain',
'OriginPath': '',
'CustomHeaders': {
'Quantity': 0
},
'CustomOriginConfig': {
'HTTPPort': 80,
'HTTPSPort': 443,
'OriginProtocolPolicy': 'match-viewer',
'OriginSslProtocols': {
'Quantity': 3,
'Items': ['TLSv1', 'TLSv1.1', 'TLSv1.2']
}
}
}
]
},
'Aliases': {
'Quantity': 1,
'Items': ['your-cname']
},
'CustomErrorResponses': {
'Quantity': 0
},
'Comment': '',
'Logging': {
'Enabled': False,
'IncludeCookies': False,
'Bucket': '',
'Prefix': ''
},
'PriceClass': 'PriceClass_All',
'CustomOriginConfig': {
'HTTPPort': 80,
'HTTPSPort': 443,
'OriginProtocolPolicy': 'http-only',
'OriginSslProtocols': {
'Quantity': 3,
'Items': ['TLSv1', 'TLSv1.1', 'TLSv1.2']
}
},
'Restrictions': {
'GeoRestriction': {
'RestrictionType': 'none',
'Quantity': 0
}
},
'WebACLId': '',
'HttpVersion': 'http2',
'IsIPV6Enabled': True
}
)
通过AWS管理控制台进行配置:
这些配置将