简体   繁体   English

通过具有身份验证的代理使用azure-notificationhubs-java-backend

[英]Use azure-notificationhubs-java-backend via proxy with authentication

Is there a way to use the azure-notificationhubs-java-backend library behind a corporate proxy with authentication? 有没有办法在带有身份验证的公司代理后面使用azure-notificationhubs-java-backend库?

I will be using the library under an application server (JBoss 6), so I'd like to avoid the classic Java system properties approach (https.proxyHost, https.proxyPort, etc.), since it affects the whole JVM. 我将在应用程序服务器(JBoss 6)下使用该库,因此我想避免使用经典的Java系统属性方法(https.proxyHost,https.proxyPort等),因为它会影响整个JVM。

Thanks in advance. 提前致谢。

Regards, Nuno Guerreiro 问候,努诺·格雷罗

I managed to solve this problem. 我设法解决了这个问题。 I'm posting the solution here, just in case anyone needs it ;). 我在这里发布解决方案,以防万一有人需要它;)。

In my specific case, I use a Windows 8 PC and my proxy requires Windows (NTLM) authentication. 在我的特定情况下,我使用Windows 8 PC,并且我的代理服务器需要Windows(NTLM)身份验证。 The code below uses NTLM integrated authentication, ie, no username and password need to be explicitly set, since the security credentials of the currently logged-on user will be used. 下面的代码使用NTLM集成身份验证,即,无需显式设置用户名和密码,因为将使用当前登录用户的安全凭证。

import com.windowsazure.messaging.*;

import java.util.concurrent.Future;

import org.apache.http.auth.AuthSchemeProvider;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.AuthSchemes;
import org.apache.http.concurrent.FutureCallback;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.HttpHost;
import org.apache.http.impl.auth.BasicSchemeFactory;
import org.apache.http.impl.auth.DigestSchemeFactory;
import org.apache.http.impl.auth.win.WindowsCredentialsProvider;
import org.apache.http.impl.auth.win.WindowsNTLMSchemeFactory;
import org.apache.http.impl.auth.win.WindowsNegotiateSchemeFactory;
import org.apache.http.impl.client.SystemDefaultCredentialsProvider;
import org.apache.http.impl.client.WinHttpClients;
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;

public class Test9 {
    private static HttpAsyncClientBuilder createAsyncBuilderWithProxy(String proxyHost, int proxyPort) {
        if (WinHttpClients.isWinAuthAvailable()) {
            final Registry<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create()
                    .register(AuthSchemes.BASIC, new BasicSchemeFactory())
                    .register(AuthSchemes.DIGEST, new DigestSchemeFactory())
                    .register(AuthSchemes.NTLM, new WindowsNTLMSchemeFactory(null))
                    .register(AuthSchemes.SPNEGO, new WindowsNegotiateSchemeFactory(null))
                    .build();
            final CredentialsProvider credsProvider = new WindowsCredentialsProvider(new SystemDefaultCredentialsProvider());
            return HttpAsyncClientBuilder.create()
                    .setDefaultCredentialsProvider(credsProvider)
                    .setDefaultAuthSchemeRegistry(authSchemeRegistry)
                    .setProxy(new HttpHost(proxyHost, proxyPort));
        } else {
            return HttpAsyncClientBuilder.create().setProxy(new HttpHost(proxyHost, proxyPort));
        }
    }

    public static void main(String[] args) throws Exception {
        if(args.length < 4) {
            System.err.println("syntax: java Test9 <hub connection string> <hub name> <push notification address> <push message>");
        } else {
            String hubConnectionString = args[0];
            String hubName = args[1];
            String pushNotificationAddress = args[2];
            String pushMessage = args[3];

            CloseableHttpAsyncClient httpClient = createAsyncBuilderWithProxy("proxy.corporate.com", 8080).build();
            httpClient.start();
            HttpClientManager.setHttpAsyncClient(httpClient);

            NotificationHub hub = new NotificationHub(hubConnectionString, hubName);

            Notification notification = Notification.createGcmNotifiation(pushMessage);

            hub.sendDirectNotification(notification, pushNotificationAddress);

            System.out.println("Notification sent!");

            httpClient.close();
        }
    }
}

Based on the azure-notificationhubs-java-backend library without the proxy setting apis, there seems to be not any way to use it via proxy with authentication without any affection for the whole JVM of your JBoss. 基于没有代理设置api的azure-notificationhubs-java-backend库,似乎没有任何办法可以通过带有身份验证的代理使用它,而不会影响整个JBoss的JVM。

So per my experience, the only way is creating a new instance of JBoss server using Java system properties for proxy settings to run your Azure notificationhubs java backend and communicate with your main application on the other JBoss server instance via the RPC ways like REST API, WS* API, etc. 因此,根据我的经验,唯一的方法是使用Java系统属性进行代理设置来创建JBoss服务器的新实例,以运行您的Azure Notificationhubs Java后端并通过REST API等RPC方法与其他JBoss服务器实例上的主应用程序进行通信, WS * API等

Hope it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM