@@ -52,7 +52,9 @@ public class HttpRequestTemplate {
5252
5353 protected HttpHost httpProxy ;
5454
55- HttpConfigStorage configStorage ;
55+ protected HttpConfigStorage configStorage ;
56+
57+ private SSLConnectionSocketFactory sslsf ;
5658 /**
5759 * 获取代理带代理地址的 HttpHost
5860 * @return 获取代理带代理地址的 HttpHost
@@ -73,9 +75,9 @@ public CloseableHttpClient getHttpClient() {
7375 .custom ()
7476 //网络提供者
7577 .setDefaultCredentialsProvider (createCredentialsProvider (configStorage ))
78+ .setConnectionManager (connectionManager (configStorage ))
7679 //设置httpclient的SSLSocketFactory
7780 .setSSLSocketFactory (createSSL (configStorage ))
78- .setConnectionManager (connectionManager (configStorage ))
7981 .setDefaultRequestConfig (createRequestConfig (configStorage ))
8082 .build ();
8183 if (null == connectionManager ) {
@@ -114,36 +116,38 @@ public HttpRequestTemplate() {
114116 * @return SSLConnectionSocketFactory Layered socket factory for TLS/SSL connections.
115117 */
116118 public SSLConnectionSocketFactory createSSL ( HttpConfigStorage configStorage ){
117-
119+ if (null != sslsf ){
120+ return sslsf ;
121+ }
118122 if (null == configStorage .getKeystore ()){
119123 try {
120- return new SSLConnectionSocketFactory (SSLContext .getDefault ());
124+ return sslsf = new SSLConnectionSocketFactory (SSLContext .getDefault ());
121125 } catch (NoSuchAlgorithmException e ) {
122126 LOG .error (e );
123127 }
124128 }
125129
126- //读取本机存放的PKCS12证书文件
130+ //读取本机存放的PKCS12证书文件
127131 try (InputStream instream = configStorage .getKeystoreInputStream ()){
128- //指定读取证书格式为PKCS12
129- KeyStore keyStore = KeyStore .getInstance ("PKCS12" );
130-
131- char [] password = configStorage .getStorePassword ().toCharArray ();
132- //指定PKCS12的密码
133- keyStore .load (instream , password );
134- // 实例化密钥库 & 初始化密钥工厂
135- KeyManagerFactory kmf = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
136- kmf .init (keyStore , password );
137- // 创建 SSLContext
138- SSLContext sslcontext = SSLContexts .custom ()
139- .loadKeyMaterial (keyStore , password ).build ();
140-
141- //指定TLS版本
142- SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory (
143- sslcontext , new String []{"TLSv1" }, null ,
144- new DefaultHostnameVerifier ());
145-
146- return sslsf ;
132+ //指定读取证书格式为PKCS12
133+ KeyStore keyStore = KeyStore .getInstance ("PKCS12" );
134+
135+ char [] password = configStorage .getStorePassword ().toCharArray ();
136+ //指定PKCS12的密码
137+ keyStore .load (instream , password );
138+ // 实例化密钥库 & 初始化密钥工厂
139+ KeyManagerFactory kmf = KeyManagerFactory .getInstance (KeyManagerFactory .getDefaultAlgorithm ());
140+ kmf .init (keyStore , password );
141+ // 创建 SSLContext
142+ SSLContext sslcontext = SSLContexts .custom ()
143+ .loadKeyMaterial (keyStore , password ).build ();
144+
145+ //指定TLS版本
146+ sslsf = new SSLConnectionSocketFactory (
147+ sslcontext , new String []{"TLSv1" }, null ,
148+ new DefaultHostnameVerifier ());
149+
150+ return sslsf ;
147151 } catch (IOException e ) {
148152 LOG .error (e );
149153 } catch (GeneralSecurityException e ) {
@@ -168,7 +172,7 @@ public CredentialsProvider createCredentialsProvider(HttpConfigStorage configSto
168172 // 需要用户认证的代理服务器
169173 CredentialsProvider credsProvider = new BasicCredentialsProvider ();
170174 credsProvider .setCredentials (
171- AuthScope .ANY ,
175+ AuthScope .ANY ,
172176 new UsernamePasswordCredentials (configStorage .getAuthUsername (), configStorage .getAuthPassword ()));
173177
174178
@@ -348,9 +352,9 @@ public <T>T doExecute(URI uri, Object request, Class<T> responseType, MethodType
348352 }
349353 httpRequest .setResponseType (responseType );
350354 try (CloseableHttpResponse response = getHttpClient ().execute (httpRequest )) {
351- return httpRequest .handleResponse (response );
355+ return httpRequest .handleResponse (response );
352356 }catch (IOException e ){
353- throw new PayErrorException (new PayException ("IOException" , e .getLocalizedMessage ()));
357+ throw new PayErrorException (new PayException ("IOException" , e .getLocalizedMessage ()));
354358 }finally {
355359 httpRequest .releaseConnection ();
356360 }
@@ -368,6 +372,6 @@ public <T>T doExecute(URI uri, Object request, Class<T> responseType, MethodType
368372 * @return 类型对象
369373 */
370374 public <T >T doExecute (String uri , Object request , Class <T > responseType , MethodType method ){
371- return doExecute (URI .create (uri ), request , responseType , method );
375+ return doExecute (URI .create (uri ), request , responseType , method );
372376 }
373377}
0 commit comments