You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Mop Sophia <mo...@gmail.com> on 2016/04/11 18:18:31 UTC
Proxy with digest authentication
Hi,
I try to do a request using a proxy with digest authentication but the
authentication fails, here is the code :
// Proxy credentials
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope("80.1.2.3", 3128),
new UsernamePasswordCredentials("proxy_user", "proxy_pass"));
HttpHost proxy = new HttpHost("80.1.2.3", 3128);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.setDefaultCredentialsProvider(credsProvider)
.setProxy(proxy)
.build();
HttpGet httpget = new HttpGet("https://myhost.com:8443");
HttpClientContext context = HttpClientContext.create();
CloseableHttpResponse resp = httpclient.execute(httpget, context);
Log.d("TST", String.valueOf(resp.getStatusLine()));
Log.d("TST", EntityUtils.toString(resp.getEntity()));
AuthState proxyAuthState = context.getProxyAuthState();
Log.d("TST", "Proxy auth state: " + proxyAuthState.getState());
Log.d("TST", "Proxy auth scheme: " + proxyAuthState.getAuthScheme());
Log.d("TST", "Proxy auth credentials: " + proxyAuthState.getCredentials());
AuthState targetAuthState = context.getTargetAuthState();
Log.d("TST", "Target auth state: " + targetAuthState.getState());
Log.d("TST", "Target auth scheme: " + targetAuthState.getAuthScheme());
Log.d("TST", "Target auth credentials: " + targetAuthState.getCredentials());
Any idea, please ?
Thanks,
Stéphane
Re: Proxy with digest authentication
Posted by Mop Sophia <mo...@gmail.com>.
Hi,
Thanks Mark for your help, it's now ok with :
new AuthScope(proxy, AuthScope.ANY_REALM, AuthSchemes.DIGEST)
In my mind, the default constructor was with AuthSchemes.ALL and so should
work with DIGEST.
But with the specified DIGEST value, it's ok, thank you very much.
Stéphane
2016-04-12 17:52 GMT+02:00 Mark A. Claassen <MC...@ocie.net>:
> I have been working with Proxies as well. I am no expert, but I think
> your AuthScope is insufficient.
>
> I have a different situation that you do. I override CredentialsProvider,
> which I don't think you need to do, but it does reveal some things when you
> do it that way.
>
> When an AuthScope comes into getCredentials(AuthScope a); it has a
> "scheme" associated with it. That scheme is matched against other schemes,
> such as AuthSchemes.DIGEST. You may be able to just change your code to
> create your AuthScope creation to something like:
>
> new AuthScope(new HttpHost("80.1.2.3", 3128), AuthScope.ANY_REALM,
> AuthScheme.DIGEST)
>
> If not, you can create your own CredentialsProvider. In the
> getCredentials() method, you can verify that it is using a DIGEST scheme
> and just return the credentials directly, or potentially put them in a
> private instance of BasicCredentialsProvider, using the passed in AuthScope
> as the parameter to setCredentials().
>
> public Credentials getCredentials(AuthScope authscope) {
> Credentials rval = delegate.getCredentials(authscope);
> if (rval == null &&
> AuthSchemes.DIGEST.equalsIgnoreCase(scheme)) }
> ...
> delegate.setProvider(authscope,rval)
> }
> }
>
> Hope this helps,
> Mark
>
>
> Disclaimer:
> The opinions provided herein do not necessarily state or reflect those of
> Donnell Systems, Inc.(DSI). DSI makes no warranty for and assumes no legal
> liability or responsibility for the posting.
>
> -----Original Message-----
> From: Mop Sophia [mailto:mopsophia@gmail.com]
> Sent: Monday, April 11, 2016 12:19 PM
> To: httpclient-users@hc.apache.org
> Subject: Proxy with digest authentication
>
> Hi,
>
> I try to do a request using a proxy with digest authentication but the
> authentication fails, here is the code :
>
> // Proxy credentials
> CredentialsProvider credsProvider = new BasicCredentialsProvider();
> credsProvider.setCredentials(new AuthScope("80.1.2.3", 3128),
> new UsernamePasswordCredentials("proxy_user", "proxy_pass"));
> HttpHost proxy = new HttpHost("80.1.2.3", 3128);
>
> CloseableHttpClient httpclient = HttpClients.custom()
> .setSSLSocketFactory(sslsf)
> .setDefaultCredentialsProvider(credsProvider)
> .setProxy(proxy)
> .build();
>
>
> HttpGet httpget = new HttpGet("https://myhost.com:8443");
>
> HttpClientContext context = HttpClientContext.create();
>
> CloseableHttpResponse resp = httpclient.execute(httpget, context);
>
> Log.d("TST", String.valueOf(resp.getStatusLine()));
> Log.d("TST", EntityUtils.toString(resp.getEntity()));
>
> AuthState proxyAuthState = context.getProxyAuthState(); Log.d("TST",
> "Proxy auth state: " + proxyAuthState.getState()); Log.d("TST", "Proxy auth
> scheme: " + proxyAuthState.getAuthScheme()); Log.d("TST", "Proxy auth
> credentials: " + proxyAuthState.getCredentials()); AuthState
> targetAuthState = context.getTargetAuthState(); Log.d("TST", "Target auth
> state: " + targetAuthState.getState()); Log.d("TST", "Target auth scheme: "
> + targetAuthState.getAuthScheme()); Log.d("TST", "Target auth credentials:
> " + targetAuthState.getCredentials());
>
>
>
>
> Any idea, please ?
>
> Thanks,
>
> Stéphane
>
RE: Proxy with digest authentication
Posted by "Mark A. Claassen" <MC...@ocie.net>.
I have been working with Proxies as well. I am no expert, but I think your AuthScope is insufficient.
I have a different situation that you do. I override CredentialsProvider, which I don't think you need to do, but it does reveal some things when you do it that way.
When an AuthScope comes into getCredentials(AuthScope a); it has a "scheme" associated with it. That scheme is matched against other schemes, such as AuthSchemes.DIGEST. You may be able to just change your code to create your AuthScope creation to something like:
new AuthScope(new HttpHost("80.1.2.3", 3128), AuthScope.ANY_REALM, AuthScheme.DIGEST)
If not, you can create your own CredentialsProvider. In the getCredentials() method, you can verify that it is using a DIGEST scheme and just return the credentials directly, or potentially put them in a private instance of BasicCredentialsProvider, using the passed in AuthScope as the parameter to setCredentials().
public Credentials getCredentials(AuthScope authscope) {
Credentials rval = delegate.getCredentials(authscope);
if (rval == null && AuthSchemes.DIGEST.equalsIgnoreCase(scheme)) }
...
delegate.setProvider(authscope,rval)
}
}
Hope this helps,
Mark
Disclaimer:
The opinions provided herein do not necessarily state or reflect those of Donnell Systems, Inc.(DSI). DSI makes no warranty for and assumes no legal liability or responsibility for the posting.
-----Original Message-----
From: Mop Sophia [mailto:mopsophia@gmail.com]
Sent: Monday, April 11, 2016 12:19 PM
To: httpclient-users@hc.apache.org
Subject: Proxy with digest authentication
Hi,
I try to do a request using a proxy with digest authentication but the authentication fails, here is the code :
// Proxy credentials
CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope("80.1.2.3", 3128),
new UsernamePasswordCredentials("proxy_user", "proxy_pass")); HttpHost proxy = new HttpHost("80.1.2.3", 3128);
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf)
.setDefaultCredentialsProvider(credsProvider)
.setProxy(proxy)
.build();
HttpGet httpget = new HttpGet("https://myhost.com:8443");
HttpClientContext context = HttpClientContext.create();
CloseableHttpResponse resp = httpclient.execute(httpget, context);
Log.d("TST", String.valueOf(resp.getStatusLine()));
Log.d("TST", EntityUtils.toString(resp.getEntity()));
AuthState proxyAuthState = context.getProxyAuthState(); Log.d("TST", "Proxy auth state: " + proxyAuthState.getState()); Log.d("TST", "Proxy auth scheme: " + proxyAuthState.getAuthScheme()); Log.d("TST", "Proxy auth credentials: " + proxyAuthState.getCredentials()); AuthState targetAuthState = context.getTargetAuthState(); Log.d("TST", "Target auth state: " + targetAuthState.getState()); Log.d("TST", "Target auth scheme: " + targetAuthState.getAuthScheme()); Log.d("TST", "Target auth credentials: " + targetAuthState.getCredentials());
Any idea, please ?
Thanks,
Stéphane