本文整理匯總了Java中org.apache.http.conn.ssl.SSLContextBuilder.loadTrustMaterial方法的典型用法代碼示例。如果您正苦於以下問題:Java SSLContextBuilder.loadTrustMaterial方法的具體用法?Java SSLContextBuilder.loadTrustMaterial怎麽用?Java SSLContextBuilder.loadTrustMaterial使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.apache.http.conn.ssl.SSLContextBuilder
的用法示例。
在下文中一共展示了SSLContextBuilder.loadTrustMaterial方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createSSLConnectionSocketFactory
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
private static SSLConnectionSocketFactory createSSLConnectionSocketFactory(Path path, char[] password, boolean strict, KeystoreType keystoreType) {
try {
SSLContextBuilder builder = SSLContexts.custom();
if (path != null) {
KeyStore trustStore = KeyStore.getInstance(keystoreType.name());
try (InputStream is = Files.newInputStream(path)) {
trustStore.load(is, password);
}
builder.loadTrustMaterial(trustStore);
} else {
builder.loadTrustMaterial(null, new TrustEverythingStrategy());
}
X509HostnameVerifier verifier;
if (strict) {
verifier = SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER;
} else {
verifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
}
return new SSLConnectionSocketFactory(builder.build(), new String[] {"TLSv1", "TLSv1.2"}, null, verifier);
} catch (IOException | GeneralSecurityException ex) {
throw new RuntimeException("Can't create SSL connection factory", ex);
}
}
示例2: getInstance
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public static SSLConnectionSocketFactory getInstance() {
if (sslConnectionSocketFactory == null) {
synchronized (ConnectionManagerFactory.class) {
if (sslConnectionSocketFactory == null) {
try{
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
sslConnectionSocketFactory = new SSLConnectionSocketFactory(builder.build());
}catch(Exception exception){
exception.getStackTrace();
}
}
}
}
return sslConnectionSocketFactory;
}
示例3: getSSLSocketFactory
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
private SSLConnectionSocketFactory getSSLSocketFactory() {
KeyStore trustStore;
try {
trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustStore.load(null, null);
TrustStrategy trustStrategy = new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
};
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.loadTrustMaterial(trustStore, trustStrategy);
sslContextBuilder.useTLS();
SSLContext sslContext = sslContextBuilder.build();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext);
return sslSocketFactory;
} catch (GeneralSecurityException | IOException e) {
System.err.println("SSL Error : " + e.getMessage());
}
return null;
}
示例4: createSslHttpClient
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
private CloseableHttpClient createSslHttpClient() throws Exception {
final SSLContextBuilder wsBuilder = new SSLContextBuilder();
wsBuilder.loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] chain,
String authType) throws CertificateException {
return true;
}
});
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(wsBuilder.build(),
new AllowAllHostnameVerifier());
//This winds up using a PoolingHttpClientConnectionManager so need to pass the
//RegistryBuilder
final Registry<ConnectionSocketFactory> registry = RegistryBuilder
.<ConnectionSocketFactory> create().register("https", sslsf)
.build();
final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
return HttpClients
.custom()
.setConnectionManager(cm)
.build();
}
示例5: createHttpClient
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public CloseableHttpClient createHttpClient(boolean allowSelfSigned)
throws IOException {
CloseableHttpClient httpclient = null;
if (allowSelfSigned) {
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build(),
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
httpclient = HttpClients.custom().setSSLSocketFactory(sslsf)
.setDefaultCookieStore(cookieStore).build();
} catch (Exception e) {
throw new IOException(e);
}
} else {
httpclient = HttpClients.custom()
.setDefaultCookieStore(cookieStore).build();
}
return httpclient;
}
示例6: RestClient
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public RestClient() throws Exception {
SSLContextBuilder builder = new SSLContextBuilder();
SSLConnectionSocketFactory sslConnectionFactory;
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
sslConnectionFactory = new SSLConnectionSocketFactory(builder.build());
this.httpClient = HttpClients.custom().setSSLSocketFactory(sslConnectionFactory)
.setConnectionManager(HTTPConnectionManager.getInstance().getHttpConnectionManager()).build();
}
示例7: RestClient
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public RestClient(String baseUrl) throws Exception {
this.baseUrl = baseUrl;
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
this.httpclient = HttpClients.custom().setUserAgent("r2cloud/0.1 [email protected]").setSSLSocketFactory(sslsf).build();
}
示例8: createUnsafeClient
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public static HttpClient createUnsafeClient() {
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build());
return HttpClients.custom()
.setSSLSocketFactory(sslsf).build();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例9: setClientToSelfSigned
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
/**
* Create a HTTP client that uses a self-signed and always trusted
* SSL strategy.
*
* @param custom The client builder
* @return builder with unsafe SSL strategy
* @throws APIException If there is a problem creating the client or strategy
*/
private HttpClientBuilder setClientToSelfSigned( final HttpClientBuilder custom ) throws APIException
{
final SSLContextBuilder builder = new SSLContextBuilder();
try {
builder.loadTrustMaterial( null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sf = new SSLConnectionSocketFactory( builder.build());
return custom.setSSLSocketFactory( sf );
} catch( NoSuchAlgorithmException | KeyStoreException | KeyManagementException e ) {
throw new APIException( "Failed to create self-signed trust strategy and/or SSL-enabled HTTP Client", e );
}
}
示例10: init
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public static void init() throws RuntimeException {
try {
logger.warn(NOTICELINE + " httpUtil init begin " + NOTICELINE);
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
// sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
sslContextBuilder.loadTrustMaterial(null,new TrustAnyTrustManager());
SSLConnectionSocketFactory sslConnectionSocketFactory =
new SSLConnectionSocketFactory(
sslContextBuilder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().
register("http", new PlainConnectionSocketFactory()).
register("https", sslConnectionSocketFactory).
build();
logger.warn(NOTICELINE + " SSL context init done " + NOTICELINE);
//init connectionManager , ThreadSafe pooled conMgr
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(registry);
poolingHttpClientConnectionManager.setMaxTotal(30);
poolingHttpClientConnectionManager.setDefaultMaxPerRoute(3);
//init request config. pooltimeout,sotime,contimeout
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(POOL_TIMECOUT).setConnectTimeout(CON_TIMEOUT).setSocketTimeout(SO_TIMEOUT).build();
// begin construct httpclient
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setConnectionManager(poolingHttpClientConnectionManager);
httpClientBuilder.setDefaultRequestConfig(requestConfig);
httpClientBuilder.setRetryHandler(new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
if (executionCount >= HTTP_RETRY_COUNT) {
return false;
}
if (exception instanceof InterruptedIOException) {
// Timeout
logger.warn("httpUtil retry for InterruptIOException");
return true;
}
if (exception instanceof UnknownHostException) {
// Unknown host
return false;
}
if (exception instanceof SSLException) {
// SSL handshake exception
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
boolean idempotent = !(request instanceof HttpEntityEnclosingRequest);
if (idempotent) {
// Retry if the request is considered idempotent
logger.warn("httpUtil retry for idempotent");
return true;
}
return false;
}
});
logger.warn(NOTICELINE + " poolManager , requestconfig init done " + NOTICELINE);
httpclient = httpClientBuilder.build();
logger.warn(NOTICELINE + " httpUtil init done " + NOTICELINE);
} catch (Exception e) {
logger.error(NOTICELINE + "httpclient init fail" + NOTICELINE, e);
throw new RuntimeException(e);
}
}
示例11: createUnsafeClient
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public static HttpClient createUnsafeClient() {
try {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
builder.build());
CloseableHttpClient httpclient = HttpClients.custom()
.setSSLSocketFactory(sslsf).build();
return httpclient;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例12: createHttpClient
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
private static CloseableHttpClient createHttpClient() {
try {
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslSocketFactory =
new SSLConnectionSocketFactory(sslContextBuilder.build(),
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
return HttpClientBuilder.create().setSSLSocketFactory(sslSocketFactory).build();
} catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
throw new RuntimeException(e);
}
}
示例13: createHttpClientThatAcceptsAllServerCerts
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
private static CloseableHttpClient createHttpClientThatAcceptsAllServerCerts()
throws NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
final SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, (chain, authType) -> true);
final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}
示例14: getHttpClient
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
/**
* Return a http client instance
*
* @param protocol- service endpoint protocol http/https
* @return
*/
public static HttpClient getHttpClient(String protocol)
throws IOException, KeyStoreException, NoSuchAlgorithmException, KeyManagementException {
HttpClient httpclient;
if (HTTPS_PROTOCOL.equals(protocol)) {
SSLContextBuilder builder = new SSLContextBuilder();
builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
} else {
httpclient = HttpClients.createDefault();
}
return httpclient;
}
示例15: createSslContext
import org.apache.http.conn.ssl.SSLContextBuilder; //導入方法依賴的package包/類
protected SSLContext createSslContext() {
try {
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
sslContextBuilder.loadTrustMaterial(null, ACCEPT_ANY_CERTIFICATE_TRUST_STRATEGY);
return sslContextBuilder.build();
} catch (KeyManagementException | KeyStoreException | NoSuchAlgorithmException e) {
throw new IllegalStateException(e);
}
}