Basic Authentication with RestTemplate (3.1)

后端 未结 4 762
野性不改
野性不改 2020-12-15 12:18

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 da

相关标签:
4条回答
  • 2020-12-15 13:01

    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

    0 讨论(0)
  • 2020-12-15 13:02

    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(...
    
    0 讨论(0)
  • 2020-12-15 13:04

    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);
    
    0 讨论(0)
  • 2020-12-15 13:18

    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

    0 讨论(0)
提交回复
热议问题