全部产品
Search
文档中心

Key Management Service:Contoh kode untuk mengambil nilai rahasia

更新时间:Jul 02, 2025

Setelah menginisialisasi SDK klien instance KMS, Anda dapat menggunakannya untuk memanggil API GetSecretValue guna mengambil nilai rahasia. Topik ini menyediakan contoh kode untuk proses tersebut.

Contoh lengkap

package com.aliyun.dkms.gcs.sdk.example;

import com.aliyun.dkms.gcs.openapi.models.Config;
import com.aliyun.dkms.gcs.openapi.util.models.RuntimeOptions;
import com.aliyun.dkms.gcs.sdk.Client;
import com.aliyun.dkms.gcs.sdk.models.GetSecretValueRequest;
import com.aliyun.dkms.gcs.sdk.models.GetSecretValueResponse;
import com.aliyun.tea.TeaException;

/**
 * Contoh pengambilan nilai rahasia.
 */
public class GetSecretValueSample {
		/**
		 * Objek klien instance KMS.
	 */	
    private static Client client = null;

    public static void main(String[] args) {
        try {
            // Membuat objek klien instance KMS.
            initClient();

            String secretName = "<SECRET_NAME>";

            // Contoh pengambilan nilai rahasia.
            getSecretValueSample(secretName);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Inisialisasi klien
     * @throws Exception
     */
    public static void initClient() throws Exception {
       // Atur protokol koneksi ke "https". Layanan instance KMS hanya mengizinkan akses melalui protokol HTTPS.
        Config config = new Config();
        config.setProtocol("https");
    
        // Kunci klien.
        config.setClientKeyFile("<CLIENT_KEY_FILE>");
     
         // Kata sandi kunci klien.
        config.setPassword("<PASSWORD>");
       
         // Atur endpoint ke <KMS_INSTANCE_ID>.cryptoservice.kms.aliyuncs.com.
        config.setEndpoint("<ENDPOINT>");
        
        // Sertifikat CA dari instance KMS. Anda dapat menentukan jalur ke file sertifikat CA atau memasukkan konten sertifikat CA.
        config.setCaFilePath("<CA_CERTIFICATE_PATH>");
        // Sebagai alternatif, atur ke konten sertifikat CA dari instance KMS.
        //config.setCa("<CA_CERTIFICATE_CONTENT");
        client = new Client(config);
    }

    /**
     * Contoh pengambilan nilai rahasia.
     * @param secretName
     */
    private static void getSecretValueSample(String secretName) {
        GetSecretValueRequest request = new GetSecretValueRequest()
                .setSecretName(secretName);
        try {
            // Jika Anda perlu mengabaikan sertifikat server, Anda dapat menggunakan kode yang dikomentari di sini untuk memanggil.
            //RuntimeOptions runtimeOptions = new RuntimeOptions();
            //runtimeOptions.setIgnoreSSL(true);
            //GetSecretValueResponse getSecretValueResponse = client.getSecretValueWithOptions(request, runtimeOptions);
            GetSecretValueResponse getSecretValueResponse = client.getSecretValue(request);
            System.out.printf("Nama Rahasia: %s%n", getSecretValueResponse.getSecretName());
            // System.out.printf("Data Rahasia: %s%n", getSecretValueResponse.getSecretData());
            System.out.printf("Tahap Versi: %s%n", getSecretValueResponse.getVersionStages());
            System.out.printf("ID Permintaan: %s%n", getSecretValueResponse.getRequestId());
        } catch (Exception e) {
            if (e instanceof TeaException) {
                System.out.printf("Kode: %s%n", ((TeaException) e).getCode());
                System.out.printf("Pesan: %s%n", ((TeaException) e).getMessage());
                System.out.printf("HttpCode: %s%n", ((TeaException) e).getData().get("httpCode"));
                System.out.printf("HostId: %s%n", ((TeaException) e).getData().get("hostId"));
                System.out.printf("RequestId: %s%n", ((TeaException) e).getData().get("requestId"));
            }
            e.printStackTrace();
        }
    }
}

Penjelasan contoh

Inisialisasi klien

import com.aliyun.dkms.gcs.openapi.models.Config;
import com.aliyun.dkms.gcs.sdk.Client;

                           
 public static void initClient() throws Exception {

        // Protokol koneksi. Atur nilainya ke https. Layanan instance KMS hanya mengizinkan akses melalui protokol HTTPS.
        Config config = new Config();
        config.setProtocol("https");
    
        // Kunci klien.
        config.setClientKeyFile("<CLIENT_KEY_FILE>");
     
         // Kata sandi kunci klien.
        config.setPassword("<PASSWORD>");
       
         // Endpoint instance KMS Anda. Atur nilainya dalam format berikut: <KMS_INSTANCE_ID>.cryptoservice.kms.aliyuncs.com.
        config.setEndpoint("<ENDPOINT>");
        
        // Sertifikat otoritas sertifikat (CA) dari instance KMS. Anda dapat menentukan jalur ke file sertifikat CA atau memasukkan konten sertifikat CA.
        config.setCaFilePath("<CA_CERTIFICATE_PATH>");
        // Sebagai alternatif, atur konten sertifikat CA dari instance KMS.
        //config.setCa("<CA_CERTIFICATE_CONTENT");
        client = new Client(config);
    }

Panggil API GetSecretValue

    /**
     * Contoh pengambilan nilai rahasia.
     * @param secretName
     */
    private static void getSecretValueSample(String secretName) {
        GetSecretValueRequest request = new GetSecretValueRequest()
                .setSecretName(secretName);
        try {
            // Jika Anda perlu mengabaikan sertifikat server, Anda dapat menggunakan kode yang dikomentari di sini untuk memanggil
            //RuntimeOptions runtimeOptions = new RuntimeOptions();
            //runtimeOptions.setIgnoreSSL(true);
            //GetSecretValueResponse getSecretValueResponse = client.getSecretValueWithOptions(request, runtimeOptions);
            GetSecretValueResponse getSecretValueResponse = client.getSecretValue(request);
            System.out.printf("Nama Rahasia: %s%n", getSecretValueResponse.getSecretName());
         // System.out.printf("Data Rahasia: %s%n", getSecretValueResponse.getSecretData());
            System.out.printf("Tahap Versi: %s%n", getSecretValueResponse.getVersionStages());
            System.out.printf("ID Permintaan: %s%n", getSecretValueResponse.getRequestId());
        } catch (Exception e) {
            if (e instanceof TeaException) {
                System.out.printf("Kode: %s%n", ((TeaException) e).getCode());
                System.out.printf("Pesan: %s%n", ((TeaException) e).getMessage());
                System.out.printf("HttpCode: %s%n", ((TeaException) e).getData().get("httpCode"));
                System.out.printf("HostId: %s%n", ((TeaException) e).getData().get("hostId"));
                System.out.printf("RequestId: %s%n", ((TeaException) e).getData().get("requestId"));
            }
            e.printStackTrace();
        }
    }
}