Commit 352eb876 authored by 郑建's avatar 郑建

增加删除临时文件

parent 87f50b15
...@@ -105,9 +105,7 @@ public class HttpClientConfig { ...@@ -105,9 +105,7 @@ public class HttpClientConfig {
} }
@Bean("niwodaiHttpsClient") @Bean("niwodaiHttpsClient")
public CloseableHttpClient niwodaiHttpsClient() throws Exception{ public CloseableHttpClient niwodaiHttpsClient() throws Exception {
File temp1 = null,temp2 = null;
try{
/** /**
* 创建TrustManager * 创建TrustManager
*/ */
...@@ -127,11 +125,11 @@ public class HttpClientConfig { ...@@ -127,11 +125,11 @@ public class HttpClientConfig {
}; };
String classesPath = "/niwodai"; String classesPath = "/niwodai";
KeyStore keyStore = KeyStore.getInstance("PKCS12"); KeyStore keyStore = KeyStore.getInstance("PKCS12");
temp1 = asFile(readJarFile(classesPath + "/qg-keystore.jks")); keyStore.load(HttpClientConfig.class.getResourceAsStream(classesPath + "/qg-keystore.jks"), "password".toCharArray());
keyStore.load(new FileInputStream(temp1), "password".toCharArray()); KeyStore trustStore = KeyStore.getInstance("JKS");
temp2 = asFile(readJarFile(classesPath + "/qg-truststore.jks")); trustStore.load(HttpClientConfig.class.getResourceAsStream(classesPath + "/qg-truststore.jks"), "password".toCharArray());
SSLContext sslcontext = SSLContexts.custom() SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(temp2, "password".toCharArray(), new TrustSelfSignedStrategy()) .loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, "password".toCharArray()) .loadKeyMaterial(keyStore, "password".toCharArray())
.build(); .build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory( SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
...@@ -170,31 +168,7 @@ public class HttpClientConfig { ...@@ -170,31 +168,7 @@ public class HttpClientConfig {
.setRetryHandler(retryHandler) .setRetryHandler(retryHandler)
.setKeepAliveStrategy(keepAliveStrategy) .setKeepAliveStrategy(keepAliveStrategy)
.build(); .build();
}finally {
if (temp1 != null){
temp1.delete();
}
if (temp2 != null){
temp2.delete();
}
}
} }
private static InputStream readJarFile(String fileName) throws IOException
{
return HttpClientConfig.class.getResourceAsStream(fileName);
}
public static File asFile(InputStream inputStream) throws IOException{
File tmp = File.createTempFile("trustKey", ".jks", new File(""));
OutputStream os = new FileOutputStream(tmp);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
inputStream.close();
return tmp;
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment