當前位置: 首頁>>代碼示例>>Java>>正文


Java SSLContextBuilder.build方法代碼示例

本文整理匯總了Java中org.apache.http.ssl.SSLContextBuilder.build方法的典型用法代碼示例。如果您正苦於以下問題:Java SSLContextBuilder.build方法的具體用法?Java SSLContextBuilder.build怎麽用?Java SSLContextBuilder.build使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在org.apache.http.ssl.SSLContextBuilder的用法示例。


在下文中一共展示了SSLContextBuilder.build方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: init

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public void init() {
	try {
		SSLContextBuilder builder = new SSLContextBuilder();
		builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
		SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
		/* 配置同時支持 HTTP 和 HTPPS */
		Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
				.register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslsf).build();
		/* 初始化連接管理器 */
		poolConnManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
		poolConnManager.setMaxTotal(maxTotal);
		poolConnManager.setDefaultMaxPerRoute(defaultMaxPerRoute);
		requestConfig = RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout)
				.setSocketTimeout(socketTimeout).setConnectTimeout(connectTimeout).build();
		httpClient = getConnection();
		log.info("HttpConnectionManager初始化完成...");
	} catch (Exception e) {
		log.error("error", e);
	}
}
 
開發者ID:yunjiweidian,項目名稱:TITAN,代碼行數:21,代碼來源:HttpConnectionManager.java

示例2: createAllTrustingClient

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
private static HttpClient createAllTrustingClient() throws RestClientException {
	HttpClient httpclient = null;
	try {
		final SSLContextBuilder builder = new SSLContextBuilder();
		builder.loadTrustMaterial((TrustStrategy) (X509Certificate[] chain, String authType) -> true);
		final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
				builder.build());
		httpclient = HttpClients
				.custom()
				.setSSLSocketFactory(sslsf)
				.setMaxConnTotal(1000)
				.setMaxConnPerRoute(1000)
				.build();
	} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
		throw new RestClientException("Cannot create all SSL certificates trusting client", e);
	}
	return httpclient;
}
 
開發者ID:syndesisio,項目名稱:syndesis-qe,代碼行數:19,代碼來源:RestUtils.java

示例3: triggerHttpGetWithCustomSSL

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public static String triggerHttpGetWithCustomSSL(String requestUrl) {
	String result = null;
	try {
		SSLContextBuilder builder = new SSLContextBuilder();
		builder.loadTrustMaterial(null, new TrustSelfSignedStrategy() {
			public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
				return true;
			}
		});
		@SuppressWarnings("deprecation")
		HostnameVerifier hostnameVerifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;

		SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), hostnameVerifier);

		CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();

		HttpGet httpGet = new HttpGet("https://" + requestUrl);
		CloseableHttpResponse response = httpclient.execute(httpGet);
		try {
			if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
				HttpEntity entity = response.getEntity();
				result = EntityUtils.toString(entity);
				log.debug("Received response: " + result);
			} else {
				log.error("Request not successful. StatusCode was " + response.getStatusLine().getStatusCode());
			}
		} finally {
			response.close();
		}
	} catch (IOException | KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
		log.error("Error executing the request.", e);
	}
	return result;
}
 
開發者ID:hotstepper13,項目名稱:alexa-gira-bridge,代碼行數:35,代碼來源:Util.java

示例4: init

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public void init()
{
  try
  {
    SSLContextBuilder builder = new SSLContextBuilder();
    builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build(), SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    Unirest.setHttpClient(httpclient);
  }
  catch (Exception e)
  {
    System.out.println("Failed to start server: " + e.toString());
    e.printStackTrace();
  }
}
 
開發者ID:Malow,項目名稱:GladiatorManager,代碼行數:17,代碼來源:ServerConnection.java

示例5: buildHttpClient

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
/**
 * This creates a HTTP client instance for connecting the IFTTT server.
 * 
 * @return the HTTP client instance
 */
private CloseableHttpClient buildHttpClient ()
{
    if ( configuration.isIftttIgnoreServerCertificate() ) {
        try {
            SSLContextBuilder builder = new SSLContextBuilder();
            builder.loadTrustMaterial(new TrustStrategy() {
                @Override
                public boolean isTrusted (X509Certificate[] chain_, String authType_) throws CertificateException
                {
                    return true;
                }
            });
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        }
        catch (Exception ex) {
            LOG.error(ex);
            // This should never happen, but we have to handle it
            throw new RuntimeException(ex);
        }
    }
    else {
        return HttpClients.createDefault();
    }
}
 
開發者ID:zazaz-de,項目名稱:iot-device-bosch-indego-controller,代碼行數:31,代碼來源:IftttIndegoAdapter.java

示例6: getHttpsClient

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
private CloseableHttpClient getHttpsClient()
{
    try
    {
        RequestConfig config = RequestConfig.custom().setSocketTimeout( 5000 ).setConnectTimeout( 5000 ).build();

        SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
        sslContextBuilder.loadTrustMaterial( null, ( TrustStrategy ) ( x509Certificates, s ) -> true );
        SSLConnectionSocketFactory sslSocketFactory =
                new SSLConnectionSocketFactory( sslContextBuilder.build(), NoopHostnameVerifier.INSTANCE );

        return HttpClients.custom().setDefaultRequestConfig( config ).setSSLSocketFactory( sslSocketFactory )
                          .build();
    }
    catch ( Exception e )
    {
        LOGGER.error( e.getMessage() );
    }

    return HttpClients.createDefault();
}
 
開發者ID:subutai-io,項目名稱:base,代碼行數:22,代碼來源:RestServiceImpl.java

示例7: configureInsecureSSL

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
/**
 * Allow SSL connections to utilize untrusted certificates
 * 
 * @param httpClientBuilder
 * @throws MojoExecutionException
 */
private void configureInsecureSSL(HttpClientBuilder httpClientBuilder) throws MojoExecutionException {
    try {
        SSLContextBuilder sslBuilder = new SSLContextBuilder();

        // Accept all Certificates
        sslBuilder.loadTrustMaterial(null, AcceptAllTrustStrategy.INSTANCE);

        SSLContext sslContext = sslBuilder.build();

        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
                NoopHostnameVerifier.INSTANCE);
        httpClientBuilder.setSSLSocketFactory(sslsf);
    } catch (Exception e) {
        throw new MojoExecutionException("Error setting insecure SSL configuration", e);
    }
}
 
開發者ID:sabre1041,項目名稱:jenkinsfile-maven-plugin,代碼行數:23,代碼來源:ValidateJenkinsfileMojo.java

示例8: HttpPosterThread

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
HttpPosterThread(LinkedBlockingQueue<String> lbq, String url) throws Exception {
    this.lbq = lbq;
    this.url = url;

    SSLContextBuilder builder = new SSLContextBuilder();
    builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
            builder.build());
    httpClient = HttpClients.custom().setSSLSocketFactory(
            sslsf).build();

    //httpClient = HttpClientBuilder.create().build();
    httpPost = new HttpPost(url);

    cntErr = 0;
}
 
開發者ID:david618,項目名稱:Simulator,代碼行數:17,代碼來源:HttpPosterThread.java

示例9: createInsecureSslFactory

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
/**
 * This method creates an insecure SSL factory that will trust on self signed certificates.
 * For that we use {@link TrustSelfSignedStrategy}.
 */
protected SSLConnectionSocketFactory createInsecureSslFactory() {
    try {
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(new TrustSelfSignedStrategy());
        SSLContext sc = builder.build();

        if (acceptAllKindsOfCertificates) {
            TrustManager[] trustAllCerts = new TrustManager[1];
            TrustManager tm = new TrustAllManager();
            trustAllCerts[0] = tm;
            sc.init(null, trustAllCerts, null);

            HostnameVerifier hostnameVerifier = createInsecureHostNameVerifier();
            return new SSLConnectionSocketFactory(sc, hostnameVerifier);
        }
        return new SSLConnectionSocketFactory(sc);
    } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
        throw new ApacheCloudStackClientRuntimeException(e);
    }
}
 
開發者ID:Autonomiccs,項目名稱:apache-cloudstack-java-client,代碼行數:25,代碼來源:ApacheCloudStackClient.java

示例10: TankHttpClient4

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
/**
 * no-arg constructor for client
 */
public TankHttpClient4() {
    try {
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
        sslsf = new SSLConnectionSocketFactory(builder.build(), NoopHostnameVerifier.INSTANCE);
    } catch (Exception e) {
        LOG.error("Error setting accept all: " + e, e);
    }

    httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    requestConfig = RequestConfig.custom().setSocketTimeout(30000)
    		.setConnectTimeout(30000)
    		.setCircularRedirectsAllowed(true)
    		.setAuthenticationEnabled(true)
    		.setRedirectsEnabled(true)
    		.setCookieSpec(CookieSpecs.STANDARD)
            .setMaxRedirects(100).build();

    // Make sure the same context is used to execute logically related
    // requests
    context = HttpClientContext.create();
    context.setCredentialsProvider(new BasicCredentialsProvider());
    context.setCookieStore(new BasicCookieStore());
    context.setRequestConfig(requestConfig);
}
 
開發者ID:intuit,項目名稱:Tank,代碼行數:29,代碼來源:TankHttpClient4.java

示例11: getHttpClient

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
private HttpClient getHttpClient() {
    CloseableHttpClient httpclient = null;

    try {
        KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(keyStore, (TrustStrategy) (trustedCert, nameConstraints) -> true);

        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(builder.build());
        httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
    } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
        _logger.error("Failed to create HTTPClient: {}", e);
    }

    return httpclient;
}
 
開發者ID:hawkular,項目名稱:hawkular-client-java,代碼行數:17,代碼來源:RestFactory.java

示例12: getAuthenticatedClient

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public CloseableHttpClient getAuthenticatedClient() {
    try {
        SSLContextBuilder builder = new SSLContextBuilder();
        builder.loadTrustMaterial(new TrustSelfSignedStrategy());
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                builder.build(), NoopHostnameVerifier.INSTANCE);

        return HttpClients.custom()
                .setSSLSocketFactory(sslsf)
                .setDefaultCredentialsProvider(getCredentialsProvider())
                .build();
    } catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex) {
        Logger.getLogger(AuthHandler.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}
 
開發者ID:Adobe-Consulting-Services,項目名稱:curly,代碼行數:17,代碼來源:AuthHandler.java

示例13: createSslCustomContext

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
private SSLContext createSslCustomContext() {
    try {
        SSLContextBuilder builder = SSLContexts.custom();
        if (options.getKeystore().isPresent()) {
            KeyStore cks = KeyStore.getInstance(KeyStore.getDefaultType());
            cks.load(new FileInputStream(options.getKeystore().get().toFile()), options.getKeystorePass().toCharArray());
            builder.loadKeyMaterial(cks, options.getKeystorePass().toCharArray());
        }

        if (options.getTruststore().isPresent()) {
            KeyStore tks = KeyStore.getInstance(KeyStore.getDefaultType());
            tks.load(new FileInputStream(options.getTruststore().get().toFile()), options.getTruststorePass().toCharArray());
            builder.loadTrustMaterial(tks, new TrustSelfSignedStrategy());
        }

        if (!options.getKeystore().isPresent() && !options.getKeystore().isPresent()) {
            return SSLContext.getDefault();
        }

        return builder.build();
    } catch (Exception e) {
        // TODO: DO SOMETHING WITH THE EXCEPTION!
        LOG.error("Exception", e);
    }
    return null;
}
 
開發者ID:ULYSSIS-KUL,項目名稱:ipp,代碼行數:27,代碼來源:HttpOutput.java

示例14: trustSelfSignedCerts

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
/**
 * Adds the ability to trust self signed certificates for this HttpClientBuilder
 * 
 * @param httpClientBuilder
 * the HttpClientBuilder to apply these settings to
 */
public static void trustSelfSignedCerts(final HttpClientBuilder httpClientBuilder)
{
	logger.debug("Trusting self-signed certs.");
	try
	{
		SSLContextBuilder builder = new SSLContextBuilder();
		builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
		SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
			builder.build(), new HostnameVerifier()
			{
				@Override
				public boolean verify(String hostname, SSLSession session)
				{
					// allow all
					return true;
				}
			});
			
		httpClientBuilder.setSSLSocketFactory(sslsf);
	}
	catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException ex)
	{
		logger.error("Error adding SSLSocketFactory to HttpClientBuilder", ex);
	}
}
 
開發者ID:ThreatConnect-Inc,項目名稱:threatconnect-java,代碼行數:32,代碼來源:ConnectionUtil.java

示例15: getFactoryDisabledSslChecks

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
/**
 * WARNING!!! disabling is not a good idea.
 * 
 * Support hardcode for ps team
 * 
 * Their server is pretty busted. -
 * http://stackoverflow.com/questions/7615645
 * /ssl-handshake-alert-unrecognized-name-error-since-upgrade-to-java-1-7-0
 * -- -Djsse.enableSNIExtension=false very ugly workaround
 * 
 * @param restTemplate
 * @throws Exception
 */
public static HttpComponentsClientHttpRequestFactory getFactoryDisabledSslChecks(
		int connectTimeoutMs, int readTimeoutMs) throws Exception {

	SSLContextBuilder builder = new SSLContextBuilder();
	builder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
	// builder.loadTrustMaterial(null, new TrustStrategy() {
	//
	// @Override
	// public boolean isTrusted(X509Certificate[] chain, String authType)
	// throws CertificateException {
	//
	// return true;
	// }
	// });

	SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
			builder.build(), new NoopHostnameVerifier());

	CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(
			sslsf).build();

	HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
	factory.setHttpClient(httpClient);
	// factory.getHttpClient().getConnectionManager().getSchemeRegistry().register(scheme);

	factory.setConnectTimeout(connectTimeoutMs);
	factory.setReadTimeout(readTimeoutMs);

	// restTemplate.setRequestFactory(factory);
	return factory;
}
 
開發者ID:csap-platform,項目名稱:csap-core,代碼行數:45,代碼來源:NagiosIntegration.java


注:本文中的org.apache.http.ssl.SSLContextBuilder.build方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。