Apache-httpclient-proxy-authentication

提供:Dev Guides
移動先:案内検索

Apache HttpClient-プロキシ認証

この章では、例を使用して、ユーザー名とパスワードを使用して認証されたHttpRequestを作成し、プロキシを介してターゲットホストにトンネルする方法を学習します。

ステップ1-CredentialsProviderオブジェクトを作成する

CredentialsProvider Interfaceは、ユーザーログイン資格情報を保持するコレクションを維持します。 このインターフェイスのデフォルト実装であるBasicCredentialsProviderクラスをインスタンス化することにより、そのオブジェクトを作成できます。

CredentialsProvider credentialsPovider = new BasicCredentialsProvider();

ステップ2-資格情報を設定する

  • setCredentials()*メソッドを使用して、CredentialsProviderオブジェクトに必要な資格情報を設定できます。 このメソッドは、2つのオブジェクトを受け入れます-
  • * AuthScopeオブジェクト*-ホスト名、ポート番号、認証スキーム名などの詳細を指定する認証スコープ。
  • 資格情報オブジェクト-資格情報(ユーザー名、パスワード)を指定します。 以下に示すように、ホストとプロキシの両方に対して* setCredentials()*メソッドを使用して資格情報を設定します。
credsProvider.setCredentials(new AuthScope("example.com", 80), new
   UsernamePasswordCredentials("user", "mypass"));
credsProvider.setCredentials(new AuthScope("localhost", 8000), new
   UsernamePasswordCredentials("abc", "passwd"));

ステップ3-HttpClientBuilderオブジェクトを作成する

以下に示すように HttpClients クラスの* custom()メソッドを使用して *HttpClientBuilder を作成します-

//Creating the HttpClientBuilder
HttpClientBuilder clientbuilder = HttpClients.custom();

ステップ4-CredentialsProviderの設定

  • setDefaultCredentialsProvider()メソッドを使用して、CredentialsProviderオブジェクトをHttpClientBuilderオブジェクトに設定できます。 以前に作成した *CredentialsProvider オブジェクトをこのメソッドに渡します。
clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);

ステップ5-CloseableHttpClientを構築する

  • build()メソッドを使用して *CloseableHttpClient オブジェクトをビルドします。
CloseableHttpClient httpclient = clientbuilder.build();

ステップ6-プロキシおよびターゲットホストを作成する

*HttpHost* クラスをインスタンス化して、ターゲットおよびプロキシホストを作成します。
//Creating the target and proxy hosts
HttpHost target = new HttpHost("example.com", 80, "http");
HttpHost proxy = new HttpHost("localhost", 8000, "http");

ステップ7-プロキシを設定し、RequestConfigオブジェクトを構築します

  • custom()メソッドを使用して *RequestConfig.Builder オブジェクトを作成します。 * setProxy()メソッドを使用して、以前に作成したproxyHostオブジェクトを *RequestConfig.Builder に設定します。 最後に、* build()メソッドを使用して *RequestConfig オブジェクトをビルドします。
RequestConfig.Builder reqconfigconbuilder= RequestConfig.custom();
reqconfigconbuilder = reqconfigconbuilder.setProxy(proxyHost);
RequestConfig config = reqconfigconbuilder.build();

ステップ8-HttpGet要求オブジェクトを作成し、それに構成オブジェクトを設定します。

HttpGetクラスをインスタンス化して HttpGet オブジェクトを作成します。 * setConfig()*メソッドを使用して、前の手順で作成した構成オブジェクトをこのオブジェクトに設定します。

//Create the HttpGet request object
HttpGet httpGet = new HttpGet("/");

//Setting the config to the request
httpget.setConfig(config);

ステップ9-リクエストを実行する

HttpHostオブジェクト(ターゲット)とリクエスト(HttpGet)をパラメーターとして* execute()*メソッドに渡すことにより、リクエストを実行します。

HttpResponse httpResponse = httpclient.execute(targetHost, httpget);

次の例は、ユーザー名とパスワードを使用してプロキシ経由でHTTP要求を実行する方法を示しています。

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;

public class ProxyAuthenticationExample {
   public static void main(String[] args) throws Exception {

     //Creating the CredentialsProvider object
      CredentialsProvider credsProvider = new BasicCredentialsProvider();

     //Setting the credentials
      credsProvider.setCredentials(new AuthScope("example.com", 80),
         new UsernamePasswordCredentials("user", "mypass"));
      credsProvider.setCredentials(new AuthScope("localhost", 8000),
         new UsernamePasswordCredentials("abc", "passwd"));

     //Creating the HttpClientBuilder
      HttpClientBuilder clientbuilder = HttpClients.custom();

     //Setting the credentials
      clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);

     //Building the CloseableHttpClient object
      CloseableHttpClient httpclient = clientbuilder.build();


     //Create the target and proxy hosts
      HttpHost targetHost = new HttpHost("example.com", 80, "http");
      HttpHost proxyHost = new HttpHost("localhost", 8000, "http");

     //Setting the proxy
      RequestConfig.Builder reqconfigconbuilder= RequestConfig.custom();
      reqconfigconbuilder = reqconfigconbuilder.setProxy(proxyHost);
      RequestConfig config = reqconfigconbuilder.build();

     //Create the HttpGet request object
      HttpGet httpget = new HttpGet("/");

     //Setting the config to the request
      httpget.setConfig(config);

     //Printing the status line
      HttpResponse response = httpclient.execute(targetHost, httpget);
      System.out.println(response.getStatusLine());

   }
}

出力

実行すると、上記のプログラムは次の出力を生成します-

HTTP/1.1 200 OK