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

通过具有身份验证的代理使用azure notificationhubs java后端,java,azure-notificationhub,Java,Azure Notificationhub,有没有办法在具有身份验证的公司代理后面使用azure notificationhubs java后端库 我将在应用服务器(JBoss 6)下使用该库,因此我希望避免使用经典的Java系统属性方法(https.proxyHost、https.proxyPort等),因为它会影响整个JVM 提前谢谢 问候,, Nuno Guerreiro基于没有代理设置API的azure notificationhubs java后端库,似乎没有任何方法可以通过代理使用它并进行身份验证而不影响JBoss的整个JVM

有没有办法在具有身份验证的公司代理后面使用azure notificationhubs java后端库

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

提前谢谢

问候,,
Nuno Guerreiro

基于没有代理设置API的azure notificationhubs java后端库,似乎没有任何方法可以通过代理使用它并进行身份验证而不影响JBoss的整个JVM

因此,根据我的经验,唯一的方法是使用Java system properties for proxy设置创建JBoss server的新实例,以运行Azure notificationhubs Java后端,并通过REST API、WS*API等RPC方式与其他JBoss server实例上的主应用程序通信


希望有帮助。

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

在我的特定情况下,我使用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();
        }
    }
}
导入com.windowsazure.messaging.*;
导入java.util.concurrent.Future;
导入org.apache.http.auth.AuthSchemeProvider;
导入org.apache.http.client.CredentialsProvider;
导入org.apache.http.client.config.AuthSchemes;
导入org.apache.http.concurrent.FutureCallback;
导入org.apache.http.config.Registry;
导入org.apache.http.config.RegistryBuilder;
导入org.apache.http.HttpHost;
导入org.apache.http.impl.auth.BasicSchemeFactory;
导入org.apache.http.impl.auth.DigestSchemeFactory;
导入org.apache.http.impl.auth.win.WindowsCredentialsProvider;
导入org.apache.http.impl.auth.win.WindowsNTLMSchemeFactory;
导入org.apache.http.impl.auth.win.WindowsNegotiateSchemeFactory;
导入org.apache.http.impl.client.SystemDefaultCredentialsProvider;
导入org.apache.http.impl.client.WinHttpClients;
导入org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
导入org.apache.http.impl.nio.client.httpacyncclientbuilder;
公共类Test9{
专用静态HttpAsyncClientBuilder CreateAyncBuilderWithProxy(字符串代理主机,int代理端口){
if(WinHttpClients.isWinAuthAvailable()){
最终注册表authSchemeRegistry=RegistryBuilder.create()
.register(AuthSchemes.BASIC,new BasicSchemeFactory())
.register(AuthSchemes.DIGEST,新的DigestSchemeFactory())
.register(AuthSchemes.NTLM,新WindowsNTLMSchemeFactory(null))
.register(AuthSchemes.SPNEGO,新WindowsNegotiateSchemeFactory(null))
.build();
final CredentialsProvider credsProvider=新的WindowsCredentialsProvider(新的SystemDefaultCredentialsProvider());
返回HttpAsyncClientBuilder.create()
.setDefaultCredentialsProvider(CredProvider)
.setDefaultAuthSchemeRegistry(authSchemeRegistry)
.setProxy(新的HttpHost(proxyHost,proxyPort));
}否则{
返回HttpAsyncClientBuilder.create().setProxy(新的HttpHost(proxyHost,proxyPort));
}
}
公共静态void main(字符串[]args)引发异常{
如果(参数长度<4){
System.err.println(“语法:JavaTest9”);
}否则{
字符串hubConnectionString=args[0];
字符串hubName=args[1];
字符串pushNotificationAddress=args[2];
字符串pushMessage=args[3];
CloseableHttpAsyncClient httpClient=createAsyncBuilderWithProxy(“proxy.corporate.com”,8080.build();
httpClient.start();
HttpClientManager.setHttpAsyncClient(httpClient);
NotificationHub hub=新的NotificationHub(hubConnectionString,hubName);
通知通知=通知.createGCMNotification(pushMessage);
hub.sendDirectNotification(通知,pushNotificationAddress);
System.out.println(“通知已发送!”);
httpClient.close();
}
}
}