#586 微信支付 WxPayConfig增加支持byte数组方式设置证书

This commit is contained in:
Binary Wang
2018-05-13 18:39:42 +08:00
parent 71f97c063f
commit 06c356bdcc
2 changed files with 49 additions and 38 deletions

View File

@@ -1,8 +1,9 @@
package com.github.binarywang.wxpay.service.impl;
import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.exception.WxPayException;
import jodd.util.Base64;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import javax.net.ssl.SSLContext;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
@@ -19,9 +20,9 @@ import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import com.github.binarywang.wxpay.bean.WxPayApiData;
import com.github.binarywang.wxpay.exception.WxPayException;
import jodd.util.Base64;
/**
* <pre>
@@ -37,8 +38,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
try {
HttpClientBuilder httpClientBuilder = createHttpClientBuilder(useKey);
HttpPost httpPost = this.createHttpPost(url, requestStr);
try (CloseableHttpClient httpclient = httpClientBuilder.build()) {
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
final byte[] bytes = EntityUtils.toByteArray(response.getEntity());
final String responseData = Base64.encodeToString(bytes);
this.log.info("\n【请求地址】{}\n【请求数据】{}\n【响应数据(Base64编码后)】:{}", url, requestStr, responseData);
@@ -60,8 +61,8 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
try {
HttpClientBuilder httpClientBuilder = this.createHttpClientBuilder(useKey);
HttpPost httpPost = this.createHttpPost(url, requestStr);
try (CloseableHttpClient httpclient = httpClientBuilder.build()) {
try (CloseableHttpResponse response = httpclient.execute(httpPost)) {
try (CloseableHttpClient httpClient = httpClientBuilder.build()) {
try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
String responseString = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
this.log.info("\n【请求地址】{}\n【请求数据】{}\n【响应数据】{}", url, requestStr, responseString);
wxApiData.set(new WxPayApiData(url, requestStr, responseString, null));
@@ -90,7 +91,7 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
private HttpClientBuilder createHttpClientBuilder(boolean useKey) throws WxPayException {
HttpClientBuilder httpClientBuilder = HttpClients.custom();
if (useKey) {
this.setKey(httpClientBuilder);
this.initSSLContext(httpClientBuilder);
}
if (StringUtils.isNotBlank(this.getConfig().getHttpProxyHost())
@@ -118,15 +119,15 @@ public class WxPayServiceApacheHttpImpl extends BaseWxPayServiceImpl {
return httpPost;
}
private void setKey(HttpClientBuilder httpClientBuilder) throws WxPayException {
private void initSSLContext(HttpClientBuilder httpClientBuilder) throws WxPayException {
SSLContext sslContext = this.getConfig().getSslContext();
if (null == sslContext) {
sslContext = this.getConfig().initSSLContext();
}
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext,
SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
new String[]{"TLSv1"}, null, new DefaultHostnameVerifier());
httpClientBuilder.setSSLSocketFactory(sslsf);
httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
}
}