mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-06-28 13:16:19 +08:00
🎨 #2467 【微信支付】微信支付V3接口请求增加代理设置参数的支持
This commit is contained in:
parent
739c222586
commit
7101c22899
@ -1,6 +1,7 @@
|
|||||||
package com.github.binarywang.wxpay.config;
|
package com.github.binarywang.wxpay.config;
|
||||||
|
|
||||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||||
|
import com.github.binarywang.wxpay.util.HttpProxyUtils;
|
||||||
import com.github.binarywang.wxpay.util.ResourcesUtils;
|
import com.github.binarywang.wxpay.util.ResourcesUtils;
|
||||||
import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder;
|
import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder;
|
||||||
import com.github.binarywang.wxpay.v3.auth.*;
|
import com.github.binarywang.wxpay.v3.auth.*;
|
||||||
@ -260,17 +261,19 @@ public class WxPayConfig {
|
|||||||
if(StringUtils.isBlank(serialNo)){
|
if(StringUtils.isBlank(serialNo)){
|
||||||
this.certSerialNo = certificate.getSerialNumber().toString(16).toUpperCase();
|
this.certSerialNo = certificate.getSerialNumber().toString(16).toUpperCase();
|
||||||
}
|
}
|
||||||
|
//构造Http Proxy正向代理
|
||||||
|
WxPayHttpProxy wxPayHttpProxy = getWxPayHttpProxy();
|
||||||
|
|
||||||
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
|
AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier(
|
||||||
new WxPayCredentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)),
|
new WxPayCredentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)),
|
||||||
apiV3Key.getBytes(StandardCharsets.UTF_8), this.getCertAutoUpdateTime());
|
apiV3Key.getBytes(StandardCharsets.UTF_8), this.getCertAutoUpdateTime(),wxPayHttpProxy);
|
||||||
|
|
||||||
WxPayV3HttpClientBuilder wxPayV3HttpClientBuilder = WxPayV3HttpClientBuilder.create()
|
WxPayV3HttpClientBuilder wxPayV3HttpClientBuilder = WxPayV3HttpClientBuilder.create()
|
||||||
.withMerchant(mchId, certSerialNo, merchantPrivateKey)
|
.withMerchant(mchId, certSerialNo, merchantPrivateKey)
|
||||||
.withWechatpay(Collections.singletonList(certificate))
|
.withWechatpay(Collections.singletonList(certificate))
|
||||||
.withValidator(new WxPayValidator(verifier));
|
.withValidator(new WxPayValidator(verifier));
|
||||||
//初始化V3接口正向代理设置
|
//初始化V3接口正向代理设置
|
||||||
initHttpProxy(wxPayV3HttpClientBuilder);
|
HttpProxyUtils.initHttpProxy(wxPayV3HttpClientBuilder,wxPayHttpProxy);
|
||||||
|
|
||||||
CloseableHttpClient httpClient = wxPayV3HttpClientBuilder.build();
|
CloseableHttpClient httpClient = wxPayV3HttpClientBuilder.build();
|
||||||
|
|
||||||
@ -285,23 +288,14 @@ public class WxPayConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 配置 http 正向代理
|
* 初始化一个WxPayHttpProxy对象
|
||||||
* 参考代码: WxPayServiceApacheHttpImpl 中的方法 createHttpClientBuilder
|
* @return 返回封装的WxPayHttpProxy对象。如未指定代理主机和端口,则默认返回null
|
||||||
* @param httpClientBuilder http构造参数
|
|
||||||
*/
|
*/
|
||||||
private void initHttpProxy(HttpClientBuilder httpClientBuilder) {
|
private WxPayHttpProxy getWxPayHttpProxy() {
|
||||||
if (StringUtils.isNotBlank(this.getHttpProxyHost()) && this.getHttpProxyPort() > 0) {
|
if (StringUtils.isNotBlank(this.getHttpProxyHost()) && this.getHttpProxyPort() > 0) {
|
||||||
if (StringUtils.isEmpty(this.getHttpProxyUsername())) {
|
return new WxPayHttpProxy(getHttpProxyHost(), getHttpProxyPort(), getHttpProxyUsername(), getHttpProxyPassword());
|
||||||
this.setHttpProxyUsername("whatever");
|
|
||||||
}
|
|
||||||
|
|
||||||
// 使用代理服务器 需要用户认证的代理服务器
|
|
||||||
CredentialsProvider provider = new BasicCredentialsProvider();
|
|
||||||
provider.setCredentials(new AuthScope(this.getHttpProxyHost(), this.getHttpProxyPort()),
|
|
||||||
new UsernamePasswordCredentials(this.getHttpProxyUsername(), this.getHttpProxyPassword()));
|
|
||||||
httpClientBuilder.setDefaultCredentialsProvider(provider);
|
|
||||||
httpClientBuilder.setProxy(new HttpHost(this.getHttpProxyHost(), this.getHttpProxyPort()));
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
private InputStream loadConfigInputStream(String configPath, byte[] configContent, String fileName) throws WxPayException {
|
private InputStream loadConfigInputStream(String configPath, byte[] configContent, String fileName) throws WxPayException {
|
||||||
|
@ -0,0 +1,75 @@
|
|||||||
|
package com.github.binarywang.wxpay.config;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信支付 HTTP Proxy 正向代理配置
|
||||||
|
*
|
||||||
|
* @author Long Yu
|
||||||
|
* @date 2021-12-28 15:49:03
|
||||||
|
*/
|
||||||
|
public class WxPayHttpProxy implements Serializable {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 代理主机
|
||||||
|
*/
|
||||||
|
private String httpProxyHost;
|
||||||
|
/**
|
||||||
|
* 代理端口
|
||||||
|
*/
|
||||||
|
private Integer httpProxyPort;
|
||||||
|
/**
|
||||||
|
* 代理用户名称
|
||||||
|
*/
|
||||||
|
private String httpProxyUsername;
|
||||||
|
/**
|
||||||
|
* 代理密码
|
||||||
|
*/
|
||||||
|
private String httpProxyPassword;
|
||||||
|
|
||||||
|
public WxPayHttpProxy() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public WxPayHttpProxy(String httpProxyHost, Integer httpProxyPort, String httpProxyUsername, String httpProxyPassword) {
|
||||||
|
this.httpProxyHost = httpProxyHost;
|
||||||
|
this.httpProxyPort = httpProxyPort;
|
||||||
|
this.httpProxyUsername = httpProxyUsername;
|
||||||
|
this.httpProxyPassword = httpProxyPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHttpProxyHost() {
|
||||||
|
return httpProxyHost;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHttpProxyHost(String httpProxyHost) {
|
||||||
|
this.httpProxyHost = httpProxyHost;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getHttpProxyPort() {
|
||||||
|
return httpProxyPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHttpProxyPort(Integer httpProxyPort) {
|
||||||
|
this.httpProxyPort = httpProxyPort;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHttpProxyUsername() {
|
||||||
|
return httpProxyUsername;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHttpProxyUsername(String httpProxyUsername) {
|
||||||
|
this.httpProxyUsername = httpProxyUsername;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getHttpProxyPassword() {
|
||||||
|
return httpProxyPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHttpProxyPassword(String httpProxyPassword) {
|
||||||
|
this.httpProxyPassword = httpProxyPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,49 @@
|
|||||||
|
package com.github.binarywang.wxpay.util;
|
||||||
|
|
||||||
|
import com.github.binarywang.wxpay.config.WxPayHttpProxy;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
|
import org.apache.http.auth.AuthScope;
|
||||||
|
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||||
|
import org.apache.http.client.CredentialsProvider;
|
||||||
|
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||||
|
import org.apache.http.impl.client.HttpClientBuilder;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信支付 HTTP Proxy 工具类
|
||||||
|
*
|
||||||
|
* @author Long Yu
|
||||||
|
* @date 2021-12-28 15:58:03
|
||||||
|
*/
|
||||||
|
public class HttpProxyUtils {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 配置 http 正向代理 可以实现内网服务通过代理调用接口
|
||||||
|
* 参考代码: WxPayServiceApacheHttpImpl 中的方法 createHttpClientBuilder
|
||||||
|
*
|
||||||
|
* @param wxPayHttpProxy 代理配置
|
||||||
|
* @param httpClientBuilder http构造参数
|
||||||
|
*/
|
||||||
|
public static void initHttpProxy(HttpClientBuilder httpClientBuilder, WxPayHttpProxy wxPayHttpProxy) {
|
||||||
|
if(wxPayHttpProxy == null){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotBlank(wxPayHttpProxy.getHttpProxyHost()) && wxPayHttpProxy.getHttpProxyPort() > 0) {
|
||||||
|
if (StringUtils.isEmpty(wxPayHttpProxy.getHttpProxyUsername())) {
|
||||||
|
wxPayHttpProxy.setHttpProxyUsername("whatever");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 使用代理服务器 需要用户认证的代理服务器
|
||||||
|
CredentialsProvider provider = new BasicCredentialsProvider();
|
||||||
|
provider.setCredentials(new AuthScope(wxPayHttpProxy.getHttpProxyHost(), wxPayHttpProxy.getHttpProxyPort()),
|
||||||
|
new UsernamePasswordCredentials(wxPayHttpProxy.getHttpProxyUsername(), wxPayHttpProxy.getHttpProxyPassword()));
|
||||||
|
httpClientBuilder.setDefaultCredentialsProvider(provider);
|
||||||
|
httpClientBuilder.setProxy(new HttpHost(wxPayHttpProxy.getHttpProxyHost(), wxPayHttpProxy.getHttpProxyPort()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
34
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifier.java
Normal file → Executable file
34
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifier.java
Normal file → Executable file
@ -1,5 +1,7 @@
|
|||||||
package com.github.binarywang.wxpay.v3.auth;
|
package com.github.binarywang.wxpay.v3.auth;
|
||||||
|
|
||||||
|
import com.github.binarywang.wxpay.config.WxPayHttpProxy;
|
||||||
|
import com.github.binarywang.wxpay.util.HttpProxyUtils;
|
||||||
import com.github.binarywang.wxpay.v3.Credentials;
|
import com.github.binarywang.wxpay.v3.Credentials;
|
||||||
import com.github.binarywang.wxpay.v3.Validator;
|
import com.github.binarywang.wxpay.v3.Validator;
|
||||||
import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder;
|
import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder;
|
||||||
@ -35,6 +37,7 @@ import java.util.concurrent.locks.ReentrantLock;
|
|||||||
* 在原有CertificatesVerifier基础上,增加自动更新证书功能
|
* 在原有CertificatesVerifier基础上,增加自动更新证书功能
|
||||||
*
|
*
|
||||||
* @author doger.wang
|
* @author doger.wang
|
||||||
|
* @author Long Yu
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class AutoUpdateCertificatesVerifier implements Verifier {
|
public class AutoUpdateCertificatesVerifier implements Verifier {
|
||||||
@ -61,6 +64,11 @@ public class AutoUpdateCertificatesVerifier implements Verifier {
|
|||||||
|
|
||||||
private final ReentrantLock lock = new ReentrantLock();
|
private final ReentrantLock lock = new ReentrantLock();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 微信支付代理对象
|
||||||
|
*/
|
||||||
|
private WxPayHttpProxy wxPayHttpProxy;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 时间间隔枚举,支持一小时、六小时以及十二小时
|
* 时间间隔枚举,支持一小时、六小时以及十二小时
|
||||||
*/
|
*/
|
||||||
@ -88,9 +96,14 @@ public class AutoUpdateCertificatesVerifier implements Verifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public AutoUpdateCertificatesVerifier(Credentials credentials, byte[] apiV3Key, int minutesInterval) {
|
public AutoUpdateCertificatesVerifier(Credentials credentials, byte[] apiV3Key, int minutesInterval) {
|
||||||
|
this(credentials,apiV3Key,minutesInterval,null);
|
||||||
|
}
|
||||||
|
|
||||||
|
public AutoUpdateCertificatesVerifier(Credentials credentials, byte[] apiV3Key, int minutesInterval,WxPayHttpProxy wxPayHttpProxy) {
|
||||||
this.credentials = credentials;
|
this.credentials = credentials;
|
||||||
this.apiV3Key = apiV3Key;
|
this.apiV3Key = apiV3Key;
|
||||||
this.minutesInterval = minutesInterval;
|
this.minutesInterval = minutesInterval;
|
||||||
|
this.wxPayHttpProxy = wxPayHttpProxy;
|
||||||
//构造时更新证书
|
//构造时更新证书
|
||||||
try {
|
try {
|
||||||
autoUpdateCert();
|
autoUpdateCert();
|
||||||
@ -126,15 +139,22 @@ public class AutoUpdateCertificatesVerifier implements Verifier {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void autoUpdateCert() throws IOException, GeneralSecurityException {
|
private void autoUpdateCert() throws IOException, GeneralSecurityException {
|
||||||
CloseableHttpClient httpClient = WxPayV3HttpClientBuilder.create()
|
WxPayV3HttpClientBuilder wxPayV3HttpClientBuilder = WxPayV3HttpClientBuilder.create()
|
||||||
.withCredentials(credentials)
|
.withCredentials(credentials)
|
||||||
.withValidator(verifier == null ? new Validator() {
|
.withValidator(verifier == null ? new Validator() {
|
||||||
@Override
|
@Override
|
||||||
public boolean validate(CloseableHttpResponse response) throws IOException {
|
public boolean validate(CloseableHttpResponse response) throws IOException {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} : new WxPayValidator(verifier))
|
} : new WxPayValidator(verifier));
|
||||||
.build();
|
|
||||||
|
//调用自定义扩展设置设置HTTP PROXY对象
|
||||||
|
HttpProxyUtils.initHttpProxy(wxPayV3HttpClientBuilder,this.wxPayHttpProxy);
|
||||||
|
|
||||||
|
//增加自定义扩展点,子类可以设置其他构造参数
|
||||||
|
this.customHttpClientBuilder(wxPayV3HttpClientBuilder);
|
||||||
|
|
||||||
|
CloseableHttpClient httpClient = wxPayV3HttpClientBuilder.build();
|
||||||
|
|
||||||
HttpGet httpGet = new HttpGet(CERT_DOWNLOAD_PATH);
|
HttpGet httpGet = new HttpGet(CERT_DOWNLOAD_PATH);
|
||||||
httpGet.addHeader("Accept", "application/json");
|
httpGet.addHeader("Accept", "application/json");
|
||||||
@ -154,6 +174,14 @@ public class AutoUpdateCertificatesVerifier implements Verifier {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 子类可以自定义添加Http client builder的信息
|
||||||
|
* @param builder httpclient构造器
|
||||||
|
*/
|
||||||
|
public void customHttpClientBuilder(WxPayV3HttpClientBuilder builder) {
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 反序列化证书并解密
|
* 反序列化证书并解密
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user