当前位置: 首页>>代码示例>>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;未经允许,请勿转载。