字符串值匹配 - Amazon Simple Notification Service
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

字符串值匹配

您可以通过将字符串值与消息属性值或消息正文属性值进行匹配来筛选消息。在 JSON 策略中,用双引号将字符串值引起来。您可以使用以下字符串操作来匹配消息属性或消息正文。

精确匹配

在策略属性值与一个或多个消息属性值匹配时,会进行精确匹配。

请考虑以下策略属性:

"customer_interests": ["rugby", "tennis"]

它匹配以下消息属性:

"customer_interests": {"Type": "String", "Value": "rugby"}
"customer_interests": {"Type": "String", "Value": "tennis"}

它还匹配以下消息正文:

{ "customer_interests": "rugby" }
{ "customer_interests": "tennis" }

但是,它不匹配以下消息属性:

"customer_interests": {"Type": "String", "Value": "baseball"}

它与以下消息正文也不匹配:

{ "customer_interests": "baseball" }

Anything-but 匹配

当策略属性值包含关键字 anything-but 时,它会匹配 包含任何策略属性值的任何消息属性值或消息正文值。anything-but 可以与 "exists": false 组合。

请考虑以下策略属性:

"customer_interests": [{"anything-but": ["rugby", "tennis"]}]

它匹配以下任一消息属性:

"customer_interests": {"Type": "String", "Value": "baseball"}
"customer_interests": {"Type": "String", "Value": "football"}

它还匹配以下任一消息正文:

{ "customer_interests": "baseball" }
{ "customer_interests": "football" }

此外,它还匹配以下消息属性(因为它包含的值不是 rugbytennis):

"customer_interests": {"Type": "String.Array", "Value": "[\"rugby\", \"baseball\"]"}

它还匹配以下消息正文(因为它包含的值不是 rugbytennis):

{ "customer_interests": ["rugby", "baseball"] }

但是,它不匹配以下消息属性:

"customer_interests": {"Type": "String", "Value": "rugby"}

它与以下消息正文也不匹配:

{ "customer_interests": ["rugby"] }

将前缀与 anything-but 运算符结合使用

要进行字符串匹配,您也可以将前缀与 anything-but 运算符结合使用。例如,以下策略属性拒绝 order- 前缀:

"event":[{"anything-but": {"prefix": "order-"}}]

它匹配以下任一属性:

"event": {"Type": "String", "Value": "data-entry"}
"event": {"Type": "String", "Value": "order_number"}

它还匹配以下任一消息正文:

{ "event": "data-entry" }
{ "event": "order_number" }

但是,它不匹配以下消息属性:

"event": {"Type": "String", "Value": "order-cancelled"}

它与以下消息正文也不匹配:

{ "event": "order-cancelled" }

等于-忽略-大小写匹配

当策略属性包含关键字 equals-ignore-case 时,它将与任何信息属性或正文属性值进行不区分大小写的匹配。

请考虑以下策略属性:

"customer_interests": [{"equals-ignore-case": "tennis"}]

它匹配以下任一消息属性:

"customer_interests": {"Type": "String", "Value": "TENNIS"}
"customer_interests": {"Type": "String", "Value": "Tennis"}

它还匹配以下任一消息正文:

{ "customer_interests": "TENNIS" }
{ "customer_interests": "teNnis" {

IP 地址匹配

您可以使用 cidr 运算符来检查传入消息是否源自特定 IP 地址或子网。

请考虑以下策略属性:

"source_ip":[{"cidr": "10.0.0.0/24"}]

它匹配以下任一消息属性:

"source_ip": {"Type": "String", "Value": "10.0.0.0"}
"source_ip": {"Type": "String", "Value": "10.0.0.255"}

它还匹配以下任一消息正文:

{ "source_ip": "10.0.0.0" }
{ "source_ip": "10.0.0.255" }

但是,它不匹配以下消息属性:

"source_ip": {"Type": "String", "Value": "10.1.1.0"}

它与以下消息正文也不匹配:

{ "source_ip": "10.1.1.0" }

前缀匹配

当策略属性值包含关键字 prefix 时,它匹配以指定字符开头的任何消息属性值或正文属性值。

请考虑以下策略属性:

"customer_interests": [{"prefix": "bas"}]

它匹配以下任一消息属性:

"customer_interests": {"Type": "String", "Value": "baseball"}
"customer_interests": {"Type": "String", "Value": "basketball"}

它还匹配以下任一消息正文:

{ "customer_interests": "baseball" }
{ "customer_interests": "basketball" }

但是,它不匹配以下消息属性:

"customer_interests": {"Type": "String", "Value": "rugby"}

它与以下消息正文也不匹配:

{ "customer_interests": "rugby" }

后缀匹配

当策略属性值包含关键字 suffix 时,它匹配以指定字符结尾的任何消息属性值或正文属性值。

请考虑以下策略属性:

"customer_interests": [{"suffix": "ball"}]

它匹配以下任一消息属性:

"customer_interests": {"Type": "String", "Value": "baseball"}
"customer_interests": {"Type": "String", "Value": "basketball"}

它还匹配以下任一消息正文:

{ "customer_interests": "baseball" }
{ "customer_interests": "basketball" }

但是,它不匹配以下消息属性:

"customer_interests": {"Type": "String", "Value": "rugby"}

它与以下消息正文也不匹配:

{ "customer_interests": "rugby" }