mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-05 17:38:05 +08:00
#586 微信支付 WxPayConfig增加支持byte数组方式设置证书
This commit is contained in:
parent
71f97c063f
commit
06c356bdcc
@ -1,5 +1,6 @@
|
||||
package com.github.binarywang.wxpay.config;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
@ -74,9 +75,14 @@ public class WxPayConfig {
|
||||
private String signType;
|
||||
private SSLContext sslContext;
|
||||
/**
|
||||
* 证书apiclient_cert.p12的文件的绝对路径.
|
||||
* p12证书文件的绝对路径或者以classpath:开头的类路径.
|
||||
*/
|
||||
private String keyPath;
|
||||
|
||||
/**
|
||||
* p12证书文件内容的字节数组.
|
||||
*/
|
||||
private byte[] keyContent;
|
||||
/**
|
||||
* 微信支付是否使用仿真测试环境.
|
||||
* 默认不使用
|
||||
@ -95,33 +101,37 @@ public class WxPayConfig {
|
||||
throw new WxPayException("请确保商户号mchId已设置");
|
||||
}
|
||||
|
||||
if (StringUtils.isBlank(this.getKeyPath())) {
|
||||
throw new WxPayException("请确保证书文件地址keyPath已配置");
|
||||
}
|
||||
|
||||
InputStream inputStream;
|
||||
final String prefix = "classpath:";
|
||||
String fileHasProblemMsg = "证书文件【" + this.getKeyPath() + "】有问题,请核实!";
|
||||
String fileNotFoundMsg = "证书文件【" + this.getKeyPath() + "】不存在,请核实!";
|
||||
if (this.getKeyPath().startsWith(prefix)) {
|
||||
String path = StringUtils.removeFirst(this.getKeyPath(), prefix);
|
||||
if (!path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
}
|
||||
inputStream = WxPayConfig.class.getResourceAsStream(path);
|
||||
if (inputStream == null) {
|
||||
throw new WxPayException(fileNotFoundMsg);
|
||||
}
|
||||
if (this.keyContent != null) {
|
||||
inputStream = new ByteArrayInputStream(this.keyContent);
|
||||
} else {
|
||||
try {
|
||||
File file = new File(this.getKeyPath());
|
||||
if (!file.exists()) {
|
||||
if (StringUtils.isBlank(this.getKeyPath())) {
|
||||
throw new WxPayException("请确保证书文件地址keyPath已配置");
|
||||
}
|
||||
|
||||
final String prefix = "classpath:";
|
||||
String fileHasProblemMsg = "证书文件【" + this.getKeyPath() + "】有问题,请核实!";
|
||||
String fileNotFoundMsg = "证书文件【" + this.getKeyPath() + "】不存在,请核实!";
|
||||
if (this.getKeyPath().startsWith(prefix)) {
|
||||
String path = StringUtils.removeFirst(this.getKeyPath(), prefix);
|
||||
if (!path.startsWith("/")) {
|
||||
path = "/" + path;
|
||||
}
|
||||
inputStream = WxPayConfig.class.getResourceAsStream(path);
|
||||
if (inputStream == null) {
|
||||
throw new WxPayException(fileNotFoundMsg);
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
File file = new File(this.getKeyPath());
|
||||
if (!file.exists()) {
|
||||
throw new WxPayException(fileNotFoundMsg);
|
||||
}
|
||||
|
||||
inputStream = new FileInputStream(file);
|
||||
} catch (IOException e) {
|
||||
throw new WxPayException(fileHasProblemMsg, e);
|
||||
inputStream = new FileInputStream(file);
|
||||
} catch (IOException e) {
|
||||
throw new WxPayException(fileHasProblemMsg, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -132,7 +142,7 @@ public class WxPayConfig {
|
||||
this.sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build();
|
||||
return this.sslContext;
|
||||
} catch (Exception e) {
|
||||
throw new WxPayException(fileHasProblemMsg, e);
|
||||
throw new WxPayException("证书文件有问题,请核实!", e);
|
||||
} finally {
|
||||
IOUtils.closeQuietly(inputStream);
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user