Apache-httpclient-user-authentication
提供:Dev Guides
Apache HttpClient-ユーザー認証
HttpClientを使用すると、ユーザー名とパスワードが必要なWebサイトに接続できます。 この章では、ユーザー名とパスワードを要求するサイトに対してクライアント要求を実行する方法について説明します。
ステップ1-CredentialsProviderオブジェクトを作成する
*CredentialsProvider* インターフェイスは、ユーザーログイン資格情報を保持するコレクションを保持します。 このインターフェイスのデフォルト実装である *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-クレデンシャルを設定する
- setDefaultCredentialsProvider()*メソッドを使用して、上記で作成したcredentialsPoviderオブジェクトをHttpClientBuilderに設定できます。
前の手順で作成したCredentialProviderオブジェクトを、次に示すように* CredentialsProvider object()*メソッドに渡すことにより、クライアントビルダーに設定します。
clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);
ステップ5-CloseableHttpClientを構築する
*HttpClientBuilder* クラスの* build()*メソッドを使用して、 *CloseableHttpClient* オブジェクトをビルドします。
CloseableHttpClient httpclient = clientbuilder.build()
ステップ6-HttpGetオブジェクトを作成して実行する
HttpGetクラスをインスタンス化して、HttpRequestオブジェクトを作成します。 * execute()*メソッドを使用してこのリクエストを実行します。
//Creating a HttpGet object
HttpGet httpget = new HttpGet("https://www.finddevguides.com/");
//Executing the Get request
HttpResponse httpresponse = httpclient.execute(httpget);
例
以下は、ユーザー認証を必要とするターゲットサイトに対するHTTP要求の実行を示すサンプルプログラムです。
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
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 UserAuthenticationExample {
public static void main(String args[]) throws Exception{
//Create an object of credentialsProvider
CredentialsProvider credentialsPovider = new BasicCredentialsProvider();
//Set the credentials
AuthScope scope = new AuthScope("https://www.finddevguides.com/questions/", 80);
Credentials credentials = new UsernamePasswordCredentials("USERNAME", "PASSWORD");
credentialsPovider.setCredentials(scope,credentials);
//Creating the HttpClientBuilder
HttpClientBuilder clientbuilder = HttpClients.custom();
//Setting the credentials
clientbuilder = clientbuilder.setDefaultCredentialsProvider(credentialsPovider);
//Building the CloseableHttpClient object
CloseableHttpClient httpclient = clientbuilder.build();
//Creating a HttpGet object
HttpGet httpget = new HttpGet("https://www.finddevguides.com/questions/index.php");
//Printing the method used
System.out.println(httpget.getMethod());
//Executing the Get request
HttpResponse httpresponse = httpclient.execute(httpget);
//Printing the status line
System.out.println(httpresponse.getStatusLine());
int statusCode = httpresponse.getStatusLine().getStatusCode();
System.out.println(statusCode);
Header[] headers= httpresponse.getAllHeaders();
for (int i = 0; i<headers.length;i++) {
System.out.println(headers[i].getName());
}
}
}
出力
実行すると、上記のプログラムは次の出力を生成します。
GET
HTTP/1.1 200 OK
200