【问题标题】:No effect in setting proxy to AuthenticationContext in KeyVaultClient java在 KeyVaultClient java 中将代理设置为 AuthenticationContext 无效
【发布时间】:2020-03-02 11:05:08
【问题描述】:

我想运行我的 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 服务器中,它显示“未知主机”异常。

【问题讨论】:

  • 这是一个很奇怪的现象。能否请您登录windows服务器看看是否可以得到proxy.server.com的任何回复?
  • 该代理服务器在其他 Rest API 调用中运行良好,如下面的 String urlString = "https://myapiurl/api"; Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.server.com", 80)); HttpURLConnection conn = (HttpURLConnection) new URL(urlString).openConnection(proxy); 没有代理,上面的代码无法在该 Windows 服务器上运行。所以 Azure Keyvault 读取的 java 代码应该以同样的方式工作,但是唉!

标签: java azure proxy azure-keyvault java-client


【解决方案1】:

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

您可以尝试找到代理的直接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 可以工作。

【讨论】:

  • 我刚刚检查了 Azure 代码甚至没有到达那个代理代码。在到达那里之前,它因来自该 Windows 服务器的“未知主机”而失败,但在我的本地机器上,它正在使用和不使用代理。不确定如何在 Azure 连接代码到达我添加代理详细信息的上下文创建部分之前向它提供代理信息。
猜你喜欢
  • 2019-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-11
  • 1970-01-01
相关资源
最近更新 更多