Basic Authentication with RestTemplate (3.1)

谁说胖子不能爱 提交于 2019-11-29 00:02:45

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);
Makoton

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!