web-push-libs / webpush-java

Web Push library for Java
MIT License
323 stars 112 forks source link

proxy autentication with username and password dosnt'work #49

Closed gianlucaUlivo closed 6 years ago

gianlucaUlivo commented 6 years ago

if i configure the JVM parameter with -Dhttp.proxyHost=proxy.test.it -Dhttp.proxyPort=3128 -Dhttp.proxyUser=username -Dhttp.proxyPassword=passw -Dhttps.proxyHost=proxy.test.it -Dhttps.proxyPort=3128 -Dhttps.proxyUser=username -Dhttps.proxyPassword=passw but the autentication doesn't work.

the solution to work is: HttpHost proxy = new HttpHost("proxy.test.it", 3128); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);

CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("proxy.test.it", 3128), new UsernamePasswordCredentials("username", "passw")); CloseableHttpAsyncClient closeableHttpAsyncClient = HttpAsyncClients.custom() .setProxy(proxy) .setRoutePlanner(routePlanner) .setDefaultCredentialsProvider(credsProvider) .build();

    closeableHttpAsyncClient.start();

    HttpResponse httpResponse = (HttpResponse) closeableHttpAsyncClient.execute(httpPost, new ClosableCallback(closeableHttpAsyncClient)).get();
martijndwars commented 6 years ago

Hm, I have never tested a proxy with authentication. I'll look into this. In the meantime you could use the preparePost method that was added in 3.1.0 (released this morning). This returns the HttpPost that you can then use to make the request using the code you just posted.

gianlucaUlivo commented 6 years ago

Can you post an example... 😬

martijndwars commented 6 years ago

Well, using the code you suggested, it basically boils down to...

HttpPost httpPost = pushService.preparePost(notification);

and then:

HttpHost proxy = new HttpHost("proxy.test.it", 3128);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
                new AuthScope("proxy.test.it", 3128),
                new UsernamePasswordCredentials("username", "passw")
);

CloseableHttpAsyncClient closeableHttpAsyncClient = HttpAsyncClients.custom()
                .setProxy(proxy)
                .setRoutePlanner(routePlanner)
                .setDefaultCredentialsProvider(credsProvider)
                .build();

closeableHttpAsyncClient.start();

HttpResponse httpResponse = (HttpResponse) closeableHttpAsyncClient.execute(httpPost, new ClosableCallback(closeableHttpAsyncClient)).get();
chrisprice commented 6 years ago

For anyone using a proxy that doesn't support UsernamePasswordCredentials, the Apache Http Client docs cover alternative authentication strategies.

martijndwars commented 6 years ago

Thanks for the addition! I assume from your reply that this solution solves the original issue posted by @gianlucaUlivo, so I'm closing this.

960590968 commented 1 year ago

Well, using the code you suggested, it basically boils down to...

HttpPost httpPost = pushService.preparePost(notification);

and then:

HttpHost proxy = new HttpHost("proxy.test.it", 3128);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);

CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
                new AuthScope("proxy.test.it", 3128),
                new UsernamePasswordCredentials("username", "passw")
);

CloseableHttpAsyncClient closeableHttpAsyncClient = HttpAsyncClients.custom()
                .setProxy(proxy)
                .setRoutePlanner(routePlanner)
                .setDefaultCredentialsProvider(credsProvider)
                .build();

closeableHttpAsyncClient.start();

HttpResponse httpResponse = (HttpResponse) closeableHttpAsyncClient.execute(httpPost, new ClosableCallback(closeableHttpAsyncClient)).get();

This code will change all http request by proxy or only changed current send notification?