我一直在尝试编写一些基本代码来测试 Azure KeyVault。目前(正如您从控制台日志中看到的那样),我可以进行身份验证,但KeyVaultClient只是失败并出现[Fatal Error] :1:1: Premature end of file。
微软似乎缺乏与 azure-java-sdk 相关的真实示例,所以我承认我一直在努力解释 JavaDocs !
16:12:02.391 [main] 调试 com.example.cli.Main - 启动!16:12:02.453 [main] 调试 escli.AzureAuthenticationResult - Authresult getToken
16:12:02.491 [pool-1-thread-1] 调试 cmaad.adal4j.AuthenticationContext - [相关 ID:XXXXXXX-XXX-XXX-XXX-XXXXXXX] 使用客户端 Http 标头:x-client-SKU=java;x- client-VER=1.0.0;x-client-OS=XXXX;x-client-CPU=XXXX;return-client-request-id=true;client-request-id=XXXXXXX-XXX-XXX-XXX-XXXXXXX;
16:12:02.491 [pool-1-thread-1] INFO cmaadal4j.AuthenticationAuthority - [相关 ID:XXXXXXX-XXX-XXX-XXX-XXXXXXX] 实例发现成功
16:12:05.142 [pool-1-thread-1] DEBUG cmaad.adal4j.AuthenticationContext - [相关 ID:XXXXXXX-XXX-XXX-XXX-XXXXXXX] 访问令牌返回哈希“ZZZZZZZZZZZZZZZZZZZZZZZ”
[致命错误]:1:1:文件过早结束。
16:12:08.135 [main] 错误 com.example.cli.Main - null java.util.concurrent.ExecutionException: com.microsoft.windowsazure.exception.ServiceException:
at java.util.concurrent.FutureTask.report(FutureTask.java :122) ~[na:1.8.0_45]在 java.util.concurrent.FutureTask.get(FutureTask.java:192) ~[na:1.8.0_45]
在 com.microsoft.azure.keyvault.FutureAdapter.get(FutureAdapter.java:53) ~[azure-keyvault-0.9.0.jar:na]
在 com.example.cli.Main.main(Main.java:37) ~[classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_45]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_45]
在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_45]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_45]
at com .intellij.rt.execution.application.AppMain.main(AppMain.java:144) [idea_rt.jar:na]引起:com.microsoft.windowsazure.exception.ServiceException:
at >com.microsoft.windowsazure.exception.ServiceException.createFromXml(ServiceException.java:216) ~[azure-core-0.9.0.jar:na]
at com。 microsoft.azure.keyvault.KeyOperationsImpl.sign(KeyOperationsImpl.java:1524) ~[azure-keyvault-0.9.0.jar:na]
at com.microsoft.azure.keyvault.KeyOperationsImpl$13.call(KeyOperationsImpl.java:1447) ~[azure-keyvault-0.9.0.jar:na] 在 >com.microsoft.azure.keyvault.KeyOperationsImpl$13.call(KeyOperationsImpl.java:1444) ~[azure-keyvault-0.9.0.jar:na]
在java.util.concurrent.FutureTask.run(FutureTask.java:266) ~[na:1.8.0_45]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) ~[na:1.8.0_45]
在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) ~[na:1.8.0_45]
在 java.lang.Thread.run(Thread.java:745) ~[na:1.8.0_45]进程以退出代码 0 结束
package com.example.cli;
import com.microsoft.azure.keyvault.KeyVaultClient;
import com.microsoft.azure.keyvault.KeyVaultClientService;
import com.microsoft.azure.keyvault.models.KeyOperationResult;
import com.microsoft.azure.keyvault.webkey.JsonWebKeySignatureAlgorithm;
import com.microsoft.windowsazure.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.security.*;
import java.util.Random;
import java.util.concurrent.Future;
public class Main {
public static void main(String[] args) {
Logger logger = LoggerFactory.getLogger(Main.class);
logger.debug("Launched !");
try {
byte[] plainText = new byte[100];
new Random(0x1234567L).nextBytes(plainText);
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(plainText);
byte[] digest = md.digest();
Configuration configuration = AzureKVCredentials.createConfiguration();
KeyVaultClient keyVaultClient = KeyVaultClientService.create(configuration);
Future<KeyOperationResult> keyOperationPromise;
KeyOperationResult keyOperationResult;
keyOperationPromise = keyVaultClient.signAsync("https://XXXXXXX.vault.azure.net/keys/XXXXXXX/XXXXXXX”,JsonWebKeySignatureAlgorithm.RS256,digest);
keyOperationResult = keyOperationPromise.get(); // <=== THIS IS LINE 37 IN THE STACKTRACE ;-) <====
byte[] res = keyOperationResult.getResult();
String b64 = java.util.Base64.getEncoder().encodeToString(res);
logger.debug(b64);
} catch (Exception e) {
logger.error(null,e);
}
}
}