如何根據(jù)鏈接獲取證書?
private static final String keyPassword = "這里填寫的證書密碼"http://微信官方下載的證書密碼一般是商戶號(hào)
private static SSLContext wx_ssl_context = null;
try {
FileInputStream inputStream = new FileInputStream("這里填寫你的證書的絕對(duì)路徑");
KeyStore keystore = KeyStore.getInstance("PKCS12");//p12格式證書
//證書密碼
char[] keyPassword = TransfersConfig.mch_id.toCharArray();//將密碼裝換為字符數(shù)組
keystore.load(inputStream, keyPassword);//加載
wx_ssl_context = SSLContexts.custom().loadKeyMaterial(keystore, keyPassword).build();//獲取到的證書
} catch (Exception e) {
e.printStackTrace();
}
如何在調(diào)用接口的時(shí)候使用證書?
private static SSLConnectionSocketFactory getSSLConnectionSocket() {
//wx_ssl_context是上一步獲取到的證書
return new SSLConnectionSocketFactory(wx_ssl_context, new String[] {"TLSv1", "TLSv1.1", "TLSv1.2"}, null,
SSLConnectionSocketFactory.getDefaultHostnameVerifier());
}
private static CloseableHttpClient buildHttpClien() {
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLSocketFactory(getSSLConnectionSocket()).build();
return httpClient;//提醒一下記得關(guān)閉httpClient芭商,httpClient.close();
}