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;
}
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;
}
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);
}
}
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();
}
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;
}
Aggregations