У кого-либо есть какие-либо дружественные подсказки относительно того, как выполнить аутентификацию клиента с помощью x509 сертификата с помощью HTTPClient 4.0.1?
Спасибо за Ваше время.
Вот код, который поможет вам начать работу. Хранилище ключей
- это объект, содержащий сертификат клиента. Если сервер использует самозаверяющий сертификат или сертификат, который не подписан ЦС, как распознается JVM во включенном файле cacerts, тогда вам нужно будет использовать TrustStore
. В противном случае, чтобы использовать файл cacerts по умолчанию, передайте null
в SSLSockeFactory
для аргумента доверенного хранилища ..
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;
...
final HttpParams httpParams = new BasicHttpParams();
// load the keystore containing the client certificate - keystore type is probably jks or pkcs12
final KeyStore keystore = KeyStore.getInstance("pkcs12");
InputStream keystoreInput = null;
// TODO get the keystore as an InputStream from somewhere
keystore.load(keystoreInput, "keystorepassword".toCharArray());
// load the trustore, leave it null to rely on cacerts distributed with the JVM - truststore type is probably jks or pkcs12
KeyStore truststore = KeyStore.getInstance("pkcs12");
InputStream truststoreInput = null;
// TODO get the trustore as an InputStream from somewhere
truststore.load(truststoreInput, "truststorepassword".toCharArray());
final SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("https", new SSLSocketFactory(keystore, keystorePassword, truststore), 443));
final DefaultHttpClient httpClient = new DefaultHttpClient(new ThreadSafeClientConnManager(httpParams, schemeRegistry), httpParams);