I am trying to reproduce the following curl command using Java:
curl -v -u user:pass http://myapp.com/api
This command returns some JSON data.
My buggy Java implementation is as follows:
@Test
public void callTest() {
RestTemplate restTemplate = createRestTemplate("user", "pass");
URI uri = new URI("http://myapp.com/api");
String res = restTemplate.getForObject(uri, String.class);
}
private static RestTemplate createRestTemplate(String username, String password) {
UsernamePasswordCredentials cred = new UsernamePasswordCredentials(username, password);
BasicCredentialsProvider cp = new BasicCredentialsProvider();
cp.setCredentials(AuthScope.ANY, cred);
DefaultHttpClient client = new DefaultHttpClient();
client.setCredentialsProvider(cp);
ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(client);
RestTemplate restTemplate = new RestTemplate(factory);
// set the media types properly
return restTemplate;
}
Yet, when I execute the test, it returns a org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
exception.
When logging in DEBUG
, I see no information about the authentication...
What am I doing wrong while setting the authentication credentials?
Instantiating using
HttpClient client = new HttpClient();
doesn't exist anymore and class DefaultHttpClient
is deprecated from HttpComponents HttpClient from version 4.3. So other answer are either invalid or deprecated. Here is my version, I wrote this class for rest requests which require basic authentication:
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClients;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
public class RestClient extends RestTemplate {
public RestClient(String username, String password) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(null, -1),
new UsernamePasswordCredentials(username, password));
HttpClient httpClient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
}
}
Then use it like, for example:
RestClient restClient = new RestClient("username", "password");
String result = restClient.postForObject(...
I do something like this - and it has worked for me:
private RestTemplate createRestTemplate(String username, String password) {
return new RestTemplate(this.createSecureTransport(username, password));
}
protected ClientHttpRequestFactory createSecureTransport(String username, String password){
HttpClient client = new HttpClient();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username,password);
client.getState().setCredentials(AuthScope.ANY, credentials);
CommonsClientHttpRequestFactory commons = new CommonsClientHttpRequestFactory(client);
return commons;
}
It is used here: Reference Code
This answer is based on the one by @kevinpeterson, but with a rewrite to use the updated Apache HTTP Client.
RestTemplate createRestTemplate(String username, String password, String host, int port ) {
return new RestTemplate(this.createSecureTransport( username, password, host, port ));
}
ClientHttpRequestFactory createSecureTransport( String username, String password, String host, int port ){
DefaultHttpClient client = new DefaultHttpClient();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials( username, password );
client.getCredentialsProvider().setCredentials( new AuthScope( host, port ), credentials );
return new HttpComponentsClientHttpRequestFactory(client);
httpclient v4.0 not Support Preemptive authentication
Look this : http://forum.springsource.org/showthread.php?123385-Preemptive-Basic-Authentication-with-RestTemplate
In spring Source found this solution.
It works for me.
Look this : RestTemplate with Basic Auth in Spring 3.1
来源:https://stackoverflow.com/questions/14383240/basic-authentication-with-resttemplate-3-1