首页 > 解决方案 > 在 KeyVaultClient java 中将代理设置为 AuthenticationContext 无效

问题描述

我想运行我的 Java 代码以在 Windows 服务器中使用代理读取 Azure KeyVault。

我浏览了很多帖子,但可以找到任何可行的解决方案。主要用于 c#,但我想要用于 Java。我的代码在我的本地机器上运行良好,但是当我尝试在需要设置代理的 Pre-Prod Windows 服务器中运行相同的代码时,它不起作用。

AuthenticationContext context = null;
        AuthenticationResult result = null;
        ExecutorService service = null;
        try {

            service = Executors.newFixedThreadPool(1);
            context = new AuthenticationContext(authorization, false, service);
        //added below 2 lines but don't see any effect
        Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.server.com", 80));
        context.setProxy(proxy);

            ClientCredential credentials = new ClientCredential(clientId, clientKey);
            Future<AuthenticationResult> future = context.acquireToken(
                    resource, credentials, null);
            result = future.get();

当我在本地机器上运行代码时,无论有无代理设置,它都运行良好,但在该 Windows 服务器中,它显示“未知主机”异常。

标签: javaazureproxyazure-keyvaultjava-client

解决方案


我不确定以下是否会有所帮助,但您可以尝试一下。

您可以尝试找到代理的直接 IP,并在您的代码中使用它:

InetAddress[] allByName = InetAddress.getAllByName("proxy.server.com");
for (InetAddress address : allByName) {
    System.out.println(address.getHostAddress());
}

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(allByName[0].getHostAddress(),80););

或者

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(InetAddress.getByName("proxy.server.com"),80));

也许直接IP可能会起作用。


推荐阅读