I’m trying to do Digest mostly (or Basic) Authentication using RestTemplate and httpclient (4.x).
Since I couldn’t find any relevant examples of how to actually do this, I have attempted various ways to hook the various httpclient artifacts, with no luck – essentially, no Authentication header is sent at all.
My current implementation is:
DefaultHttpClient newHttpClient = new DefaultHttpClient();
Credentials credentials = new UsernamePasswordCredentials( username, password );
AuthScope authScope = new AuthScope( host, port, AuthScope.ANY_REALM );
BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials( authScope, credentials );
newHttpClient.setCredentialsProvider( credentialsProvider );
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory( newHttpClient );
restTemplate.setRequestFactory( requestFactory );
Is there something I’m doing wrong? Is there also a working example for this anywhere?
Any help is appreciated.
Thanks.
Try implementing your own RequestFactory in order to achieve preemptive authentication.
An then just use it:
Hope it helps
how to set the username and password (Copied from @bifur’s comment)
You can use
UserNamePasswordCredentialsAnd just use the client in the previous factory