通过代理使用azure-notificationhubs-java-backend进行身份验证

时间:2017-09-18 11:42:22

标签: java azure-notificationhub

有没有办法在公司代理后面使用azure-notificationhubs-java-backend库进行身份验证?

我将在应用服务器(JBoss 6)下使用该库,因此我想避免使用传统的Java系统属性方法(https.proxyHost,https.proxyPort等),因为它会影响整个JVM。

提前致谢。

此致 Nuno Guerreiro

2 个答案:

答案 0 :(得分:1)

我设法解决了这个问题。我在这里发布解决方案,万一有人需要它;)。

在我的具体情况下,我使用的是Windows 8 PC,我的代理需要Windows(NTLM)身份验证。下面的代码使用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();
        }
    }
}

答案 1 :(得分:0)

基于没有代理设置apis的azure-notificationhubs-java-backend库,似乎没有任何方法可以通过代理使用它进行身份验证,而不会对JBoss的整个JVM产生任何影响。

根据我的经验,唯一的方法是使用Java系统属性为代理设置创建一个新的JBoss服务器实例,以运行Azure notificationhubs java后端,并通过RPC方式与其他JBoss服务器实例上的主应用程序进行通信REST API,WS * API等

希望它有所帮助。