Java httpClient 4.3.6基本身份验证,包含完整的URI和方案

时间:2015-02-06 13:34:33

标签: java authentication basic-authentication apache-httpclient-4.x

我想要的是什么: 使用preemtive bassic身份验证发送GET请求。

请求看起来像这样:

<startURL>/app/process?job=doSomething&param=value1,value2

而startURL始终是https链接取决于环境。

看起来像这样:     https://testABC.com     https://prodABC.com

startURL也放在属性文件中,与不同环境一样。

我的目的是: http://www.baeldung.com/httpclient-4-basic-authentication

http://www.java-tips.org/other-api-tips/httpclient/how-to-use-basic-authentication.html

http://hc.apache.org/httpcomponents-client-ga/httpclient/examples/org/apache/http/examples/client/ClientPreemptiveBasicAuthentication.java

http://hc.apache.org/httpcomponents-client-ga/tutorial/html/authentication.html

这一切都包含

 HttpHost targetHost = new HttpHost("hostname", portnumber, "scheme");

这就是我遇到的麻烦。此方法也是唯一允许您将方案指定为“https”的方法。 一个问题是,帽子我不知道这个号码。我认为(?)我可能只能为默认端口指定-1,以使其工作,但即使抛开我也没有主机名,只有上面提到的startURL。我真的不想每次都解析这个额外的,而我也不想添加另一个属性,只是为了主机名。

我四处寻找并找到了这个片段,看起来就像我想要的那样:

HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://foo.com/bar");
httpGet.addHeader(BasicScheme.authenticate(
 new UsernamePasswordCredentials("user", "password"),
 "UTF-8", false));

HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity responseEntity = httpResponse.getEntity();

来自HTTP requests with basic authentication

它提供完整的请求URL,只需添加基本标头,不需要指定任何端口。仅从版本4.2开始不推荐使用它:

Deprecated. (4.2) Use ContextAwareAuthScheme.authenticate( Credentials, HttpRequest, org.apache.http.protocol.HttpContext)

我找不到这个方法的一个例子来返回基本的auth头。它还需要一个上下文作为参数,上面的剪切没有。我真的不知道应该如何使用它。

所以,我想具体了解一下:

我只想设置一个包含完整链接的请求,其中包含所有内容,例如:

https://testABC.com/app/process?job=doSomething&param=value1,value2

并将此作为执行抢占式基本身份验证的请求的参数。

有没有办法在不挖掘弃用的方法的情况下做到这一点?它看起来像什么?

2 个答案:

答案 0 :(得分:3)

我遇到了和你一样的问题。

对我有用的是:

UsernamePasswordCredentials creds = new UsernamePasswordCredentials("user", "12345");

HttpGet get = new HttpGet("https://foo.bar.com/rest");
HttpHost targetHost = new HttpHost("foo.bar.com", 443, "https");
  CredentialsProvider credsProvider = new BasicCredentialsProvider();
  credsProvider.setCredentials(
          new AuthScope(targetHost.getHostName(), targetHost.getPort()),
          creds);
  credsProvider.setCredentials(AuthScope.ANY,creds);

 // Create AuthCache instance
  AuthCache authCache = new BasicAuthCache();
 // Generate BASIC scheme object and add it to the local auth cache
  BasicScheme basicAuth = new BasicScheme();
  authCache.put(targetHost, basicAuth);

 // Add AuthCache to the execution context
  HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
context.setAuthCache(authCache);
HttpResponse response = client.execute(targetHost, get, context);

我在HttpClientBuilder basic auth

上找到了这个解决方案

答案 1 :(得分:1)

最后,我自己手动编写标题并发送内容:

    String header = "Basic ";       
    String headerValue = "username" + ":" + "password";
    String encodedHeaderValue = Base64.encodeBase64String(headerValue.getBytes());      
    String headerBasic =  header + encodedHeaderValue;

    Header authHeader = new BasicHeader("Authorization", headerBasic);
      ArrayList<Header> headers = new ArrayList<Header>();
      headers.add(authHeader);  

    ArrayList<Header> headers = getHttpHeaders();
    HttpClient client = HttpClients.custom().setDefaultHeaders(headers).build();

    HttpUriRequest request = RequestBuilder.get().setUri(uri).build();
    HttpResponse response = client.execute(request);

    int responseCode = response.getStatusLine().getStatusCode();