99import org .apache .commons .io .IOUtils ;
1010import org .apache .commons .lang3 .RegExUtils ;
1111import 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 ;
1217import org .apache .http .impl .client .CloseableHttpClient ;
18+ import org .apache .http .impl .client .HttpClientBuilder ;
1319import org .apache .http .ssl .SSLContexts ;
1420
1521import javax .net .ssl .SSLContext ;
@@ -259,11 +265,15 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
259265 new WxPayCredentials (mchId , new PrivateKeySigner (certSerialNo , merchantPrivateKey )),
260266 apiV3Key .getBytes (StandardCharsets .UTF_8 ), this .getCertAutoUpdateTime ());
261267
262- CloseableHttpClient httpClient = WxPayV3HttpClientBuilder .create ()
268+ WxPayV3HttpClientBuilder wxPayV3HttpClientBuilder = WxPayV3HttpClientBuilder .create ()
263269 .withMerchant (mchId , certSerialNo , merchantPrivateKey )
264270 .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+
267277 this .apiV3HttpClient = httpClient ;
268278 this .verifier =verifier ;
269279 this .privateKey = merchantPrivateKey ;
@@ -274,7 +284,25 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
274284 }
275285 }
276286
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+ }
277297
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+ }
278306
279307 private InputStream loadConfigInputStream (String configPath , byte [] configContent , String fileName ) throws WxPayException {
280308 InputStream inputStream ;
0 commit comments