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


Java SSLContextBuilder.loadKeyMaterial方法代碼示例

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


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

示例1: 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

示例2: getHttpClientWithSSL

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public static HttpClient getHttpClientWithSSL(File keyStoreFile, String keyStorePassword, File trustStoreFile, String trustStorePassword) {

        try {
            SSLContextBuilder sslContextBuilder = SSLContexts.custom()
                    .useProtocol("TLS")
                    .loadTrustMaterial(trustStoreFile, trustStorePassword.toCharArray());
            if (keyStoreFile != null) {
                sslContextBuilder.loadKeyMaterial(keyStoreFile, keyStorePassword.toCharArray(), keyStorePassword.toCharArray());
            }
            SSLContext sslContext = sslContextBuilder.build();
            SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);

            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", socketFactory)
                    .build();

            return HttpClientBuilder.create()
                    .setSSLSocketFactory(socketFactory)
                            //.setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
                    .setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                    .setConnectionManager(new PoolingHttpClientConnectionManager(registry))
                    .setSchemePortResolver(new DefaultSchemePortResolver())
                    .build();

        } catch (Exception e) {
            LOGGER.error("Creating HttpClient with customized SSL failed. We are returning the default one instead.", e);
            return HttpClients.createDefault();
        }
    }
 
開發者ID:wildfly,項目名稱:wildfly-core,代碼行數:31,代碼來源:SSLTruststoreUtil.java

示例3: initSslContext

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
private SSLConnectionSocketFactory initSslContext(String keyStoreType, String keyStorePath, String keyStorePassword, String keyPassword,
                                                  String trustStoreType, String trustStorePath, String trustStorePassword)
        throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, UnrecoverableKeyException, KeyManagementException {

    SSLContextBuilder sslContextBuilder = SSLContexts.custom();

    if (StringUtils.isNoneBlank(keyStorePath)) {
        KeyStore keyStore = SslUtils.getStore(keyStoreType, keyStorePath, keyStorePassword);
        if (keyStore.size() == 0) {
            throw new IllegalStateException("Key store has no keys");
        }

        sslContextBuilder.loadKeyMaterial(keyStore, keyPassword.toCharArray());
    }

    if (StringUtils.isNoneBlank(trustStorePath)) {
        KeyStore trustStore = SslUtils.getStore(trustStoreType, trustStorePath, trustStorePassword);
        if (trustStore.size() == 0) {
            throw new IllegalStateException("Trust store has no keys");
        }

        sslContextBuilder.loadTrustMaterial(trustStore, new TrustSelfSignedStrategy());
    }

    return new SSLConnectionSocketFactory(
            sslContextBuilder.build(),
            SSLConnectionSocketFactory.getDefaultHostnameVerifier());
}
 
開發者ID:VKCOM,項目名稱:vk-java-sdk,代碼行數:29,代碼來源:YouTrackClient.java

示例4: socketFactory

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
private SSLConnectionSocketFactory socketFactory() throws Exception {
    char[] password = "password".toCharArray();
    KeyStore truststore = KeyStore.getInstance("PKCS12");
    truststore.load(new ClassPathResource("rod.p12").getInputStream(), password);
    SSLContextBuilder builder = new SSLContextBuilder();
    builder.loadKeyMaterial(truststore, password);
    builder.loadTrustMaterial(truststore, new TrustSelfSignedStrategy());
    return new SSLConnectionSocketFactory(builder.build(),
            new NoopHostnameVerifier());
}
 
開發者ID:livelessons-spring,項目名稱:building-microservices,代碼行數:11,代碼來源:X509ApplicationTests.java

示例5: socketFactory

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
private SSLConnectionSocketFactory socketFactory() throws Exception {
	char[] password = "password".toCharArray();
	KeyStore truststore = KeyStore.getInstance("PKCS12");
	truststore.load(new ClassPathResource("rod.p12").getInputStream(), password);
	SSLContextBuilder builder = new SSLContextBuilder();
	builder.loadKeyMaterial(truststore, password);
	builder.loadTrustMaterial(truststore, new TrustSelfSignedStrategy());
	return new SSLConnectionSocketFactory(builder.build(),
			new NoopHostnameVerifier());
}
 
開發者ID:livelessons-spring,項目名稱:building-microservices,代碼行數:11,代碼來源:BasicHttpsSecurityApplicationTests.java

示例6: build

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
public CloseableHttpClient build() throws Exception {
    HttpClientBuilder builder = HttpClients.custom();
    builder.useSystemProperties();
    builder
            .setDefaultSocketConfig(SocketConfig.custom()
                    .setTcpNoDelay(true)
                    .setSoKeepAlive(true)
                    .build()
            )
            .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE);

    HostnameVerifier hostnameVerifier = sslVerificationMode.verifier();
    TrustStrategy trustStrategy = sslVerificationMode.trustStrategy();
    KeyStore trustStore = agentTruststore();

    SSLContextBuilder sslContextBuilder = SSLContextBuilder.create()
            .useProtocol(systemEnvironment.get(SystemEnvironment.GO_SSL_TRANSPORT_PROTOCOL_TO_BE_USED_BY_AGENT));

    if (trustStore != null || trustStrategy != null) {
        sslContextBuilder.loadTrustMaterial(trustStore, trustStrategy);
    }

    sslContextBuilder.loadKeyMaterial(agentKeystore(), keystorePassword().toCharArray());

    SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContextBuilder.build(), hostnameVerifier);
    builder.setSSLSocketFactory(sslConnectionSocketFactory);
    return builder.build();
}
 
開發者ID:gocd,項目名稱:gocd,代碼行數:29,代碼來源:GoAgentServerHttpClientBuilder.java

示例7: loadKeyMaterial

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
private static SSLContextBuilder loadKeyMaterial(SSLContextBuilder builder, File file, char[] ksp,
        char[] kp, PrivateKeyStrategy privateKeyStrategy) throws NoSuchAlgorithmException,
                KeyStoreException, UnrecoverableKeyException, CertificateException, IOException {
    Args.notNull(file, "Keystore file"); //$NON-NLS-1$
    final KeyStore identityStore = KeyStore.getInstance(KeyStore.getDefaultType());
    final FileInputStream instream = new FileInputStream(file);
    try {
        identityStore.load(instream, ksp);
    } finally {
        instream.close();
    }
    return builder.loadKeyMaterial(identityStore, kp, privateKeyStrategy);
}
 
開發者ID:apiman,項目名稱:apiman,代碼行數:14,代碼來源:SSLSessionStrategyFactory.java

示例8: getHTTPClient

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
protected final CloseableHttpClient getHTTPClient() throws Exception {

		final HttpClientBuilder hcb = HttpClients.custom();

		if (enableHTTPClientSSL) {

			log.debug("Configure HTTP client with SSL");
			
			if(prefix != null && !keystore.contains("/")) {
			    keystore = prefix+"/"+keystore;
			}
			
            final String keyStorePath = FileHelper.getAbsoluteFilePathFromClassPath(keystore).toFile().getParent();
                        
			final KeyStore myTrustStore = KeyStore.getInstance("JKS");
			myTrustStore.load(new FileInputStream(keyStorePath+"/truststore.jks"),
					"changeit".toCharArray());

			final KeyStore keyStore = KeyStore.getInstance("JKS");
			keyStore.load(new FileInputStream(FileHelper.getAbsoluteFilePathFromClassPath(keystore).toFile()), "changeit".toCharArray());

			final SSLContextBuilder sslContextbBuilder = SSLContexts.custom();

			if (trustHTTPServerCertificate) {
				sslContextbBuilder.loadTrustMaterial(myTrustStore, null);
			}

			if (sendHTTPClientCertificate) {
				sslContextbBuilder.loadKeyMaterial(keyStore, "changeit".toCharArray());
			}

			final SSLContext sslContext = sslContextbBuilder.build();

			String[] protocols = null;

			if (enableHTTPClientSSLv3Only) {
				protocols = new String[] { "SSLv3" };
			} else {
				protocols = new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" };
			}

			final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
			        sslContext, 
			        protocols, 
			        null,
					NoopHostnameVerifier.INSTANCE);

			hcb.setSSLSocketFactory(sslsf);
		}

		hcb.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(60 * 1000).build());

		return hcb.build();
	}
 
開發者ID:floragunncom,項目名稱:search-guard,代碼行數:55,代碼來源:RestHelper.java

示例9: makeGetRequest

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
private int makeGetRequest(String url, String clientKeystoreLocation, String clientKeystorePassword,
                           String clientKeyPassword)
    throws Exception {
  log.debug("Making GET " + url);
  HttpGet httpget = new HttpGet(url);
  CloseableHttpClient httpclient;
  if (url.startsWith("http://")) {
    httpclient = HttpClients.createDefault();
  } else {
    // trust all self-signed certs.
    SSLContextBuilder sslContextBuilder = SSLContexts.custom()
            .loadTrustMaterial(new TrustSelfSignedStrategy());

    // add the client keystore if it's configured.
    if (clientKeystoreLocation != null) {
      sslContextBuilder.loadKeyMaterial(new File(clientKeystoreLocation),
              clientKeystorePassword.toCharArray(),
              clientKeyPassword.toCharArray());
    }
    SSLContext sslContext = sslContextBuilder.build();

    SSLConnectionSocketFactory sslSf = new SSLConnectionSocketFactory(sslContext, new String[]{"TLSv1"},
            null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());

    httpclient = HttpClients.custom()
            .setSSLSocketFactory(sslSf)
            .build();
  }

  int statusCode = -1;
  CloseableHttpResponse response = null;
  try {
    response = httpclient.execute(httpget);
    statusCode = response.getStatusLine().getStatusCode();
  } finally {
    if (response != null) {
      response.close();
    }
    httpclient.close();
  }
  return statusCode;
}
 
開發者ID:confluentinc,項目名稱:rest-utils,代碼行數:43,代碼來源:SslTest.java

示例10: getHTTPClient

import org.apache.http.ssl.SSLContextBuilder; //導入方法依賴的package包/類
protected final CloseableHttpClient getHTTPClient() throws Exception {

        final HttpClientBuilder hcb = HttpClients.custom();

        if (enableHTTPClientSSL) {

            log.debug("Configure HTTP client with SSL");

            final KeyStore myTrustStore = KeyStore.getInstance("JKS");
            myTrustStore.load(new FileInputStream(getAbsoluteFilePathFromClassPath("truststore.jks").toFile()), "changeit".toCharArray());

            final KeyStore keyStore = KeyStore.getInstance("JKS");
            keyStore.load(new FileInputStream(getAbsoluteFilePathFromClassPath(keystore).toFile()), "changeit".toCharArray());

            final SSLContextBuilder sslContextbBuilder = SSLContexts.custom().useProtocol("TLS");

            if (trustHTTPServerCertificate) {
                sslContextbBuilder.loadTrustMaterial(myTrustStore, null);
            }

            if (sendHTTPClientCertificate) {
                sslContextbBuilder.loadKeyMaterial(keyStore, "changeit".toCharArray());
            }
            
            final SSLContext sslContext = sslContextbBuilder.build();

            String[] protocols = null;

            if (enableHTTPClientSSLv3Only) {
                protocols = new String[] { "SSLv3" };
            } else {
                protocols = new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" };
            }
            
            final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, protocols, null, NoopHostnameVerifier.INSTANCE);

            hcb.setSSLSocketFactory(sslsf);
        }

        hcb.setDefaultSocketConfig(SocketConfig.custom().setSoTimeout(60 * 1000).build());

        return hcb.build();
    }
 
開發者ID:floragunncom,項目名稱:search-guard-ssl,代碼行數:44,代碼來源:AbstractUnitTest.java


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