

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

# 在 Amazon SQS 队列中使用服务器端加密
<a name="sqs-java-configure-sse"></a>

使用 适用于 Java 的 AWS SDK 将服务器端加密 (SSE) 添加到 Amazon SQS 队列。每个队列都使用 AWS Key Management Service (AWS KMS) KMS 密钥生成数据加密密钥。此示例设置 AWS 托管 KMS 密钥用于 Amazon SQS。

有关使用 SSE 和 KMS 密钥角色的更多信息，请参阅[Amazon SQS 中的静态加密](sqs-server-side-encryption.md)。

## 向现有队列添加 SSE
<a name="sqs-java-configure-sse-existing-queue"></a>

要为现有队列启用服务器端加密，请使用 `[SetQueueAttributes](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SetQueueAttributes.html)` 方法设置 `KmsMasterKeyId` 属性。

以下代码示例将设置 AWS KMS key 为 Amazon SQS 的 AWS 托管 KMS 密钥。该示例还会将 [AWS KMS key 重用周期](sqs-server-side-encryption.md#sqs-sse-key-terms)设置为 140 秒。

 在运行示例代码之前，请确保已设置 AWS 凭据。有关更多信息，请参阅《*AWS SDK for Java 2.x 开发人员指南》中的设置开发 AWS *[凭证和区域](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html#setup-credentials)。

```
    public static void addEncryption(String queueName, String kmsMasterKeyAlias) {
        SqsClient sqsClient = SqsClient.create();

        GetQueueUrlRequest urlRequest = GetQueueUrlRequest.builder()
                .queueName(queueName)
                .build();

        GetQueueUrlResponse getQueueUrlResponse;
        try {
            getQueueUrlResponse = sqsClient.getQueueUrl(urlRequest);
        } catch (QueueDoesNotExistException e) {
            LOGGER.error(e.getMessage(), e);
            throw new RuntimeException(e);
        }
        String queueUrl = getQueueUrlResponse.queueUrl();


        Map<QueueAttributeName, String> attributes = Map.of(
                QueueAttributeName.KMS_MASTER_KEY_ID, kmsMasterKeyAlias,
                QueueAttributeName.KMS_DATA_KEY_REUSE_PERIOD_SECONDS, "140" // Set the data key reuse period to 140 seconds.
        );                                                                  // This is how long SQS can reuse the data key before requesting a new one from KMS.

        SetQueueAttributesRequest attRequest = SetQueueAttributesRequest.builder()
                .queueUrl(queueUrl)
                .attributes(attributes)
                .build();
        try {
            sqsClient.setQueueAttributes(attRequest);
            LOGGER.info("The attributes have been applied to {}", queueName);
        } catch (InvalidAttributeNameException | InvalidAttributeValueException e) {
            LOGGER.error(e.getMessage(), e);
            throw new RuntimeException(e);
        } finally {
            sqsClient.close();
        }
    }
```

## 为队列禁用 SSE
<a name="sqs-java-disable-sse"></a>

要为现有队列禁用服务器端加密，请使用 `SetQueueAttributes` 方法将 `KmsMasterKeyId` 属性设置为空字符串。

**重要**  
`null` 对于 `KmsMasterKeyId` 是无效值。

## 使用 SSE 创建队列
<a name="sqs-java-configure-sse-create-queue"></a>

要在创建队列时启用 SSE，请将 `KmsMasterKeyId` 属性添加到 `[CreateQueue](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_CreateQueue.html)` API 方法中。

以下示例将在启用了 SSE 的情况下创建新队列。队列将 AWS 托管 KMS 密钥用于 Amazon SQS。该示例还会将 [AWS KMS key 重用周期](sqs-server-side-encryption.md#sqs-sse-key-terms)设置为 160 秒。

 在运行示例代码之前，请确保已设置 AWS 凭据。有关更多信息，请参阅《*AWS SDK for Java 2.x 开发人员指南》中的设置开发 AWS *[凭证和区域](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html#setup-credentials)。

```
// Create an SqsClient for the specified Region.
SqsClient sqsClient = SqsClient.builder().region(Region.US_WEST_1).build();

// Create a hashmap for the attributes. Add the key alias and reuse period to the hashmap.
HashMap<QueueAttributeName, String> attributes = new HashMap<QueueAttributeName, String>();
final String kmsMasterKeyAlias = "alias/aws/sqs";  // the alias of the AWS managed KMS key for Amazon SQS.
attributes.put(QueueAttributeName.KMS_MASTER_KEY_ID, kmsMasterKeyAlias);
attributes.put(QueueAttributeName.KMS_DATA_KEY_REUSE_PERIOD_SECONDS, "140");				

// Add the attributes to the CreateQueueRequest.
CreateQueueRequest createQueueRequest =
                CreateQueueRequest.builder()
                        .queueName(queueName)
                        .attributes(attributes)
                        .build();
sqsClient.createQueue(createQueueRequest);
```

## 检索 SSE 属性
<a name="sqs-java-get-sse-attributes"></a>

有关检索队列属性的信息，请参阅 *Amazon Simple Queue Service API 参考*中的[示例](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_GetQueueAttributes.html#API_GetQueueAttributes_Examples)。

要检索特定队列的 KMS 密钥 ID 或数据密钥重用周期，请运行 `[GetQueueAttributes](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_GetQueueAttributes.html)` 方法并检索 `KmsMasterKeyId` 和 `KmsDataKeyReusePeriodSeconds` 值。