use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project cas by apereo.
the class TrustedProxyAuthenticationTrustStoreSslSocketFactoryTests method prepareHttpClient.
@Before
public void prepareHttpClient() throws Exception {
final SimpleHttpClientFactoryBean clientFactory = new SimpleHttpClientFactoryBean();
clientFactory.setSslSocketFactory(new SSLConnectionSocketFactory(new DefaultCasSslContext(TRUST_STORE, TRUST_STORE_PSW, KeyStore.getDefaultType()).getSslContext()));
this.client = clientFactory.getObject();
}
use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project quickutil by quickutil.
the class HttpUtil method initHttpsClientMananger.
/**
* 生成https连接管理器
*
* @param clientCer-客户端证书
* @param clientPW-客户端证书密钥
* @param serverCer-服务端证书
* @param serverPW-服务端证书密钥
* @return
*/
public static HttpClientConnectionManager initHttpsClientMananger(InputStream clientCer, String clientPW, InputStream serverCer, String serverPW) {
try {
KeyManager[] keysManagers = null;
TrustManager[] trustManagers = null;
// 验证客户端证书
if (clientCer != null) {
KeyStore ks = KeyStore.getInstance("pkcs12");
ks.load(clientCer, clientPW.toCharArray());
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyManagerFactory.init(ks, clientPW.toCharArray());
keysManagers = keyManagerFactory.getKeyManagers();
}
// 验证服务端证书
if (serverCer != null) {
KeyStore ks2 = KeyStore.getInstance("pkcs12");
ks2.load(serverCer, serverPW.toCharArray());
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(ks2);
trustManagers = trustManagerFactory.getTrustManagers();
} else {
trustManagers = new TrustManager[] { tm };
}
// 生成ssl参数
SSLContext context = SSLContext.getInstance("TLS");
context.init(keysManagers, trustManagers, null);
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(context);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", socketFactory).build();
return new PoolingHttpClientConnectionManager(socketFactoryRegistry);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project dq-easy-cloud by dq-open-cloud.
the class DqHttpRequestTemplateBO method createSSL.
/**
* 创建ssl配置
*
* @param configStorage
* 请求配置
* @return SSLConnectionSocketFactory Layered socket factory for TLS/SSL
* connections.
*/
public SSLConnectionSocketFactory createSSL(DqHttpConfigStorageDTO configStorage) {
if (DqStringUtils.isEmpty(configStorage.getKeystore())) {
return null;
}
// 读取本机存放的PKCS12证书文件
try (InputStream instream = configStorage.isPath() ? new FileInputStream(new File(configStorage.getKeystore())) : new ByteArrayInputStream(configStorage.getKeystore().getBytes())) {
// 指定读取证书格式为PKCS12
KeyStore keyStore = KeyStore.getInstance("PKCS12");
char[] password = configStorage.getStorePassword().toCharArray();
// 指定PKCS12的密码
keyStore.load(instream, password);
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, password).build();
// 指定TLS版本
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, new DefaultHostnameVerifier());
return sslsf;
} catch (IOException e) {
e.printStackTrace();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
return null;
}
use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project dq-easy-cloud by dq-open-cloud.
the class DqWxPayService method wxPayRefund.
/**
* @Author: HONGLINCHEN
* @Description:微信退款 注意::微信金额的单位是分 所以这里要X100 转成int是因为 退款的时候不能有小数点
* @param merchantNumber 商户这边的订单号
* @param wxTransactionNumber 微信那边的交易单号
* @param totalFee 订单的金额
* @Date: 2017-9-12 11:18
*/
@SuppressWarnings("deprecation")
public Object wxPayRefund(DqRefundOrderAbstractDTO refundOrder) {
try {
KeyStore keyStore = KeyStore.getInstance("PKCS12");
FileInputStream instream = new FileInputStream(new File("E:/tools/wx_pay/cert/apiclient_cert.p12"));
try {
keyStore.load(instream, payConfigStorage.getPid().toCharArray());
} finally {
instream.close();
}
// Trust own CA and all self-signed certs
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, payConfigStorage.getPid().toCharArray()).build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null, SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
// HttpGet httpget = new
// HttpGet("https://api.mch.weixin.qq.com/secapi/pay/refund");
HttpPost httppost = new HttpPost("https://api.mch.weixin.qq.com/secapi/pay/refund");
// 微信金额的单位是分 所以这里要X100 转成int是因为 退款的时候不能有小数点
// String xml = WXPayUtil.wxPayRefund(merchantNumber,wxTransactionNumber,String.valueOf((int)(totalFee*100)));
// 获取公共参数
Map<String, Object> parameters = getPublicParameters();
if (DqStringUtils.isNotEmpty(refundOrder.getTradeNo())) {
parameters.put(DqWxPayKey.TRANSACTION__ID_KEY, refundOrder.getTradeNo());
} else {
parameters.put(DqWxPayKey.OUT__TRADE__NO_KEY, refundOrder.getOutTradeNo());
}
parameters.put(DqWxPayKey.OUT__REFUND__NO_KEY, refundOrder.getRefundNo());
parameters.put(DqWxPayKey.TOTAL__FEE_KEY, refundOrder.getTotalAmountOfCent());
parameters.put(DqWxPayKey.REFUND__FEE_KEY, refundOrder.getRefundAmount());
parameters.put(DqWxPayKey.OP__USER__ID_KEY, payConfigStorage.getPid());
parameters.put(DqWxPayKey.NONCE__STR_KEY, String.valueOf(System.currentTimeMillis()));
// 设置签名
setSign(parameters);
String xml = DqXMLUtils.getXmlStrFromMap(parameters);
try {
StringEntity se = new StringEntity(xml);
httppost.setEntity(se);
System.out.println("executing request" + httppost.getRequestLine());
CloseableHttpResponse responseEntry = httpclient.execute(httppost);
try {
HttpEntity entity = responseEntry.getEntity();
System.out.println(responseEntry.getStatusLine());
if (entity != null) {
return DqXMLUtils.getMapFromInputStream(entity.getContent());
}
EntityUtils.consume(entity);
} finally {
responseEntry.close();
}
} finally {
httpclient.close();
}
return null;
} catch (Exception e) {
e.printStackTrace();
JSONObject result = new JSONObject();
result.put("status", "error");
result.put("msg", e.getMessage());
return result;
}
}
use of org.apache.http.conn.ssl.SSLConnectionSocketFactory in project questdb by bluestreak01.
the class HttpTestUtils method createHttpClient_AcceptsUntrustedCerts.
private static HttpClientBuilder createHttpClient_AcceptsUntrustedCerts() throws Exception {
HttpClientBuilder b = HttpClientBuilder.create();
// setup a Trust Strategy that allows all certificates.
//
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, (arg0, arg1) -> true).build();
b.setSSLContext(sslContext);
// here's the special part:
// -- need to create an SSL Socket Factory, to use our weakened "trust strategy";
// -- and create a Registry, to register it.
//
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, (s, sslSession) -> true);
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory).build();
// now, we create connection-manager using our Registry.
// -- allows multi-threaded use
b.setConnectionManager(new PoolingHttpClientConnectionManager(socketFactoryRegistry));
return b;
}
Aggregations