7

我正在尝试使用 Java 重现以下 curl 命令:

curl -v -u user:pass http://myapp.com/api

此命令返回一些 JSON 数据。

我的错误Java实现如下:

@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;
}

然而,当我执行测试时,它返回一个org.springframework.web.client.HttpClientErrorException: 401 Unauthorized异常。

登录时DEBUG,我看不到有关身份验证的信息...

设置身份验证凭据时我做错了什么?

4

4 回答 4

13

使用实例化

HttpClient client = new HttpClient();

不再存在,并且从版本 4.3的HttpComponents HttpClientDefaultHttpClient中弃用了类。因此,其他答案无效或已弃用。这是我的版本,我为需要基本身份验证的休息请求编写了这个类:

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

然后像这样使用它,例如:

RestClient restClient = new RestClient("username", "password");

String result = restClient.postForObject(...
于 2013-12-08T00:22:55.127 回答
1

我做了这样的事情 - 它对我有用:

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;
}

此处使用:参考代码

于 2013-01-18T12:46:41.967 回答
1

httpclient v4.0 不支持抢先认证

看这个: http: //forum.springsource.org/showthread.php?123385-Preemptive-Basic-Authentication-with-RestTemplate

在 spring Source 中找到了这个解决方案。

这个对我有用。

看这个:RestTemplate with Basic Auth in Spring 3.1

于 2013-03-25T22:24:40.787 回答
1

此答案基于@kevinpeterson 的答案,但经过重写以使用更新的 Apache HTTP 客户端。

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);
于 2013-08-24T15:25:35.027 回答