mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-08-24 16:18:51 +08:00
#251 微信支付证书文件路径配置支持classpath开头的地址
This commit is contained in:
parent
ec4bf2687c
commit
c7ffff0a9c
@ -1,11 +1,15 @@
|
|||||||
package com.github.binarywang.wxpay.config;
|
package com.github.binarywang.wxpay.config;
|
||||||
|
|
||||||
|
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
import org.apache.commons.lang3.StringUtils;
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.apache.http.ssl.SSLContexts;
|
import org.apache.http.ssl.SSLContexts;
|
||||||
|
|
||||||
import javax.net.ssl.SSLContext;
|
import javax.net.ssl.SSLContext;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
import java.security.KeyStore;
|
import java.security.KeyStore;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -143,7 +147,7 @@ public class WxPayConfig {
|
|||||||
this.useSandboxEnv = useSandboxEnv;
|
this.useSandboxEnv = useSandboxEnv;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SSLContext initSSLContext() {
|
public SSLContext initSSLContext() throws WxPayException {
|
||||||
if (StringUtils.isBlank(mchId)) {
|
if (StringUtils.isBlank(mchId)) {
|
||||||
throw new IllegalArgumentException("请确保商户号mchId已设置");
|
throw new IllegalArgumentException("请确保商户号mchId已设置");
|
||||||
}
|
}
|
||||||
@ -152,20 +156,33 @@ public class WxPayConfig {
|
|||||||
throw new IllegalArgumentException("请确保证书文件地址keyPath已配置");
|
throw new IllegalArgumentException("请确保证书文件地址keyPath已配置");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
InputStream inputStream;
|
||||||
|
final String prefix = "classpath:";
|
||||||
|
if (this.keyPath.startsWith(prefix)) {
|
||||||
|
inputStream = WxPayConfig.class.getResourceAsStream(this.keyPath.replace(prefix, ""));
|
||||||
|
} else {
|
||||||
|
try {
|
||||||
File file = new File(this.keyPath);
|
File file = new File(this.keyPath);
|
||||||
if (!file.exists()) {
|
if (!file.exists()) {
|
||||||
throw new RuntimeException("证书文件【" + file.getPath() + "】不存在!");
|
throw new WxPayException("证书文件【" + file.getPath() + "】不存在!");
|
||||||
|
}
|
||||||
|
|
||||||
|
inputStream = new FileInputStream(file);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new WxPayException("证书文件有问题,请核实!", e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
FileInputStream inputStream = new FileInputStream(file);
|
|
||||||
KeyStore keystore = KeyStore.getInstance("PKCS12");
|
KeyStore keystore = KeyStore.getInstance("PKCS12");
|
||||||
char[] partnerId2charArray = mchId.toCharArray();
|
char[] partnerId2charArray = mchId.toCharArray();
|
||||||
keystore.load(inputStream, partnerId2charArray);
|
keystore.load(inputStream, partnerId2charArray);
|
||||||
this.sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build();
|
this.sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build();
|
||||||
return this.sslContext;
|
return this.sslContext;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("证书文件有问题,请核实!", e);
|
throw new WxPayException("证书文件有问题,请核实!", e);
|
||||||
|
} finally {
|
||||||
|
IOUtils.closeQuietly(inputStream);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -45,6 +45,11 @@ public class WxPayException extends Exception {
|
|||||||
this.customErrorMsg = customErrorMsg;
|
this.customErrorMsg = customErrorMsg;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public WxPayException(String customErrorMsg, Throwable tr) {
|
||||||
|
super(customErrorMsg, tr);
|
||||||
|
this.customErrorMsg = customErrorMsg;
|
||||||
|
}
|
||||||
|
|
||||||
private WxPayException(Builder builder) {
|
private WxPayException(Builder builder) {
|
||||||
super(builder.buildErrorMsg());
|
super(builder.buildErrorMsg());
|
||||||
returnCode = builder.returnCode;
|
returnCode = builder.returnCode;
|
||||||
|
@ -497,7 +497,7 @@ public class WxPayServiceImpl implements WxPayService {
|
|||||||
|
|
||||||
String responseString = this.getResponseString(request.send());
|
String responseString = this.getResponseString(request.send());
|
||||||
|
|
||||||
this.log.debug("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", url, requestStr, responseString);
|
this.log.info("\n【请求地址】: {}\n【请求参数】:{}\n【响应数据】:{}", url, requestStr, responseString);
|
||||||
return responseString;
|
return responseString;
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
|
this.log.error("\n【请求地址】: {}\n【请求参数】:{}\n【异常信息】:{}", url, requestStr, e.getMessage());
|
||||||
|
@ -0,0 +1,24 @@
|
|||||||
|
package com.github.binarywang.wxpay.config;
|
||||||
|
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import static org.testng.Assert.*;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <pre>
|
||||||
|
* Created by BinaryWang on 2017/6/18.
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||||
|
*/
|
||||||
|
public class WxPayConfigTest {
|
||||||
|
private WxPayConfig payConfig = new WxPayConfig();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInitSSLContext() throws Exception {
|
||||||
|
payConfig.setMchId("123");
|
||||||
|
payConfig.setKeyPath("classpath:/abc.p12");
|
||||||
|
payConfig.initSSLContext();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
1
weixin-java-pay/src/test/resources/.gitignore
vendored
Normal file
1
weixin-java-pay/src/test/resources/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
*.p12
|
Loading…
Reference in New Issue
Block a user