Skip to content

feat(代理): 添加对代理的支持 #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.execchain.ClientExecChain;
import org.apache.http.HttpHost;

/**
* @author xy-peng
Expand All @@ -21,6 +22,7 @@ public class WechatPayHttpClientBuilder extends HttpClientBuilder {
private Credentials credentials;
private Validator validator;


private WechatPayHttpClientBuilder() {
super();

Expand Down Expand Up @@ -51,6 +53,13 @@ public WechatPayHttpClientBuilder withWechatPay(List<X509Certificate> certificat
return this;
}

public WechatPayHttpClientBuilder withProxy(HttpHost proxy) {
if (proxy != null) {
this.setProxy(proxy);
}
return this;
}

/**
* Please use {@link #withWechatPay(List)} instead
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpHost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

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

private HttpHost proxy;

private ConcurrentHashMap<String, ConcurrentHashMap<BigInteger, X509Certificate>> certificates = new ConcurrentHashMap<>();

private ConcurrentHashMap<String, Credentials> credentialsMap = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -158,6 +161,15 @@ public synchronized void putMerchant(String merchantId, Credentials credentials,
}
}

/***
* 代理配置
*
* @param proxy 代理host
**/
public synchronized void setProxy(HttpHost proxy) {
this.proxy = proxy;
}

/**
* 停止自动更新平台证书,停止后无法再重新启动
*/
Expand Down Expand Up @@ -257,6 +269,7 @@ private synchronized void downloadAndUpdateCert(String merchantId, Verifier veri
.withCredentials(credentials)
.withValidator(verifier == null ? (response) -> true
: new WechatPay2Validator(verifier))
.withProxy(proxy)
.build()) {
HttpGet httpGet = new HttpGet(CERT_DOWNLOAD_PATH);
httpGet.addHeader(ACCEPT, APPLICATION_JSON.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpHost;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -41,11 +42,15 @@ public class CertificatesManagerTest {
CertificatesManager certificatesManager;
Verifier verifier;

private static final HttpHost proxy = null;

@Before
public void setup() throws Exception {
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKey);
// 获取证书管理器实例
certificatesManager = CertificatesManager.getInstance();
// 添加代理服务器
certificatesManager.setProxy(proxy);
// 向证书管理器增加需要自动更新平台证书的商户信息
certificatesManager.putMerchant(merchantId, new WechatPay2Credentials(merchantId,
new PrivateKeySigner(merchantSerialNumber, merchantPrivateKey)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.HttpHost;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -47,6 +48,8 @@ public class HttpClientBuilderTest {
+ "-----END CERTIFICATE-----";
private CloseableHttpClient httpClient;

private static final HttpHost proxy = null;

@Before
public void setup() {
PrivateKey merchantPrivateKey = PemUtil.loadPrivateKey(privateKey);
Expand All @@ -59,6 +62,7 @@ public void setup() {
httpClient = WechatPayHttpClientBuilder.create()
.withMerchant(merchantId, merchantSerialNumber, merchantPrivateKey)
.withWechatPay(wechatPayCertificates)
.withProxy(proxy)
.build();
}

Expand Down