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


Java SSLContexts類代碼示例

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


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

示例1: createSSLConnectionSocketFactory

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的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);
	}
}
 
開發者ID:xtf-cz,項目名稱:xtf,代碼行數:27,代碼來源:HttpClient.java

示例2: createRestTemplate

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的package包/類
private static RestTemplate createRestTemplate(String host, String username, String password, Set<ClientHttpRequestInterceptor> interceptors) throws GeneralSecurityException {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(host, 25555),
        new UsernamePasswordCredentials(username, password));

    SSLContext sslContext = SSLContexts.custom()
        .loadTrustMaterial(null, new TrustSelfSignedStrategy())
        .useTLS()
        .build();

    SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier());

    HttpClient httpClient = HttpClientBuilder.create()
        .disableRedirectHandling()
        .setDefaultCredentialsProvider(credentialsProvider)
        .setSSLSocketFactory(connectionFactory)
        .build();

    RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
    restTemplate.getInterceptors().addAll(interceptors);

    return restTemplate;
}
 
開發者ID:strepsirrhini-army,項目名稱:chaos-lemur,代碼行數:24,代碼來源:StandardDirectorUtils.java

示例3: getSslFactoryRegistry

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的package包/類
private static Registry<ConnectionSocketFactory> getSslFactoryRegistry(String certPath) throws IOException {
    try {
        KeyStore keyStore = KeyStoreUtils.createDockerKeyStore(certPath);

        SSLContext sslContext =
                SSLContexts.custom()
                        .useTLS()
                        .loadKeyMaterial(keyStore, "docker".toCharArray())
                        .loadTrustMaterial(keyStore)
                        .build();

        SSLConnectionSocketFactory sslsf =

                new SSLConnectionSocketFactory(sslContext);
        return RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslsf).build();
    } catch (GeneralSecurityException e) {
        throw new IOException(e);
    }
}
 
開發者ID:oncecloud,項目名稱:devops-cstack,代碼行數:20,代碼來源:JSONClient.java

示例4: getTrustedSslContext

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的package包/類
/**
 * Gets the trusted ssl context.
 *
 * @param trustStoreFile the trust store file
 * @param trustStorePassword the trust store password
 * @param trustStoreType the trust store type
 * @return the trusted ssl context
 */
private static SSLContext getTrustedSslContext(final File trustStoreFile, final String trustStorePassword,
                                        final String trustStoreType) {
    try {

        if (!trustStoreFile.exists() || !trustStoreFile.canRead()) {
            throw new FileNotFoundException("Truststore file cannot be located at "
                + trustStoreFile.getCanonicalPath());
        }

        final KeyStore casTrustStore = KeyStore.getInstance(trustStoreType);
        final char[] trustStorePasswordCharArray = trustStorePassword.toCharArray();

        try (final FileInputStream casStream = new FileInputStream(trustStoreFile)) {
            casTrustStore.load(casStream, trustStorePasswordCharArray);
        }

        final String defaultAlgorithm = KeyManagerFactory.getDefaultAlgorithm();
        final X509KeyManager customKeyManager = getKeyManager("PKIX", casTrustStore, trustStorePasswordCharArray);
        final X509KeyManager jvmKeyManager = getKeyManager(defaultAlgorithm, null, null);
        final X509TrustManager customTrustManager = getTrustManager("PKIX", casTrustStore);
        final X509TrustManager jvmTrustManager = getTrustManager(defaultAlgorithm, null);

        final KeyManager[] keyManagers = {
                new CompositeX509KeyManager(Arrays.asList(jvmKeyManager, customKeyManager))
        };
        final TrustManager[] trustManagers = {
                new CompositeX509TrustManager(Arrays.asList(jvmTrustManager, customTrustManager))
        };

        final SSLContext context = SSLContexts.custom().useSSL().build();
        context.init(keyManagers, trustManagers, null);
        return context;

    } catch (final Exception e) {
        LOGGER.error(e.getMessage(), e);
        throw new RuntimeException(e);
    }
}
 
開發者ID:yuweijun,項目名稱:cas-server-4.2.1,代碼行數:47,代碼來源:FileTrustStoreSslSocketFactory.java

示例5: createHttpClientWithDisabledSSLCheck

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的package包/類
private CloseableHttpClient createHttpClientWithDisabledSSLCheck(){
    SSLContext sslcontext = null;
       try {
           sslcontext = SSLContexts.custom()
                   .loadTrustMaterial(null, new TrustStrategy(){

                       @Override
                       public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                           return true;
                       }
                       
                   })
                   .build();
       } catch (Exception e) {
           throw new RuntimeException("SSL Context can not be created.", e);
       }

       SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext);
       return HttpClients.custom()
                        .setSSLSocketFactory(sslsf)
                        .build();
}
 
開發者ID:automate-website,項目名稱:manager-api-client,代碼行數:23,代碼來源:RestTemplate.java

示例6: createSSLSocketFactory

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的package包/類
private SSLSocketFactory createSSLSocketFactory(boolean useKeyStoreToConnect, String keyStorePath,
                                                String keyStorePassword) throws Exception {

    //Only load KeyStore when it's needed to connect to IP, SSLContext is fine with KeyStore being null otherwise.
    KeyStore trustStore = null;
    if (useKeyStoreToConnect) {
        trustStore = KeyStoreLoader.loadKeyStore(keyStorePath, keyStorePassword);
    }

    SSLContext sslContext = SSLContexts.custom()
            .useSSL()
            .loadTrustMaterial(trustStore, new TrustStrategy() {
                //Always trust
                @Override
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            })
            .loadKeyMaterial(trustStore, keyStorePassword.toCharArray())
            .setSecureRandom(new java.security.SecureRandom())
            .build();

    return sslContext.getSocketFactory();
}
 
開發者ID:Microsoft,項目名稱:Availability-Monitor-for-Kafka,代碼行數:25,代碼來源:Producer.java

示例7: ceateSSLClient

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的package包/類
public static CloseableHttpClient ceateSSLClient(File keyFile, String protocol, String password){
	CloseableHttpClient httpclient = null;
	try{
		KeyStore keyStore  = KeyStore.getInstance("PKCS12");
        FileInputStream instream = new FileInputStream(keyFile);
        try {
            keyStore.load(instream, password.toCharArray());
        } finally {
            instream.close();
        }
        // Trust own CA and all self-signed certs
        SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, password.toCharArray()).build();
        // Allow TLSv1 protocol only
        String[] protocols = new String[] {protocol};
        SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext,protocols,null,SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
        httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
	}catch(Exception e){
		e.printStackTrace();
	}
	return httpclient;
}
 
開發者ID:anylineorg,項目名稱:anyline,代碼行數:22,代碼來源:HttpClientUtil.java

示例8: getSSLConnectionSocketFactory

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的package包/類
/**
     * 
    		*@name 獲取ssl鏈接
    		*@Description  
    		*@CreateDate 2015年12月31日下午2:34:40
     */
    public static SSLConnectionSocketFactory getSSLConnectionSocketFactory(String keyStoreType,String certFilePath,String certPassword) throws Exception{
    	KeyStore keyStore  = KeyStore.getInstance(keyStoreType);
//        FileInputStream instream = new FileInputStream(new File(certFilePath));
    	InputStream instream = null;
        try {
        	instream = new ClassPathResource(certFilePath).getInputStream();
            keyStore.load(instream, certPassword.toCharArray());
        } 
        finally {
            instream.close();
        }

        SSLContext sslContext = SSLContexts.custom()
                		  				   .loadKeyMaterial(keyStore, certPassword.toCharArray())
                		  				   .build();
        
        return new SSLConnectionSocketFactory(sslContext,new String[] { "TLSv1" },null,
                	SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    }
 
開發者ID:yinshipeng,項目名稱:sosoapi-base,代碼行數:26,代碼來源:HttpClientUtils.java

示例9: createConnectionRegistry

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的package包/類
protected Registry<ConnectionSocketFactory> createConnectionRegistry(X509HostnameVerifier x509HostnameVerifier, SSLContextParameters sslContextParams)
    throws GeneralSecurityException, IOException {
    // create the default connection registry to use
    RegistryBuilder<ConnectionSocketFactory> builder = RegistryBuilder.<ConnectionSocketFactory>create();
    builder.register("http", PlainConnectionSocketFactory.getSocketFactory());
    builder.register("http4", PlainConnectionSocketFactory.getSocketFactory());
    if (sslContextParams != null) {
        builder.register("https", new SSLConnectionSocketFactory(sslContextParams.createSSLContext(getCamelContext()), x509HostnameVerifier));
        builder.register("https4", new SSLConnectionSocketFactory(sslContextParams.createSSLContext(getCamelContext()), x509HostnameVerifier));
    } else {
        builder.register("https4", new SSLConnectionSocketFactory(SSLContexts.createDefault(), x509HostnameVerifier));
        builder.register("https", new SSLConnectionSocketFactory(SSLContexts.createDefault(), x509HostnameVerifier));
    }
    return builder.build();
}
 
開發者ID:HydAu,項目名稱:Camel,代碼行數:16,代碼來源:HttpComponent.java

示例10: initBase

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的package包/類
protected void initBase() {
       SSLContext sslContext = SSLContexts.createSystemDefault();
       SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
               sslContext,
               SSLConnectionSocketFactory.STRICT_HOSTNAME_VERIFIER);

       httpRequestConfig = RequestConfig.custom()
               .setSocketTimeout(TIMEOUT)
               .setConnectTimeout(TIMEOUT)
               .setCookieSpec(CookieSpecs.BEST_MATCH)
               .build();

       cookieStore = new BasicCookieStore();

       httpClient = HttpClientBuilder.create()
           .setSSLSocketFactory(sslsf)
           .setDefaultCookieStore(cookieStore)
           .setDefaultRequestConfig(httpRequestConfig)
               .build();


	localContext = HttpClientContext.create();

}
 
開發者ID:Naiqus,項目名稱:E1zone_Android_Client,代碼行數:25,代碼來源:MyWebClient.java

示例11: createRequestFactory

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的package包/類
private ClientHttpRequestFactory createRequestFactory(String host, String username,
        String password) {
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(host, 25555),
            new UsernamePasswordCredentials(username, password));

    SSLContext sslContext = null;
    try {
        sslContext = SSLContexts.custom()
                .loadTrustMaterial(null, new TrustSelfSignedStrategy()).useTLS().build();
    } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException e) {
        throw new DirectorException("Unable to configure ClientHttpRequestFactory", e);
    }

    SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext,
            new AllowAllHostnameVerifier());

    // disabling redirect handling is critical for the way BOSH uses 302's
    HttpClient httpClient = HttpClientBuilder.create().disableRedirectHandling()
            .setDefaultCredentialsProvider(credentialsProvider)
            .setSSLSocketFactory(connectionFactory).build();

    return new HttpComponentsClientHttpRequestFactory(httpClient);
}
 
開發者ID:davidehringer,項目名稱:bosh-java-client,代碼行數:25,代碼來源:SpringDirectorClientBuilder.java

示例12: initSSLContext

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的package包/類
/**
 * 初始化SSL證書
 *
 * @return
 * @throws PaymentException
 */
private SSLContext initSSLContext() throws PaymentException {
    // 初始化證書, 證書位置為classes目錄
    SSLContext sslContext = null;
    try {
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        FileInputStream inputStream = new FileInputStream(new File(wxConfig.getMchPKCs()));
        keyStore.load(inputStream, wxConfig.getMchid().toCharArray());
        // Trust own CA and all self-signed certs
        sslContext = SSLContexts.custom()
                .loadKeyMaterial(keyStore, wxConfig.getMchid().toCharArray())
                .build();
    } catch (Exception e) {
        throw new PaymentException("在進行退款時發生了證書初始化錯誤.", e);
    }

    return sslContext;
}
 
開發者ID:coffeefoam,項目名稱:wechat-dev-framework,代碼行數:24,代碼來源:PaymentCapability.java

示例13: getHgmHttpsRestTemplate

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的package包/類
@Bean
@Qualifier("hgmRestTemplate")
public RestTemplate getHgmHttpsRestTemplate() throws KeyStoreException, NoSuchAlgorithmException,
    KeyManagementException {
  SSLContext sslContext =
      SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).useTLS()
          .build();
  SSLConnectionSocketFactory connectionFactory =
      new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier());
  BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(configuration.getUsername(),
      configuration.getPassword()));

  HttpClient httpClient =
      HttpClientBuilder.create().setSSLSocketFactory(connectionFactory)
          .setDefaultCredentialsProvider(credentialsProvider).build();

  ClientHttpRequestFactory requestFactory =
      new HttpComponentsClientHttpRequestFactory(httpClient);
  return new RestTemplate(requestFactory);
}
 
開發者ID:trustedanalytics,項目名稱:hdfs-broker,代碼行數:22,代碼來源:HgmHttpsConfiguration.java

示例14: ExtHttpClientStack

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的package包/類
public ExtHttpClientStack() throws VolleyError {
	try {
		SSLContext sslcontext = SSLContexts.custom()
				.loadTrustMaterial(null, new TrustStrategy() {
					@Override
					public boolean isTrusted(
							final X509Certificate[] chain,
							final String authType) throws CertificateException {
						return true;
					}
				}).build();

		client = HttpClients.custom()
				//.setUserAgent(USER_AGENT)
				.setRedirectStrategy(new LaxRedirectStrategy())
				.setSslcontext(sslcontext)
				.build();
	} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
		throw new VolleyError("Cannot create HttpClient");
	}
}
 
開發者ID:m1chalz,項目名稱:RESTImplementation,代碼行數:22,代碼來源:ExtHttpClientStack.java

示例15: prepareHttpClient

import org.apache.http.conn.ssl.SSLContexts; //導入依賴的package包/類
private CloseableHttpClient prepareHttpClient()
		throws NoSuchAlgorithmException, CertificateException, IOException,
		KeyManagementException, KeyStoreException {

	// for simple http (no https) use this instead:
	// return HttpClients.createDefault();

	FileInputStream fin = new FileInputStream(TRUSTSTORE_PATH);
	KeyStore ks = KeyStore.getInstance(TRUSTSTORE_TYPE);
	ks.load(fin, TRUSTSTORE_PASSWORD.toCharArray());

	SSLContext sslContext = SSLContexts.custom().useTLS()
			.loadTrustMaterial(ks).build();

	SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
			sslContext);

	return HttpClients.custom().setSSLSocketFactory(sslsf).build();
}
 
開發者ID:ytus,項目名稱:https-helloworld-server-and-client,代碼行數:20,代碼來源:HelloWorldClient.java


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