9
9
import org .apache .commons .io .IOUtils ;
10
10
import org .apache .commons .lang3 .RegExUtils ;
11
11
import org .apache .commons .lang3 .StringUtils ;
12
+ import org .apache .http .HttpHost ;
13
+ import org .apache .http .auth .AuthScope ;
14
+ import org .apache .http .auth .UsernamePasswordCredentials ;
15
+ import org .apache .http .client .CredentialsProvider ;
16
+ import org .apache .http .impl .client .BasicCredentialsProvider ;
12
17
import org .apache .http .impl .client .CloseableHttpClient ;
18
+ import org .apache .http .impl .client .HttpClientBuilder ;
13
19
import org .apache .http .ssl .SSLContexts ;
14
20
15
21
import javax .net .ssl .SSLContext ;
@@ -259,11 +265,15 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
259
265
new WxPayCredentials (mchId , new PrivateKeySigner (certSerialNo , merchantPrivateKey )),
260
266
apiV3Key .getBytes (StandardCharsets .UTF_8 ), this .getCertAutoUpdateTime ());
261
267
262
- CloseableHttpClient httpClient = WxPayV3HttpClientBuilder .create ()
268
+ WxPayV3HttpClientBuilder wxPayV3HttpClientBuilder = WxPayV3HttpClientBuilder .create ()
263
269
.withMerchant (mchId , certSerialNo , merchantPrivateKey )
264
270
.withWechatpay (Collections .singletonList (certificate ))
265
- .withValidator (new WxPayValidator (verifier ))
266
- .build ();
271
+ .withValidator (new WxPayValidator (verifier ));
272
+ //初始化V3接口正向代理设置
273
+ initHttpProxy (wxPayV3HttpClientBuilder );
274
+
275
+ CloseableHttpClient httpClient = wxPayV3HttpClientBuilder .build ();
276
+
267
277
this .apiV3HttpClient = httpClient ;
268
278
this .verifier =verifier ;
269
279
this .privateKey = merchantPrivateKey ;
@@ -274,7 +284,25 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
274
284
}
275
285
}
276
286
287
+ /**
288
+ * 配置 http 正向代理
289
+ * 参考代码: WxPayServiceApacheHttpImpl 中的方法 createHttpClientBuilder
290
+ * @param httpClientBuilder http构造参数
291
+ */
292
+ private void initHttpProxy (HttpClientBuilder httpClientBuilder ) {
293
+ if (StringUtils .isNotBlank (this .getHttpProxyHost ()) && this .getHttpProxyPort () > 0 ) {
294
+ if (StringUtils .isEmpty (this .getHttpProxyUsername ())) {
295
+ this .setHttpProxyUsername ("whatever" );
296
+ }
277
297
298
+ // 使用代理服务器 需要用户认证的代理服务器
299
+ CredentialsProvider provider = new BasicCredentialsProvider ();
300
+ provider .setCredentials (new AuthScope (this .getHttpProxyHost (), this .getHttpProxyPort ()),
301
+ new UsernamePasswordCredentials (this .getHttpProxyUsername (), this .getHttpProxyPassword ()));
302
+ httpClientBuilder .setDefaultCredentialsProvider (provider );
303
+ httpClientBuilder .setProxy (new HttpHost (this .getHttpProxyHost (), this .getHttpProxyPort ()));
304
+ }
305
+ }
278
306
279
307
private InputStream loadConfigInputStream (String configPath , byte [] configContent , String fileName ) throws WxPayException {
280
308
InputStream inputStream ;
0 commit comments