如何在Oracle Cloud Infrastructure中使用密码读取.pem文件?

时间:2019-04-02 12:43:10

标签: oracle-cloud-infrastructure oci-java-sdk

我需要使用REST API监视Oracle Cloud Compute VM。我在Oracle文档中找到了以下用于使用标头签名的代码行。

String privateKeyFilename = "/.oci/oci_api_key.pem";
PrivateKey privateKey = loadPrivateKey(privateKeyFilename);      
RequestSigner signer = new RequestSigner(apiKey, privateKey);

loadPrivateKey(privateKeyFilename)方法

private static PrivateKey loadPrivateKey(String privateKeyFilename) {
    System.out.println(SystemUtils.getUserHome().toString() + Paths.get(privateKeyFilename));
    try (InputStream privateKeyStream = Files
            .newInputStream(Paths.get(SystemUtils.getUserHome().toString() + privateKeyFilename))) {
        return PEM.readPrivateKey(privateKeyStream);
    } catch (InvalidKeySpecException e) {
        throw new RuntimeException("Invalid format for private key");
    } catch (IOException e) {
        throw new RuntimeException("Failed to load private key");
    }
}

执行这些行将涵盖使用密码短语读取文件的操作。有输入吗?

0 个答案:

没有答案