[英]Basic Authentication with RestTemplate (3.1)
I am trying to reproduce the following curl command using Java: 我试图使用Java重现以下curl命令:
curl -v -u user:pass http://myapp.com/api
This command returns some JSON data. 此命令返回一些JSON数据。
My buggy Java implementation is as follows: 我的错误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;
}
Yet, when I execute the test, it returns a org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
exception. 然而,当我执行测试时,它返回一个
org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
exception。
When logging in DEBUG
, I see no information about the authentication... 登录
DEBUG
,我看不到有关身份验证的信息......
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 . 不再存在,并且从版本4.3的 HttpComponents HttpClient中弃用了类
DefaultHttpClient
。 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 它在这里使用: 参考代码
httpclient v4.0 not Support Preemptive authentication httpclient v4.0不支持抢占式身份验证
Look this : http://forum.springsource.org/showthread.php?123385-Preemptive-Basic-Authentication-with-RestTemplate 看看这个: http : //forum.springsource.org/showthread.php?123385-Preemptive-Basic-Authentication-with-RestTemplate
In spring Source found this solution. 在春天Source找到了这个解决方案。
It works for me. 这个对我有用。
Look this : RestTemplate with Basic Auth in Spring 3.1 看看这个: 在Spring 3.1中使用Basic Auth进行RestTemplate
This answer is based on the one by @kevinpeterson, but with a rewrite to use the updated Apache HTTP Client. 这个答案基于@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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.