Apache HttpClient - Proxy Kimlik Doğrulaması
Bu bölümde, kullanıcı adı ve parola kullanılarak kimliği doğrulanmış bir HttpRequest'i nasıl oluşturacağımızı ve bunu bir proxy aracılığıyla bir hedef ana bilgisayara bir örnek kullanarak tünellemeyi öğreneceğiz.
Adım 1 - Bir CredentialsProvider nesnesi oluşturun
CredentialsProvider Arabirimi, kullanıcı oturum açma kimlik bilgilerini tutmak için bir koleksiyon tutar. Bu arabirimin varsayılan uygulaması olan BasicCredentialsProvider sınıfını başlatarak nesnesini oluşturabilirsiniz.
CredentialsProvider credentialsPovider = new BasicCredentialsProvider();
Adım 2 - Kimlik bilgilerini ayarlayın
Gerekli kimlik bilgilerini CredentialsProvider nesnesine şu komutu kullanarak ayarlayabilirsiniz: setCredentials()yöntem. Bu yöntem iki nesneyi kabul eder -
AuthScope object - Ana bilgisayar adı, bağlantı noktası numarası ve kimlik doğrulama düzeni adı gibi ayrıntıları belirten kimlik doğrulama kapsamı.
Credentials object- Kimlik bilgilerini (kullanıcı adı, şifre) belirtme. Kimlik bilgilerini kullanarak ayarlayınsetCredentials() aşağıda gösterildiği gibi hem ana bilgisayar hem de proxy için yöntem.
credsProvider.setCredentials(new AuthScope("example.com", 80), new
UsernamePasswordCredentials("user", "mypass"));
credsProvider.setCredentials(new AuthScope("localhost", 8000), new
UsernamePasswordCredentials("abc", "passwd"));
Adım 3 - HttpClientBuilder nesnesi oluşturun
Oluşturmak HttpClientBuilder kullanmak custom() yöntemi HttpClients aşağıda gösterildiği gibi sınıf -
//Creating the HttpClientBuilder
HttpClientBuilder clientbuilder = HttpClients.custom();
Adım 4 - CredentialsProvider'ı ayarlayın
CredentialsProvider nesnesini bir HttpClientBuilder nesnesine ayarlamak için setDefaultCredentialsProvider()yöntem. Önceden oluşturulmuşCredentialsProvider bu yönteme itiraz edin.
clientbuilder = clientbuilder.setDefaultCredentialsProvider(credsProvider);
Adım 5 - CloseableHttpClient'i Oluşturun
İnşa et CloseableHttpClient kullanarak nesne build() yöntem.
CloseableHttpClient httpclient = clientbuilder.build();
Adım 6 - Proxy ve hedef ana bilgisayarları oluşturun
Hedef ve proxy ana bilgisayarlarını oluşturun. HttpHost sınıf.
//Creating the target and proxy hosts
HttpHost target = new HttpHost("example.com", 80, "http");
HttpHost proxy = new HttpHost("localhost", 8000, "http");
Adım 7 - Proxy'yi ayarlayın ve bir RequestConfig nesnesi oluşturun
Oluşturmak RequestConfig.Builder kullanarak nesne custom()yöntem. Önceden oluşturulan proxyHost nesnesiniRequestConfig.Builder kullanmak setProxy()yöntem. Son olarak,RequestConfig kullanarak nesne build() yöntem.
RequestConfig.Builder reqconfigconbuilder= RequestConfig.custom();
reqconfigconbuilder = reqconfigconbuilder.setProxy(proxyHost);
RequestConfig config = reqconfigconbuilder.build();
Adım 8 - Bir HttpGet istek nesnesi oluşturun ve buna yapılandırma nesnesi ayarlayın.
Oluşturmak HttpGetHttpGet sınıfını başlatarak nesne. Önceki adımda oluşturulan yapılandırma nesnesini bu nesneye ayarlayın.setConfig() yöntem.
//Create the HttpGet request object
HttpGet httpGet = new HttpGet("/");
//Setting the config to the request
httpget.setConfig(config);
Adım 9 - İsteği gerçekleştirin
HttpHost nesnesini (hedef) ve isteği (HttpGet) parametre olarak ileterek isteği yürütün. execute() yöntem.
HttpResponse httpResponse = httpclient.execute(targetHost, httpget);
Misal
Aşağıdaki örnek, kullanıcı adı ve parola kullanarak bir proxy aracılığıyla bir HTTP isteğinin nasıl yürütüleceğini gösterir.
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());
}
}
Çıktı
Yürütüldüğünde, yukarıdaki program aşağıdaki çıktıyı üretir -
HTTP/1.1 200 OK