Skip to content

Commit 7101c22

Browse files
🎨 #2467 【微信支付】微信支付V3接口请求增加代理设置参数的支持
1 parent 739c222 commit 7101c22

4 files changed

Lines changed: 165 additions & 19 deletions

File tree

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.binarywang.wxpay.config;
22

33
import com.github.binarywang.wxpay.exception.WxPayException;
4+
import com.github.binarywang.wxpay.util.HttpProxyUtils;
45
import com.github.binarywang.wxpay.util.ResourcesUtils;
56
import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder;
67
import com.github.binarywang.wxpay.v3.auth.*;
@@ -260,17 +261,19 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
260261
if(StringUtils.isBlank(serialNo)){
261262
this.certSerialNo = certificate.getSerialNumber().toString(16).toUpperCase();
262263
}
264+
//构造Http Proxy正向代理
265+
WxPayHttpProxy wxPayHttpProxy = getWxPayHttpProxy();
263266

264267
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
265268
new WxPayCredentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)),
266-
apiV3Key.getBytes(StandardCharsets.UTF_8), this.getCertAutoUpdateTime());
269+
apiV3Key.getBytes(StandardCharsets.UTF_8), this.getCertAutoUpdateTime(),wxPayHttpProxy);
267270

268271
WxPayV3HttpClientBuilder wxPayV3HttpClientBuilder = WxPayV3HttpClientBuilder.create()
269272
.withMerchant(mchId, certSerialNo, merchantPrivateKey)
270273
.withWechatpay(Collections.singletonList(certificate))
271274
.withValidator(new WxPayValidator(verifier));
272275
//初始化V3接口正向代理设置
273-
initHttpProxy(wxPayV3HttpClientBuilder);
276+
HttpProxyUtils.initHttpProxy(wxPayV3HttpClientBuilder,wxPayHttpProxy);
274277

275278
CloseableHttpClient httpClient = wxPayV3HttpClientBuilder.build();
276279

@@ -285,23 +288,14 @@ public CloseableHttpClient initApiV3HttpClient() throws WxPayException {
285288
}
286289

287290
/**
288-
* 配置 http 正向代理
289-
* 参考代码: WxPayServiceApacheHttpImpl 中的方法 createHttpClientBuilder
290-
* @param httpClientBuilder http构造参数
291+
* 初始化一个WxPayHttpProxy对象
292+
* @return 返回封装的WxPayHttpProxy对象。如未指定代理主机和端口,则默认返回null
291293
*/
292-
private void initHttpProxy(HttpClientBuilder httpClientBuilder) {
294+
private WxPayHttpProxy getWxPayHttpProxy() {
293295
if (StringUtils.isNotBlank(this.getHttpProxyHost()) && this.getHttpProxyPort() > 0) {
294-
if (StringUtils.isEmpty(this.getHttpProxyUsername())) {
295-
this.setHttpProxyUsername("whatever");
296-
}
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()));
296+
return new WxPayHttpProxy(getHttpProxyHost(), getHttpProxyPort(), getHttpProxyUsername(), getHttpProxyPassword());
304297
}
298+
return null;
305299
}
306300

307301
private InputStream loadConfigInputStream(String configPath, byte[] configContent, String fileName) throws WxPayException {
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.github.binarywang.wxpay.config;
2+
3+
4+
import java.io.Serializable;
5+
6+
/**
7+
* 微信支付 HTTP Proxy 正向代理配置
8+
*
9+
* @author Long Yu
10+
* @date 2021-12-28 15:49:03
11+
*/
12+
public class WxPayHttpProxy implements Serializable {
13+
14+
/**
15+
* 代理主机
16+
*/
17+
private String httpProxyHost;
18+
/**
19+
* 代理端口
20+
*/
21+
private Integer httpProxyPort;
22+
/**
23+
* 代理用户名称
24+
*/
25+
private String httpProxyUsername;
26+
/**
27+
* 代理密码
28+
*/
29+
private String httpProxyPassword;
30+
31+
public WxPayHttpProxy() {
32+
}
33+
34+
public WxPayHttpProxy(String httpProxyHost, Integer httpProxyPort, String httpProxyUsername, String httpProxyPassword) {
35+
this.httpProxyHost = httpProxyHost;
36+
this.httpProxyPort = httpProxyPort;
37+
this.httpProxyUsername = httpProxyUsername;
38+
this.httpProxyPassword = httpProxyPassword;
39+
}
40+
41+
public String getHttpProxyHost() {
42+
return httpProxyHost;
43+
}
44+
45+
public void setHttpProxyHost(String httpProxyHost) {
46+
this.httpProxyHost = httpProxyHost;
47+
}
48+
49+
public Integer getHttpProxyPort() {
50+
return httpProxyPort;
51+
}
52+
53+
public void setHttpProxyPort(Integer httpProxyPort) {
54+
this.httpProxyPort = httpProxyPort;
55+
}
56+
57+
public String getHttpProxyUsername() {
58+
return httpProxyUsername;
59+
}
60+
61+
public void setHttpProxyUsername(String httpProxyUsername) {
62+
this.httpProxyUsername = httpProxyUsername;
63+
}
64+
65+
public String getHttpProxyPassword() {
66+
return httpProxyPassword;
67+
}
68+
69+
public void setHttpProxyPassword(String httpProxyPassword) {
70+
this.httpProxyPassword = httpProxyPassword;
71+
}
72+
73+
74+
75+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.github.binarywang.wxpay.util;
2+
3+
import com.github.binarywang.wxpay.config.WxPayHttpProxy;
4+
import org.apache.commons.lang3.StringUtils;
5+
import org.apache.http.HttpHost;
6+
import org.apache.http.auth.AuthScope;
7+
import org.apache.http.auth.UsernamePasswordCredentials;
8+
import org.apache.http.client.CredentialsProvider;
9+
import org.apache.http.impl.client.BasicCredentialsProvider;
10+
import org.apache.http.impl.client.HttpClientBuilder;
11+
12+
/**
13+
* 微信支付 HTTP Proxy 工具类
14+
*
15+
* @author Long Yu
16+
* @date 2021-12-28 15:58:03
17+
*/
18+
public class HttpProxyUtils {
19+
20+
/**
21+
* 配置 http 正向代理 可以实现内网服务通过代理调用接口
22+
* 参考代码: WxPayServiceApacheHttpImpl 中的方法 createHttpClientBuilder
23+
*
24+
* @param wxPayHttpProxy 代理配置
25+
* @param httpClientBuilder http构造参数
26+
*/
27+
public static void initHttpProxy(HttpClientBuilder httpClientBuilder, WxPayHttpProxy wxPayHttpProxy) {
28+
if(wxPayHttpProxy == null){
29+
return;
30+
}
31+
if (StringUtils.isNotBlank(wxPayHttpProxy.getHttpProxyHost()) && wxPayHttpProxy.getHttpProxyPort() > 0) {
32+
if (StringUtils.isEmpty(wxPayHttpProxy.getHttpProxyUsername())) {
33+
wxPayHttpProxy.setHttpProxyUsername("whatever");
34+
}
35+
36+
// 使用代理服务器 需要用户认证的代理服务器
37+
CredentialsProvider provider = new BasicCredentialsProvider();
38+
provider.setCredentials(new AuthScope(wxPayHttpProxy.getHttpProxyHost(), wxPayHttpProxy.getHttpProxyPort()),
39+
new UsernamePasswordCredentials(wxPayHttpProxy.getHttpProxyUsername(), wxPayHttpProxy.getHttpProxyPassword()));
40+
httpClientBuilder.setDefaultCredentialsProvider(provider);
41+
httpClientBuilder.setProxy(new HttpHost(wxPayHttpProxy.getHttpProxyHost(), wxPayHttpProxy.getHttpProxyPort()));
42+
}
43+
}
44+
45+
46+
47+
48+
49+
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifier.java

100644100755
Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.binarywang.wxpay.v3.auth;
22

3+
import com.github.binarywang.wxpay.config.WxPayHttpProxy;
4+
import com.github.binarywang.wxpay.util.HttpProxyUtils;
35
import com.github.binarywang.wxpay.v3.Credentials;
46
import com.github.binarywang.wxpay.v3.Validator;
57
import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder;
@@ -35,6 +37,7 @@
3537
* 在原有CertificatesVerifier基础上,增加自动更新证书功能
3638
*
3739
* @author doger.wang
40+
* @author Long Yu
3841
*/
3942
@Slf4j
4043
public class AutoUpdateCertificatesVerifier implements Verifier {
@@ -61,6 +64,11 @@ public class AutoUpdateCertificatesVerifier implements Verifier {
6164

6265
private final ReentrantLock lock = new ReentrantLock();
6366

67+
/**
68+
* 微信支付代理对象
69+
*/
70+
private WxPayHttpProxy wxPayHttpProxy;
71+
6472
/**
6573
* 时间间隔枚举,支持一小时、六小时以及十二小时
6674
*/
@@ -88,9 +96,14 @@ public AutoUpdateCertificatesVerifier(Credentials credentials, byte[] apiV3Key)
8896
}
8997

9098
public AutoUpdateCertificatesVerifier(Credentials credentials, byte[] apiV3Key, int minutesInterval) {
99+
this(credentials,apiV3Key,minutesInterval,null);
100+
}
101+
102+
public AutoUpdateCertificatesVerifier(Credentials credentials, byte[] apiV3Key, int minutesInterval,WxPayHttpProxy wxPayHttpProxy) {
91103
this.credentials = credentials;
92104
this.apiV3Key = apiV3Key;
93105
this.minutesInterval = minutesInterval;
106+
this.wxPayHttpProxy = wxPayHttpProxy;
94107
//构造时更新证书
95108
try {
96109
autoUpdateCert();
@@ -126,15 +139,22 @@ private void checkAndAutoUpdateCert() {
126139
}
127140

128141
private void autoUpdateCert() throws IOException, GeneralSecurityException {
129-
CloseableHttpClient httpClient = WxPayV3HttpClientBuilder.create()
142+
WxPayV3HttpClientBuilder wxPayV3HttpClientBuilder = WxPayV3HttpClientBuilder.create()
130143
.withCredentials(credentials)
131144
.withValidator(verifier == null ? new Validator() {
132145
@Override
133146
public boolean validate(CloseableHttpResponse response) throws IOException {
134147
return true;
135148
}
136-
} : new WxPayValidator(verifier))
137-
.build();
149+
} : new WxPayValidator(verifier));
150+
151+
//调用自定义扩展设置设置HTTP PROXY对象
152+
HttpProxyUtils.initHttpProxy(wxPayV3HttpClientBuilder,this.wxPayHttpProxy);
153+
154+
//增加自定义扩展点,子类可以设置其他构造参数
155+
this.customHttpClientBuilder(wxPayV3HttpClientBuilder);
156+
157+
CloseableHttpClient httpClient = wxPayV3HttpClientBuilder.build();
138158

139159
HttpGet httpGet = new HttpGet(CERT_DOWNLOAD_PATH);
140160
httpGet.addHeader("Accept", "application/json");
@@ -154,6 +174,14 @@ public boolean validate(CloseableHttpResponse response) throws IOException {
154174
}
155175
}
156176

177+
178+
/**
179+
* 子类可以自定义添加Http client builder的信息
180+
* @param builder httpclient构造器
181+
*/
182+
public void customHttpClientBuilder(WxPayV3HttpClientBuilder builder) {
183+
}
184+
157185
/**
158186
* 反序列化证书并解密
159187
*/

0 commit comments

Comments
 (0)