diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java index 641ee9a53..59733944a 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayConfig.java @@ -1,6 +1,7 @@ package com.github.binarywang.wxpay.config; 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.v3.WxPayV3HttpClientBuilder; import com.github.binarywang.wxpay.v3.auth.*; @@ -260,17 +261,19 @@ public class WxPayConfig { if(StringUtils.isBlank(serialNo)){ this.certSerialNo = certificate.getSerialNumber().toString(16).toUpperCase(); } + //构造Http Proxy正向代理 + WxPayHttpProxy wxPayHttpProxy = getWxPayHttpProxy(); AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier( 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() .withMerchant(mchId, certSerialNo, merchantPrivateKey) .withWechatpay(Collections.singletonList(certificate)) .withValidator(new WxPayValidator(verifier)); //初始化V3接口正向代理设置 - initHttpProxy(wxPayV3HttpClientBuilder); + HttpProxyUtils.initHttpProxy(wxPayV3HttpClientBuilder,wxPayHttpProxy); CloseableHttpClient httpClient = wxPayV3HttpClientBuilder.build(); @@ -285,23 +288,14 @@ public class WxPayConfig { } /** - * 配置 http 正向代理 - * 参考代码: WxPayServiceApacheHttpImpl 中的方法 createHttpClientBuilder - * @param httpClientBuilder http构造参数 + * 初始化一个WxPayHttpProxy对象 + * @return 返回封装的WxPayHttpProxy对象。如未指定代理主机和端口,则默认返回null */ - private void initHttpProxy(HttpClientBuilder httpClientBuilder) { + private WxPayHttpProxy getWxPayHttpProxy() { if (StringUtils.isNotBlank(this.getHttpProxyHost()) && this.getHttpProxyPort() > 0) { - if (StringUtils.isEmpty(this.getHttpProxyUsername())) { - 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 new WxPayHttpProxy(getHttpProxyHost(), getHttpProxyPort(), getHttpProxyUsername(), getHttpProxyPassword()); } + return null; } private InputStream loadConfigInputStream(String configPath, byte[] configContent, String fileName) throws WxPayException { diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayHttpProxy.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayHttpProxy.java new file mode 100755 index 000000000..2b3b1c937 --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/config/WxPayHttpProxy.java @@ -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; + } + + + +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/util/HttpProxyUtils.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/util/HttpProxyUtils.java new file mode 100755 index 000000000..7e24cde80 --- /dev/null +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/util/HttpProxyUtils.java @@ -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())); + } + } + + + + + +} diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifier.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifier.java old mode 100644 new mode 100755 index d365ccaf4..ca8b30bc8 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifier.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/AutoUpdateCertificatesVerifier.java @@ -1,5 +1,7 @@ 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.Validator; import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder; @@ -35,6 +37,7 @@ import java.util.concurrent.locks.ReentrantLock; * 在原有CertificatesVerifier基础上,增加自动更新证书功能 * * @author doger.wang + * @author Long Yu */ @Slf4j public class AutoUpdateCertificatesVerifier implements Verifier { @@ -61,6 +64,11 @@ public class AutoUpdateCertificatesVerifier implements Verifier { 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) { + this(credentials,apiV3Key,minutesInterval,null); + } + + public AutoUpdateCertificatesVerifier(Credentials credentials, byte[] apiV3Key, int minutesInterval,WxPayHttpProxy wxPayHttpProxy) { this.credentials = credentials; this.apiV3Key = apiV3Key; this.minutesInterval = minutesInterval; + this.wxPayHttpProxy = wxPayHttpProxy; //构造时更新证书 try { autoUpdateCert(); @@ -126,15 +139,22 @@ public class AutoUpdateCertificatesVerifier implements Verifier { } private void autoUpdateCert() throws IOException, GeneralSecurityException { - CloseableHttpClient httpClient = WxPayV3HttpClientBuilder.create() + WxPayV3HttpClientBuilder wxPayV3HttpClientBuilder = WxPayV3HttpClientBuilder.create() .withCredentials(credentials) .withValidator(verifier == null ? new Validator() { @Override public boolean validate(CloseableHttpResponse response) throws IOException { return true; } - } : new WxPayValidator(verifier)) - .build(); + } : new WxPayValidator(verifier)); + + //调用自定义扩展设置设置HTTP PROXY对象 + HttpProxyUtils.initHttpProxy(wxPayV3HttpClientBuilder,this.wxPayHttpProxy); + + //增加自定义扩展点,子类可以设置其他构造参数 + this.customHttpClientBuilder(wxPayV3HttpClientBuilder); + + CloseableHttpClient httpClient = wxPayV3HttpClientBuilder.build(); HttpGet httpGet = new HttpGet(CERT_DOWNLOAD_PATH); 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) { + } + /** * 反序列化证书并解密 */