

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

# Menggunakan enkripsi sisi server dengan antrian Amazon SQS
<a name="sqs-java-configure-sse"></a>

Gunakan AWS SDK untuk Java untuk menambahkan enkripsi sisi server (SSE) ke antrean Amazon SQS. Setiap antrian menggunakan kunci AWS Key Management Service (AWS KMS) KMS untuk menghasilkan kunci enkripsi data. Contoh ini menggunakan kunci KMS AWS terkelola untuk Amazon SQS.

Untuk informasi selengkapnya tentang penggunaan SSE dan peran kunci KMS, lihat. [Enkripsi saat istirahat di Amazon SQS](sqs-server-side-encryption.md) 

## Menambahkan SSE ke antrian yang ada
<a name="sqs-java-configure-sse-existing-queue"></a>

Untuk mengaktifkan enkripsi sisi server untuk antrian yang ada, gunakan `[SetQueueAttributes](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SetQueueAttributes.html)` metode untuk mengatur atribut. `KmsMasterKeyId`

Contoh kode berikut menetapkan AWS KMS key sebagai kunci KMS AWS terkelola untuk Amazon SQS. Contoh ini juga mengatur [periode AWS KMS key penggunaan kembali](sqs-server-side-encryption.md#sqs-sse-key-terms) menjadi 140 detik.

 Sebelum Anda menjalankan kode contoh, pastikan Anda telah menetapkan AWS kredensialnya. Untuk informasi selengkapnya, lihat [Menyiapkan AWS Kredensial dan Wilayah untuk Pengembangan](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html#setup-credentials) di Panduan *AWS SDK for Java 2.x Pengembang*. 

```
    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();
        }
    }
```

## Menonaktifkan SSE untuk antrian
<a name="sqs-java-disable-sse"></a>

Untuk menonaktifkan enkripsi sisi server untuk antrian yang ada, setel `KmsMasterKeyId` atribut ke string kosong menggunakan metode. `SetQueueAttributes`

**penting**  
`null` bukanlah nilai yang valid untuk `KmsMasterKeyId`.

## Membuat antrian dengan SSE
<a name="sqs-java-configure-sse-create-queue"></a>

Untuk mengaktifkan SSE saat Anda membuat antrian, tambahkan `KmsMasterKeyId` atribut ke metode `[CreateQueue](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_CreateQueue.html)` API.

Contoh berikut membuat antrian baru dengan SSE diaktifkan. Antrian menggunakan kunci KMS AWS terkelola untuk Amazon SQS. Contoh ini juga mengatur [periode AWS KMS key penggunaan kembali](sqs-server-side-encryption.md#sqs-sse-key-terms) menjadi 160 detik.

 Sebelum Anda menjalankan kode contoh, pastikan Anda telah menetapkan AWS kredensialnya. Untuk informasi selengkapnya, lihat [Menyiapkan AWS Kredensial dan Wilayah untuk Pengembangan](https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/setup.html#setup-credentials) di Panduan *AWS SDK for Java 2.x Pengembang*. 

```
// 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);
```

## Mengambil atribut SSE
<a name="sqs-java-get-sse-attributes"></a>

Untuk informasi tentang mengambil atribut antrian, lihat [Contoh](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_GetQueueAttributes.html#API_GetQueueAttributes_Examples) di Referensi API *Layanan Antrian Sederhana Amazon*.

Untuk mengambil ID kunci KMS atau periode penggunaan kembali kunci data untuk antrian tertentu, jalankan `[GetQueueAttributes](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_GetQueueAttributes.html)` metode dan ambil nilai dan. `KmsMasterKeyId` `KmsDataKeyReusePeriodSeconds`