

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

# 發佈/訂閱政策範例
<a name="pub-sub-policy"></a>

您使用的政策取決於您的連線方式 AWS IoT Core。您可以使用 MQTT AWS IoT Core 用戶端、HTTP 或 WebSocket 連線至 。當您使用 MQTT 用戶端連接，必須使用 X.509 憑證進行驗證。當您透過 HTTP 或 WebSocket 通訊協定連接時，必須使用 Signature 第 4 版和 Amazon Cognito 進行驗證。

**注意**  
對於已註冊的裝置，建議您使用 `Connect` 動作的[物件政策變數](thing-policy-variables.md)，並將物件附加至用於連線的主體。

**Topics**
+ [在 MQTT 和 AWS IoT Core 政策中使用萬用字元](#pub-sub-policy-cert)
+ [向/從特定主題發佈、訂閱及接收訊息的政策](#pub-sub-specific-topic)
+ [向/從具有特定前綴主題發佈、訂閱及接收訊息的政策](#pub-sub-policy-specific-topic-prefix)
+ [向/從各項裝置特定主題發佈、訂閱及接收訊息的政策](#pub-sub-specific-topic-device)
+ [向/從主題名稱中具有物件屬性的主題發佈、訂閱和接收訊息的政策](#pub-sub-topic-attribute)
+ [拒絕向特定主題名稱的子主題進行發佈的政策](#pub-sub-deny-publish)
+ [拒絕從特定主題名稱的子主題進行接收的政策](#pub-sub-deny-receive)
+ [從使用 MQTT 萬用字元的主題進行訂閱的政策](#pub-sub-topic-wildcard)
+ [適用 HTTP 和 WebSocket 用戶端的政策](#pub-sub-policy-cognito)

## 在 MQTT 和 AWS IoT Core 政策中使用萬用字元
<a name="pub-sub-policy-cert"></a>

MQTT 和 AWS IoT Core 政策具有不同的萬用字元，您應該在仔細考慮後選擇它們。在 MQTT 中，萬用字元 `+`和 `#` 用於 [MQTT 主題篩選條件](https://docs.aws.amazon.com/iot/latest/developerguide/topics.html#topicfilters)，以訂閱多個主題 name. AWS IoT Core policies 使用 `*` 和 `?`做為萬用字元，並遵循 [IAM 政策](https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_grammar.html#policies-grammar-json)的慣例。在政策文件中，`*` 代表任意字元組合，`?` 則代表任何單一字元。在政策文件中，MQTT 萬用字元 `+` 和 `#` 視為沒有特殊含義的字元。若要在政策中的 `resource` 屬性描述多個主題名稱和主題篩選條件，請使用 `*` 和 `?` 萬用字元代替 MQTT 萬用字元。

當您選擇要在政策文件中使用的萬用字元時，請考慮該`*`字元不限於單一主題層級。`+` 字元僅限於 MQTT 主題篩選條件中的單一主題層級。要幫助將萬用字元規範限制為單一 MQTT 主題篩選條件層級，請考慮使用多個 `?` 字元。更多有關在政策資源中使用萬用字元的資訊以及它們比對的更多範例，請參閱[在資源 ARN 中使用萬用字元](https://docs.aws.amazon.com//IAM/latest/UserGuide/reference_policies_elements_resource.html#reference_policies_elements_resource_wildcards)。

下表顯示 MQTT 用戶端的 AWS IoT Core 政策中使用的不同萬用字元。


| 萬用字元 | 是 MQTT 萬用字元 | MQTT 中的範例 | 是 AWS IoT Core 政策萬用字元 | MQTT 用戶端 AWS IoT Core 政策中的範例 | 
| --- | --- | --- | --- | --- | 
| \$1 | 是 | some/\$1 | 否 | N/A | 
| \$1 | 是 | some/\$1/topic | 否 | 不適用 | 
| \$1 | 否 | N/A | 是 | `topicfilter/some/*/topic` `topicfilter/some/sensor*/topic`  | 
| ? | 否 | N/A | 是 |  `topic/some/?????/topic` `topicfilter/some/sensor???/topic`  | 

## 向/從特定主題發佈、訂閱及接收訊息的政策
<a name="pub-sub-specific-topic"></a>

以下範例顯示已註冊和未註冊裝置向/從名為 "some\$1specific\$1topic" 的主題發佈、訂閱和接收訊息的情形。這些範例也突顯 `Publish` 和 `Receive` 使用 "topic" 作為資源，以及 `Subscribe` 使用 "topicfilter" 作為資源。

------
#### [ Registered devices ]

對於在 AWS IoT Core 登錄檔中註冊的裝置，下列政策允許裝置使用與登錄檔中物件名稱相符的 clientId 進行連線。針對名為 "some\$1specific\$1topic" 的主題，它也提供 `Publish`、`Subscribe` 和 `Receive` 的許可。

****  

```
{
	"Version":"2012-10-17",		 	 	 
	"Statement": [
		{
			"Effect": "Allow",
			"Action": [
				"iot:Connect"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:client/${iot:Connection.Thing.ThingName}"
			],
			"Condition": {
				"Bool": {
					"iot:Connection.Thing.IsAttached": "true"
				}
			}
		},
		{
			"Effect": "Allow",
			"Action": [
				"iot:Publish"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:topic/some_specific_topic"
			]
		},
		{
			"Effect": "Allow",
			"Action": [
				"iot:Subscribe"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:topicfilter/some_specific_topic"
			]
		},
		{
			"Effect": "Allow",
			"Action": [
				"iot:Receive"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:topic/some_specific_topic"
			]
		}
	]
}
```

------
#### [ Unregistered devices ]

對於未在 AWS IoT Core 登錄檔中註冊的裝置，下列政策允許裝置使用 clientId1, clientId2 或 clientId3 進行連線。針對名為 "some\$1specific\$1topic" 的主題，它也提供 `Publish`、`Subscribe` 和 `Receive` 的許可。

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iot:Connect"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:client/clientId1",
                "arn:aws:iot:us-east-1:123456789012:client/clientId2",
                "arn:aws:iot:us-east-1:123456789012:client/clientId3"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iot:Publish"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:topic/some_specific_topic"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iot:Subscribe"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:topicfilter/some_specific_topic"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iot:Receive"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:topic/some_specific_topic"
            ]
        }
    ]
}
```

------

## 向/從具有特定前綴主題發佈、訂閱及接收訊息的政策
<a name="pub-sub-policy-specific-topic-prefix"></a>

以下範例顯示已註冊和未註冊裝置向/從具有 "topic\$1prefix" 前綴的主題發佈、訂閱和接收訊息的情形。

**注意**  
請注意，`*`在此範例中使用萬用字元。雖然在單一陳述式中提供多個主題名稱的許可`*`非常有用，但透過為裝置提供比所需更多的權限，可能會導致意外的後果。因此，我們建議您在仔細考慮`*`之後，才使用萬用字元。

------
#### [ Registered devices ]

對於在 AWS IoT Core 登錄檔中註冊的裝置，下列政策允許裝置使用與登錄檔中物件名稱相符的 clientId 進行連線。針對以 "topic\$1prefix" 為前綴的主題，它也提供 `Publish`、`Subscribe` 和 `Receive` 的許可。

****  

```
{
	"Version":"2012-10-17",		 	 	 
	"Statement": [
		{
			"Effect": "Allow",
			"Action": [
				"iot:Connect"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:client/${iot:Connection.Thing.ThingName}"
			],
			"Condition": {
				"Bool": {
					"iot:Connection.Thing.IsAttached": "true"
				}
			}
		},
		{
			"Effect": "Allow",
			"Action": [
				"iot:Publish",
				"iot:Receive"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:topic/topic_prefix*"
			]
		},
		{
			"Effect": "Allow",
			"Action": [
				"iot:Subscribe"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:topicfilter/topic_prefix*"
			]
		}
	]
}
```

------
#### [ Unregistered devices ]

對於未在 AWS IoT Core 登錄檔中註冊的裝置，下列政策允許裝置使用 clientId1, clientId2 或 clientId3 進行連線。它也為以 "topic\$1prefix" 為前綴的主題提供 `Publish`、`Subscribe` 和 `Receive` 的許可。

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iot:Connect"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:client/clientId1",
                "arn:aws:iot:us-east-1:123456789012:client/clientId2",
                "arn:aws:iot:us-east-1:123456789012:client/clientId3"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iot:Publish",
                "iot:Receive"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:topic/topic_prefix*"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iot:Subscribe"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:topicfilter/topic_prefix*"
            ]
        }
    ]
}
```

------

## 向/從各項裝置特定主題發佈、訂閱及接收訊息的政策
<a name="pub-sub-specific-topic-device"></a>

以下範例顯示已註冊和未註冊裝置向/從特定裝置專屬的主題發佈、訂閱和接收訊息的情形。

------
#### [ Registered devices ]

對於在 AWS IoT Core 登錄檔中註冊的裝置，下列政策允許裝置使用與登錄檔中物件名稱相符的 clientId 進行連線。它提供向物件特定主題 (`sensor/device/${iot:Connection.Thing.ThingName}`) 進行發佈的許可，以及從物件特定主題 (`command/device/${iot:Connection.Thing.ThingName}`) 進行訂閱和接收的許可。如果登錄檔中的物件名稱是「thing1」，裝置將能夠發佈到主題「sensor/device/thing1」。裝置也可以訂閱主題「command/device/thing1」並從中接收。

****  

```
{
	"Version":"2012-10-17",		 	 	 
	"Statement": [
		{
			"Effect": "Allow",
			"Action": [
				"iot:Connect"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:client/${iot:Connection.Thing.ThingName}"
			],
			"Condition": {
				"Bool": {
					"iot:Connection.Thing.IsAttached": "true"
				}
			}
		},
		{
			"Effect": "Allow",
			"Action": [
				"iot:Publish"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:topic/sensor/device/${iot:Connection.Thing.ThingName}"
			]
		},
		{
			"Effect": "Allow",
			"Action": [
				"iot:Subscribe"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:topicfilter/command/device/${iot:Connection.Thing.ThingName}"
			]
		},
		{
			"Effect": "Allow",
			"Action": [
				"iot:Receive"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:topic/command/device/${iot:Connection.Thing.ThingName}"
			]
		}
	]
}
```

------
#### [ Unregistered devices ]

對於未在 AWS IoT Core 登錄檔中註冊的裝置，下列政策允許裝置使用 clientId1, clientId2 或 clientId3 進行連線。它提供向用戶端特定主題 (`sensor/device/${iot:ClientId}`) 進行發佈的許可，以及從用戶端特定主題 (`command/device/${iot:ClientId}`) 進行訂閱和接收的許可。如果裝置以 clientId1 連接 clientId，它將能夠發佈到主題 "sensor/device/clientId1"。裝置也可以從主題 訂閱和接收 `device/clientId1/command`。

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iot:Connect"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:client/clientId1",
                "arn:aws:iot:us-east-1:123456789012:client/clientId2",
                "arn:aws:iot:us-east-1:123456789012:client/clientId3"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iot:Publish"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:topic/sensor/device/${iot:Connection.Thing.ThingName}"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iot:Subscribe"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:topicfilter/command/device/${iot:Connection.Thing.ThingName}"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iot:Receive"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:topic/command/device/${iot:Connection.Thing.ThingName}"
            ]
        }
    ]
}
```

------

## 向/從主題名稱中具有物件屬性的主題發佈、訂閱和接收訊息的政策
<a name="pub-sub-topic-attribute"></a>

以下範例顯示已註冊裝置向/從名稱含有物件屬性的主題發佈、訂閱和接收訊息的情形。

**注意**  
物件屬性僅存在於登錄檔中 AWS IoT Core 註冊的裝置。未註冊的裝置沒有對應的範例。

------
#### [ Registered devices ]

對於在 AWS IoT Core 登錄檔中註冊的裝置，下列政策允許裝置使用與登錄檔中物件名稱相符的 clientId 進行連線。它提供向主題 (`sensor/${iot:Connection.Thing.Attributes[version]}`) 進行發佈的許可，以及從主題名稱含有物件屬性的主題 (`command/${iot:Connection.Thing.Attributes[location]}`) 進行訂閱和接收的許可。如果登錄檔中的物件名稱具有 `version=v1`和 `location=Seattle`，裝置將能夠發佈至主題「感應器/v1」，並從主題「命令/西雅圖」訂閱和接收。

****  

```
{
	"Version":"2012-10-17",		 	 	 
	"Statement": [
		{
			"Effect": "Allow",
			"Action": [
				"iot:Connect"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:client/${iot:Connection.Thing.ThingName}"
			],
			"Condition": {
				"Bool": {
					"iot:Connection.Thing.IsAttached": "true"
				}
			}
		},
		{
			"Effect": "Allow",
			"Action": [
				"iot:Publish"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:topic/sensor/${iot:Connection.Thing.Attributes[version]}"
			]
		},
		{
			"Effect": "Allow",
			"Action": [
				"iot:Subscribe"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:topicfilter/command/${iot:Connection.Thing.Attributes[location]}"
			]
		},
		{
			"Effect": "Allow",
			"Action": [
				"iot:Receive"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:topic/command/${iot:Connection.Thing.Attributes[location]}"
			]
		}
	]
}
```

------
#### [ Unregistered devices ]

由於物件屬性僅適用於 AWS IoT Core 在登錄檔中註冊的裝置，因此沒有未註冊物件的對應範例。

------

## 拒絕向特定主題名稱的子主題進行發佈的政策
<a name="pub-sub-deny-publish"></a>

以下範例顯示已註冊和未註冊裝置向特定子主題以外的所有主題發佈訊息的情形。

------
#### [ Registered devices ]

對於在 AWS IoT Core 登錄檔中註冊的裝置，下列政策允許裝置使用與登錄檔中物件名稱相符的 clientId 進行連線。它提供向所有以 "department/" 為前綴的主題進行發佈的許可，但未及於 "department/admins" 子主題。

****  

```
{
	"Version":"2012-10-17",		 	 	 
	"Statement": [
		{
			"Effect": "Allow",
			"Action": [
				"iot:Connect"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:client/${iot:Connection.Thing.ThingName}"
			],
			"Condition": {
				"Bool": {
					"iot:Connection.Thing.IsAttached": "true"
				}
			}
		},
		{
			"Effect": "Allow",
			"Action": [
				"iot:Publish"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:topic/department/*"
			]
		},
		{
			"Effect": "Deny",
			"Action": [
				"iot:Publish"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:topic/department/admins"
			]
		}
	]
}
```

------
#### [ Unregistered devices ]

對於未在 AWS IoT Core 登錄檔中註冊的裝置，下列政策允許裝置使用 clientId1, clientId2 或 clientId3 進行連線。它提供向所有以 "department/" 為前綴的主題進行發佈的許可，但未及於 "department/admins" 子主題。

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iot:Connect"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:client/clientId1",
                "arn:aws:iot:us-east-1:123456789012:client/clientId2",
                "arn:aws:iot:us-east-1:123456789012:client/clientId3"
            ]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iot:Publish"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:topic/department/*"
            ]
        },
        {
            "Effect": "Deny",
            "Action": [
                "iot:Publish"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:topic/department/admins"
            ]
        }
    ]
}
```

------

## 拒絕從特定主題名稱的子主題進行接收的政策
<a name="pub-sub-deny-receive"></a>

以下範例顯示已註冊和未註冊裝置從特定子主題以外所有具特定前綴主題訂閱和接收訊息的情形。

------
#### [ Registered devices ]

對於在 AWS IoT Core 登錄檔中註冊的裝置，下列政策允許裝置使用與登錄檔中物件名稱相符的 clientId 進行連線。此政策允許裝置訂閱任何前綴為 "topic\$1prefix" 的主題。藉由在 `iot:Receive` 的陳述式中使用，`NotResource`我們允許裝置接收所有來自其已訂閱主題的訊息，但前綴為 "topic\$1prefix/restricted" 的主題除外。例如在此政策中，裝置可以訂閱 "topic\$1prefix/topic1" 甚至 "topic\$1prefix/restricted"，但是它們只會接收來自 "opic\$1prefix/topic1" 主題的訊息，且不會接收來自 "topic\$1prefix/restricted" 主題的訊息。

****  

```
{
	"Version":"2012-10-17",		 	 	 
	"Statement": [
		{
			"Effect": "Allow",
			"Action": [
				"iot:Connect"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:client/${iot:Connection.Thing.ThingName}"
			],
			"Condition": {
				"Bool": {
					"iot:Connection.Thing.IsAttached": "true"
				}
			}
		},
		{
			"Effect": "Allow",
			"Action": "iot:Subscribe",
			"Resource": "arn:aws:iot:us-east-1:123456789012:topicfilter/topic_prefix/*"
		},
		{
			"Effect": "Allow",
			"Action": "iot:Receive",
			"NotResource": "arn:aws:iot:us-east-1:123456789012:topic/topic_prefix/restricted/*"
		}
	]
}
```

------
#### [ Unregistered devices ]

對於未在 AWS IoT Core 登錄檔中註冊的裝置，下列政策允許裝置使用 clientId1, clientId2 或 clientId3 進行連線。此政策允許裝置訂閱任何前綴為 "topic\$1prefix" 的主題。藉由在 `iot:Receive` 的陳述式中使用 `NotResource`，我們允許裝置接收所有來自其已訂閱主題的訊息，但前綴為 "topic\$1prefix/restricted" 的主題除外。例如，使用此政策，裝置可以訂閱 "topic\$1prefix/topic1" 甚至 "topic\$1prefix/restricted"。不過，他們只會收到主題 "topic\$1prefix/topic1" 的訊息，而不會收到主題 "topic\$1prefix/restricted" 的訊息。

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iot:Connect"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:client/clientId1",
                "arn:aws:iot:us-east-1:123456789012:client/clientId2",
                "arn:aws:iot:us-east-1:123456789012:client/clientId3"
            ]
        },
        {
            "Effect": "Allow",
            "Action": "iot:Subscribe",
            "Resource": "arn:aws:iot:us-east-1:123456789012:topicfilter/topic_prefix/*"
        },
        {
            "Effect": "Allow",
            "Action": "iot:Receive",
            "NotResource": "arn:aws:iot:us-east-1:123456789012:topic/topic_prefix/restricted/*"
        }
    ]
}
```

------

## 從使用 MQTT 萬用字元的主題進行訂閱的政策
<a name="pub-sub-topic-wildcard"></a>

MQTT 萬用字元 \$1 和 \$1 視為文字字串，但在 AWS IoT Core 政策中使用時不會視為萬用字元。在 MQTT 中，僅在訂閱主題篩選條件時才會將 \$1 和 \$1 視為萬用字元，但在所有其他情況下視為常值字串。我們建議您在仔細考慮後，才使用這些 MQTT 萬用字元做為 AWS IoT Core 政策的一部分。

以下顯示 AWS IoT Core 政策中使用 MQTT 萬用字元註冊和未註冊物件的範例。這些萬用字元會視為常值字串。

------
#### [ Registered devices ]

對於在 AWS IoT Core 登錄檔中註冊的裝置，下列政策允許裝置使用與登錄檔中物件名稱相符的 clientId 進行連線。此政策允許裝置訂閱 "department/\$1/employees" 和 "location/\$1" 主題。由於 \$1 和 \$1 在 AWS IoT Core 政策中被視為文字字串，裝置可以訂閱主題「department/\$1/employees」，但不能訂閱主題「department/engineering/employees」。同樣，裝置可以訂閱 "location/\$1" 主題，但不能訂閱 "location/Seattle" 主題。但是，一旦裝置訂閱了 "department/\$1/employees" 主題，此政策將允許其接收來自 "department/engineering/employees" 主題的訊息。同樣，一旦裝置訂閱了 "location/\$1" 主題，其也將接收來自 "location/Seattle" 主題的訊息。

****  

```
{
	"Version":"2012-10-17",		 	 	 
	"Statement": [
		{
			"Effect": "Allow",
			"Action": [
				"iot:Connect"
			],
			"Resource": [
				"arn:aws:iot:us-east-1:123456789012:client/${iot:Connection.Thing.ThingName}"
			],
			"Condition": {
				"Bool": {
					"iot:Connection.Thing.IsAttached": "true"
				}
			}
		},
		{
			"Effect": "Allow",
			"Action": "iot:Subscribe",
			"Resource": "arn:aws:iot:us-east-1:123456789012:topicfilter/department/+/employees"
		},
		{
			"Effect": "Allow",
			"Action": "iot:Subscribe",
			"Resource": "arn:aws:iot:us-east-1:123456789012:topicfilter/location/#"
		},
		{
			"Effect": "Allow",
			"Action": "iot:Receive",
			"Resource": "arn:aws:iot:us-east-1:123456789012:topic/*"
		}
	]
}
```

------
#### [ Unregistered devices ]

對於未在 AWS IoT Core 登錄檔中註冊的裝置，下列政策允許裝置使用 clientId1, clientId2 或 clientId3 進行連線。此政策允許裝置訂閱 "department/\$1/employees" 和 "location/\$1" 主題。由於 \$1 和 \$1 在 AWS IoT Core 政策中被視為文字字串，裝置可以訂閱主題「department/\$1/employees」，但不能訂閱主題「department/engineering/employees」。同樣，裝置可以訂閱 "location/\$1" 主題，但不能訂閱 "location/Seattle"。但是，一旦裝置訂閱了 "department/\$1/employees" 主題，此政策將允許其接收來自 "department/engineering/employees" 主題的訊息。同樣，一旦裝置訂閱了 "location/\$1" 主題，其也將接收來自 "location/Seattle" 主題的訊息。

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iot:Connect"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:client/clientId1",
                "arn:aws:iot:us-east-1:123456789012:client/clientId2",
                "arn:aws:iot:us-east-1:123456789012:client/clientId3"
            ]
        },
        {
            "Effect": "Allow",
            "Action": "iot:Subscribe",
            "Resource": "arn:aws:iot:us-east-1:123456789012:topicfilter/department/+/employees"
        },
        {
            "Effect": "Allow",
            "Action": "iot:Subscribe",
            "Resource": "arn:aws:iot:us-east-1:123456789012:topicfilter/location/#"
        },
        {
            "Effect": "Allow",
            "Action": "iot:Receive",
            "Resource": "arn:aws:iot:us-east-1:123456789012:topic/*"
        }
    ]
}
```

------

## 適用 HTTP 和 WebSocket 用戶端的政策
<a name="pub-sub-policy-cognito"></a>

當您透過 HTTP 或 WebSocket 通訊協定連接時，必須使用 Signature 第 4 版和 Amazon Cognito 進行驗證。Amazon Cognito 身分可以是已驗證或未驗證的身分。已驗證的身分屬於已由任何支援的身分提供者驗證的使用者。未驗證的身分通常屬於未向身分提供者驗證身分的訪客使用者。Amazon Cognito 提供唯一識別符和 AWS 登入資料，以支援未經驗證的身分。如需詳細資訊，請參閱[使用 Amazon Cognito 身分授權](cog-iot-policies.md)。

對於下列操作， AWS IoT Core 會使用透過 `AttachPolicy` API 連接到 Amazon Cognito 身分 AWS IoT Core 的政策。這會縮小連接到具有已驗證身分之 Amazon Cognito 身分集區的許可範圍。
+ `iot:Connect`
+ `iot:Publish`
+ `iot:Subscribe`
+ `iot:Receive`
+ `iot:GetThingShadow`
+ `iot:UpdateThingShadow`
+ `iot:DeleteThingShadow`

這表示 Amazon Cognito Identity 需要 IAM 角色政策和 AWS IoT Core 政策的許可。您可以透過 `AttachPolicy` API 將 IAM 角色政策連接至集區，並將 AWS IoT Core 政策連接至 Amazon Cognito Identity AWS IoT Core 。

已驗證和未驗證的使用者是不同的身分類型。如果您未將 AWS IoT 政策連接至 Amazon Cognito Identity，則已驗證的使用者在 中的授權會失敗 AWS IoT ，而且無法存取 AWS IoT 資源和動作。

**注意**  
對於其他 AWS IoT Core 操作或未經驗證的身分， AWS IoT Core 不會縮小連接到 Amazon Cognito 身分集區角色的許可範圍。對於已驗證和未驗證兩者的身分而言，這是我們針對連接至 Amazon Cognito 集區角色的政策，所提供的最寬容建議。

**HTTP**

若要允許未驗證 Amazon Cognito 身分透過 HTTP 對 Amazon Cognito 身分特定的主題發佈訊息，請將下列 IAM 政策連接至 Amazon Cognito 身分集區角色：

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iot:Publish"
            ],
            "Resource": [
                "arn:aws:iot:us-east-1:123456789012:topic/${cognito-identity.amazonaws.com:sub}"
            ]
        }
    ]
}
```

若要允許已驗證的使用者，請使用 AWS IoT Core [AttachPolicy](https://docs.aws.amazon.com//iot/latest/apireference/API_AttachPolicy.html) API 將上述政策連接至 Amazon Cognito 身分集區角色和 Amazon Cognito 身分。

**注意**  
授權 Amazon Cognito 身分時， AWS IoT Core 請考慮政策並授予指定的最低權限。只有在兩個政策都允許要求的動作時，才會允許執行動作。如果兩個政策都禁止動作，則該動作為未授權。

**MQTT**

若要允許未驗證 Amazon Cognito 身分透過 WebSocket 對您帳戶中 Amazon Cognito 身分特定的主題發佈 MQTT 訊息，請將下列 IAM 政策連接至 Amazon Cognito 身分集區角色：

****  

```
{
    "Version":"2012-10-17",		 	 	 
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "iot:Publish"
            ],
            "Resource": ["arn:aws:iot:us-east-1:123456789012:topic/${cognito-identity.amazonaws.com:sub}"]
        },
        {
            "Effect": "Allow",
            "Action": [
                "iot:Connect"
            ],
            "Resource": ["arn:aws:iot:us-east-1:123456789012:client/${cognito-identity.amazonaws.com:sub}"]
        }
    ]
}
```

若要允許已驗證的使用者，請使用 AWS IoT Core [AttachPolicy](https://docs.aws.amazon.com//iot/latest/apireference/API_AttachPolicy.html) API 將上述政策連接至 Amazon Cognito 身分集區角色和 Amazon Cognito 身分。

**注意**  
授權 Amazon Cognito 身分時， AWS IoT Core 同時考慮 和 會授予指定的最低權限。只有在兩個政策都允許要求的動作時，才會允許執行動作。如果兩個政策都禁止動作，則該動作為未授權。