Skip to content

Commit 468e219

Browse files
authored
feat(代理): 添加对代理的支持 (#163)
1 parent c5e2487 commit 468e219

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

src/main/java/com/wechat/pay/contrib/apache/httpclient/WechatPayHttpClientBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.apache.http.impl.client.CloseableHttpClient;
1111
import org.apache.http.impl.client.HttpClientBuilder;
1212
import org.apache.http.impl.execchain.ClientExecChain;
13+
import org.apache.http.HttpHost;
1314

1415
/**
1516
* @author xy-peng
@@ -21,6 +22,7 @@ public class WechatPayHttpClientBuilder extends HttpClientBuilder {
2122
private Credentials credentials;
2223
private Validator validator;
2324

25+
2426
private WechatPayHttpClientBuilder() {
2527
super();
2628

@@ -51,6 +53,13 @@ public WechatPayHttpClientBuilder withWechatPay(List<X509Certificate> certificat
5153
return this;
5254
}
5355

56+
public WechatPayHttpClientBuilder withProxy(HttpHost proxy) {
57+
if (proxy != null) {
58+
this.setProxy(proxy);
59+
}
60+
return this;
61+
}
62+
5463
/**
5564
* Please use {@link #withWechatPay(List)} instead
5665
*

src/main/java/com/wechat/pay/contrib/apache/httpclient/cert/CertificatesManager.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.http.client.methods.HttpGet;
3333
import org.apache.http.impl.client.CloseableHttpClient;
3434
import org.apache.http.util.EntityUtils;
35+
import org.apache.http.HttpHost;
3536
import org.slf4j.Logger;
3637
import org.slf4j.LoggerFactory;
3738

@@ -53,6 +54,8 @@ public class CertificatesManager {
5354
private volatile static CertificatesManager instance = null;
5455
private ConcurrentHashMap<String, byte[]> apiV3Keys = new ConcurrentHashMap<>();
5556

57+
private HttpHost proxy;
58+
5659
private ConcurrentHashMap<String, ConcurrentHashMap<BigInteger, X509Certificate>> certificates = new ConcurrentHashMap<>();
5760

5861
private ConcurrentHashMap<String, Credentials> credentialsMap = new ConcurrentHashMap<>();
@@ -158,6 +161,15 @@ public synchronized void putMerchant(String merchantId, Credentials credentials,
158161
}
159162
}
160163

164+
/***
165+
* 代理配置
166+
*
167+
* @param proxy 代理host
168+
**/
169+
public synchronized void setProxy(HttpHost proxy) {
170+
this.proxy = proxy;
171+
}
172+
161173
/**
162174
* 停止自动更新平台证书,停止后无法再重新启动
163175
*/
@@ -257,6 +269,7 @@ private synchronized void downloadAndUpdateCert(String merchantId, Verifier veri
257269
.withCredentials(credentials)
258270
.withValidator(verifier == null ? (response) -> true
259271
: new WechatPay2Validator(verifier))
272+
.withProxy(proxy)
260273
.build()) {
261274
HttpGet httpGet = new HttpGet(CERT_DOWNLOAD_PATH);
262275
httpGet.addHeader(ACCEPT, APPLICATION_JSON.toString());

src/test/java/com/wechat/pay/contrib/apache/httpclient/CertificatesManagerTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.http.client.utils.URIBuilder;
2626
import org.apache.http.impl.client.CloseableHttpClient;
2727
import org.apache.http.util.EntityUtils;
28+
import org.apache.http.HttpHost;
2829
import org.junit.After;
2930
import org.junit.Before;
3031
import org.junit.Test;
@@ -41,11 +42,15 @@ public class CertificatesManagerTest {
4142
CertificatesManager certificatesManager;
4243
Verifier verifier;
4344

45+
private static final HttpHost proxy = null;
46+
4447
@Before
4548
public void setup() throws Exception {
4649
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKey);
4750
// 获取证书管理器实例
4851
certificatesManager = CertificatesManager.getInstance();
52+
// 添加代理服务器
53+
certificatesManager.setProxy(proxy);
4954
// 向证书管理器增加需要自动更新平台证书的商户信息
5055
certificatesManager.putMerchant(merchantId, new WechatPay2Credentials(merchantId,
5156
new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)),

src/test/java/com/wechat/pay/contrib/apache/httpclient/HttpClientBuilderTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.apache.http.entity.InputStreamEntity;
2626
import org.apache.http.entity.StringEntity;
2727
import org.apache.http.impl.client.CloseableHttpClient;
28+
import org.apache.http.HttpHost;
2829
import org.junit.After;
2930
import org.junit.Before;
3031
import org.junit.Test;
@@ -47,6 +48,8 @@ public class HttpClientBuilderTest {
4748
+ "-----END CERTIFICATE-----";
4849
private CloseableHttpClient httpClient;
4950

51+
private static final HttpHost proxy = null;
52+
5053
@Before
5154
public void setup() {
5255
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKey);
@@ -59,6 +62,7 @@ public void setup() {
5962
httpClient = WechatPayHttpClientBuilder.create()
6063
.withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey)
6164
.withWechatPay(wechatPayCertificates)
65+
.withProxy(proxy)
6266
.build();
6367
}
6468

0 commit comments

Comments
 (0)