Search in sources :

Example 16 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project synergic-developing by zeemood.

the class HttpClientUtils method connectWithXMLAndSSLByPost.

@SuppressWarnings("deprecation")
public static String connectWithXMLAndSSLByPost(URI uri, Document doc, String sslPath, String password) throws Exception {
    String ret = "";
    String xml = doc.asXML();
    KeyStore keyStore = KeyStore.getInstance("PKCS12");
    try (FileInputStream instream = new FileInputStream(new File(WechatPayConfigurations.getRefundCertificatePath()))) {
        keyStore.load(instream, password.toCharArray());
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    // Trust own CA and all self-signed certs
    SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, password.toCharArray()).build();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    try (CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build()) {
        HttpPost post = new HttpPost(uri);
        post.setEntity(new StringEntity(xml, "UTF-8"));
        try (CloseableHttpResponse response = httpClient.execute(post)) {
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
                String text;
                while ((text = bufferedReader.readLine()) != null) {
                    ret += text;
                }
            }
            EntityUtils.consume(entity);
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    } catch (Exception e) {
        e.printStackTrace();
        throw e;
    }
    return ret;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) HttpEntity(org.apache.http.HttpEntity) InputStreamReader(java.io.InputStreamReader) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) FileInputStream(java.io.FileInputStream) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) BufferedReader(java.io.BufferedReader) File(java.io.File)

Example 17 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project webanno by webanno.

the class WebhookService method getNonValidatingRequestFactory.

private HttpComponentsClientHttpRequestFactory getNonValidatingRequestFactory() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
    if (nonValidatingRequestFactory == null) {
        TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
        SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
        SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext);
        CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(csf).build();
        nonValidatingRequestFactory = new HttpComponentsClientHttpRequestFactory();
        nonValidatingRequestFactory.setHttpClient(httpClient);
    }
    return nonValidatingRequestFactory;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) TrustStrategy(org.apache.http.ssl.TrustStrategy) SSLContext(javax.net.ssl.SSLContext) HttpComponentsClientHttpRequestFactory(org.springframework.http.client.HttpComponentsClientHttpRequestFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) X509Certificate(java.security.cert.X509Certificate)

Example 18 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project ligoj-api by ligoj.

the class CurlProcessor method newSslContext.

/**
 * Return a trusted SSL registry using the given protocol.
 *
 * @param protocol
 *            The SSL protocol.
 * @return A new trusted SSL registry using the given protocol.
 */
protected static Registry<ConnectionSocketFactory> newSslContext(final String protocol) {
    // Initialize HTTPS scheme
    final TrustManager[] allCerts = new TrustManager[] { new TrustedX509TrustManager() };
    try {
        final SSLContext sslContext = SSLContext.getInstance(protocol);
        sslContext.init(null, allCerts, new SecureRandom());
        final SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
        return RegistryBuilder.<ConnectionSocketFactory>create().register("https", sslSocketFactory).register("http", PlainConnectionSocketFactory.getSocketFactory()).build();
    } catch (final GeneralSecurityException e) {
        // Wrap the exception
        throw new IllegalStateException("Unable to build a secured " + protocol + " registry", e);
    }
}
Also used : GeneralSecurityException(java.security.GeneralSecurityException) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) TrustManager(javax.net.ssl.TrustManager)

Example 19 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project spring-boot by Linda-Tan.

the class RestTemplateConfig method httpClient.

@Bean
public HttpClient httpClient() throws KeyManagementException, NoSuchAlgorithmException {
    Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", new SSLConnectionSocketFactory(createIgnoreVerifySSL())).build();
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(registry);
    connectionManager.setMaxTotal(5);
    connectionManager.setDefaultMaxPerRoute(5);
    RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(8000).setConnectTimeout(8000).setConnectionRequestTimeout(8000).build();
    return HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setConnectionManager(connectionManager).build();
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 20 with SSLConnectionSocketFactory

use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project JFramework by gugumall.

the class JHttp method createSelfSignedX.

/**
 * @param certFilePath
 * @param password
 * @param keyStoreType
 * @return
 * @throws Exception
 */
public static JHttp createSelfSignedX(URL certFilePath, String password, String[] protocols) throws Exception {
    JHttp jhttp = new JHttp();
    SSLContext ctx = SSLContexts.custom().loadTrustMaterial(certFilePath, password.toCharArray(), new TrustSelfSignedStrategy()).build();
    ctx.init(null, new TrustManager[] { new MyTrustManager() }, null);
    SSLConnectionSocketFactory factory = new SSLConnectionSocketFactory(ctx, protocols, null, SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    jhttp.poolingmgr = new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", factory).build(), null, null, null, 5000, TimeUnit.MILLISECONDS);
    jhttp.poolingmgr.setDefaultMaxPerRoute(100);
    jhttp.poolingmgr.setMaxTotal(1000);
    return jhttp;
}
Also used : SSLContext(javax.net.ssl.SSLContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager)

Aggregations

SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)179 SSLContext (javax.net.ssl.SSLContext)109 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)72 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)61 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)58 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)58 IOException (java.io.IOException)49 TrustSelfSignedStrategy (org.apache.http.conn.ssl.TrustSelfSignedStrategy)45 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)44 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)41 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)35 KeyManagementException (java.security.KeyManagementException)34 NoopHostnameVerifier (org.apache.http.conn.ssl.NoopHostnameVerifier)33 HttpClient (org.apache.http.client.HttpClient)28 RequestConfig (org.apache.http.client.config.RequestConfig)28 KeyStoreException (java.security.KeyStoreException)27 KeyStore (java.security.KeyStore)26 HostnameVerifier (javax.net.ssl.HostnameVerifier)26 CertificateException (java.security.cert.CertificateException)24 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)24