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 b94620ee9..e6a266640 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 @@ -6,33 +6,32 @@ import com.github.binarywang.wxpay.util.ResourcesUtils; import com.github.binarywang.wxpay.v3.WxPayV3HttpClientBuilder; import com.github.binarywang.wxpay.v3.auth.*; import com.github.binarywang.wxpay.v3.util.PemUtils; -import lombok.*; -import org.apache.commons.io.IOUtils; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.SneakyThrows; +import lombok.ToString; import org.apache.commons.lang3.RegExUtils; 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.CloseableHttpClient; -import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.ssl.SSLContexts; import javax.net.ssl.SSLContext; -import java.io.*; +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.security.KeyStore; import java.security.PrivateKey; import java.security.cert.X509Certificate; import java.util.Base64; -import java.util.Collections; /** * 微信支付配置 * - * @author Binary Wang (https://github.com/binarywang) + * @author Binary Wang (...) */ @Data @ToString(exclude = "verifier") @@ -199,7 +198,7 @@ public class WxPayConfig { /** * v3接口下证书检验对象,通过改对象可以获取到X509Certificate,进一步对敏感信息加密 - * 文档见 https://wechatpay-api.gitbook.io/wechatpay-api-v3/qian-ming-zhi-nan-1/min-gan-xin-xi-jia-mi + * 文档 */ private Verifier verifier; @@ -236,9 +235,8 @@ public class WxPayConfig { throw new WxPayException("请确保商户号mchId已设置"); } - InputStream inputStream = this.loadConfigInputStream(this.keyString, this.getKeyPath(), this.keyContent, "p12证书"); - - try { + try (InputStream inputStream = this.loadConfigInputStream(this.keyString, this.getKeyPath(), + this.keyContent, "p12证书");) { KeyStore keystore = KeyStore.getInstance("PKCS12"); char[] partnerId2charArray = this.getMchId().toCharArray(); keystore.load(inputStream, partnerId2charArray); @@ -246,9 +244,8 @@ public class WxPayConfig { return this.sslContext; } catch (Exception e) { throw new WxPayException("证书文件有问题,请核实!", e); - } finally { - IOUtils.closeQuietly(inputStream); } + } /** @@ -259,42 +256,37 @@ public class WxPayConfig { * @author doger.wang **/ public CloseableHttpClient initApiV3HttpClient() throws WxPayException { - val privateKeyString = this.getPrivateKeyString(); - val privateKeyPath = this.getPrivateKeyPath(); - val privateCertString = this.getPrivateCertString(); - val privateCertPath = this.getPrivateCertPath(); - val serialNo = this.getCertSerialNo(); - val apiV3Key = this.getApiV3Key(); - if (StringUtils.isBlank(apiV3Key)) { + if (StringUtils.isBlank(this.getApiV3Key())) { throw new WxPayException("请确保apiV3Key值已设置"); } - InputStream keyInputStream = this.loadConfigInputStream(privateKeyString, privateKeyPath, this.privateKeyContent, "privateKeyPath"); - InputStream certInputStream = this.loadConfigInputStream(privateCertString, privateCertPath, this.privateCertContent, "privateCertPath"); + InputStream keyInputStream = this.loadConfigInputStream(this.getPrivateKeyString(), this.getPrivateKeyPath(), + this.privateKeyContent, "privateKeyPath"); + InputStream certInputStream = this.loadConfigInputStream(this.getPrivateCertString(), this.getPrivateCertPath(), + this.privateCertContent, "privateCertPath"); try { PrivateKey merchantPrivateKey = PemUtils.loadPrivateKey(keyInputStream); X509Certificate certificate = PemUtils.loadCertificate(certInputStream); - if(StringUtils.isBlank(serialNo)){ + if (StringUtils.isBlank(this.getCertSerialNo())) { this.certSerialNo = certificate.getSerialNumber().toString(16).toUpperCase(); } //构造Http Proxy正向代理 WxPayHttpProxy wxPayHttpProxy = getWxPayHttpProxy(); - AutoUpdateCertificatesVerifier verifier = new AutoUpdateCertificatesVerifier( + AutoUpdateCertificatesVerifier certificatesVerifier = new AutoUpdateCertificatesVerifier( new WxPayCredentials(mchId, new PrivateKeySigner(certSerialNo, merchantPrivateKey)), - apiV3Key.getBytes(StandardCharsets.UTF_8), this.getCertAutoUpdateTime(),wxPayHttpProxy); + this.getApiV3Key().getBytes(StandardCharsets.UTF_8), this.getCertAutoUpdateTime(), wxPayHttpProxy); WxPayV3HttpClientBuilder wxPayV3HttpClientBuilder = WxPayV3HttpClientBuilder.create() .withMerchant(mchId, certSerialNo, merchantPrivateKey) - .withWechatpay(Collections.singletonList(certificate)) - .withValidator(new WxPayValidator(verifier)); + .withValidator(new WxPayValidator(certificatesVerifier)); //初始化V3接口正向代理设置 - HttpProxyUtils.initHttpProxy(wxPayV3HttpClientBuilder,wxPayHttpProxy); + HttpProxyUtils.initHttpProxy(wxPayV3HttpClientBuilder, wxPayHttpProxy); CloseableHttpClient httpClient = wxPayV3HttpClientBuilder.build(); this.apiV3HttpClient = httpClient; - this.verifier=verifier; + this.verifier = certificatesVerifier; this.privateKey = merchantPrivateKey; return httpClient; @@ -305,6 +297,7 @@ public class WxPayConfig { /** * 初始化一个WxPayHttpProxy对象 + * * @return 返回封装的WxPayHttpProxy对象。如未指定代理主机和端口,则默认返回null */ private WxPayHttpProxy getWxPayHttpProxy() { @@ -314,11 +307,12 @@ public class WxPayConfig { return null; } - private InputStream loadConfigInputStream(String configString, String configPath, byte[] configContent, String fileName) throws WxPayException { + private InputStream loadConfigInputStream(String configString, String configPath, byte[] configContent, + String fileName) throws WxPayException { InputStream inputStream; if (configContent != null) { inputStream = new ByteArrayInputStream(configContent); - } else if(StringUtils.isNotEmpty(configString)) { + } else if (StringUtils.isNotEmpty(configString)) { configContent = Base64.getDecoder().decode(configString); inputStream = new ByteArrayInputStream(configContent); } else { @@ -333,34 +327,42 @@ public class WxPayConfig { /** * 从配置路径 加载配置 信息(支持 classpath、本地路径、网络url) + * * @param configPath 配置路径 - * @return - * @throws WxPayException + * @return . + * @throws WxPayException . */ private InputStream loadConfigInputStream(String configPath) throws WxPayException { - InputStream inputStream; - final String prefix = "classpath:"; String fileHasProblemMsg = String.format(PROBLEM_MSG, configPath); String fileNotFoundMsg = String.format(NOT_FOUND_MSG, configPath); + + final String prefix = "classpath:"; + InputStream inputStream; if (configPath.startsWith(prefix)) { String path = RegExUtils.removeFirst(configPath, prefix); if (!path.startsWith("/")) { path = "/" + path; } + try { inputStream = ResourcesUtils.getResourceAsStream(path); if (inputStream == null) { throw new WxPayException(fileNotFoundMsg); } + + return inputStream; } catch (Exception e) { throw new WxPayException(fileNotFoundMsg, e); } - } else if (configPath.startsWith("http://") || configPath.startsWith("https://")) { + } + + if (configPath.startsWith("http://") || configPath.startsWith("https://")) { try { inputStream = new URL(configPath).openStream(); if (inputStream == null) { throw new WxPayException(fileNotFoundMsg); } + return inputStream; } catch (IOException e) { throw new WxPayException(fileNotFoundMsg, e); } @@ -371,11 +373,11 @@ public class WxPayConfig { throw new WxPayException(fileNotFoundMsg); } - inputStream = new FileInputStream(file); + return Files.newInputStream(file.toPath()); } catch (IOException e) { throw new WxPayException(fileHasProblemMsg, e); } } - return inputStream; + } } diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WxPayV3HttpClientBuilder.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WxPayV3HttpClientBuilder.java index 986a8f4cb..f47936723 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WxPayV3HttpClientBuilder.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/WxPayV3HttpClientBuilder.java @@ -46,11 +46,6 @@ public class WxPayV3HttpClientBuilder extends HttpClientBuilder { return this; } - public WxPayV3HttpClientBuilder withWechatpay(List certificates) { - this.validator = new WxPayValidator(new CertificatesVerifier(certificates)); - return this; - } - public WxPayV3HttpClientBuilder withValidator(Validator validator) { this.validator = validator; return this; diff --git a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayValidator.java b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayValidator.java index e14d8b5b1..acb75bb6c 100644 --- a/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayValidator.java +++ b/weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/WxPayValidator.java @@ -11,9 +11,12 @@ import org.apache.http.util.EntityUtils; import java.io.IOException; +/** + * @author spvycf & F00lish + */ @Slf4j public class WxPayValidator implements Validator { - private Verifier verifier; + private final Verifier verifier; public WxPayValidator(Verifier verifier) { this.verifier = verifier; @@ -21,7 +24,8 @@ public class WxPayValidator implements Validator { @Override public final boolean validate(CloseableHttpResponse response) throws IOException { - if (!ContentType.APPLICATION_JSON.getMimeType().equals(ContentType.parse(String.valueOf(response.getFirstHeader("Content-Type").getValue())).getMimeType())) { + if (!ContentType.APPLICATION_JSON.getMimeType().equals(ContentType.parse(String.valueOf(response.getFirstHeader( + "Content-Type").getValue())).getMimeType())) { return true; } Header serialNo = response.getFirstHeader("Wechatpay-Serial"); @@ -44,8 +48,8 @@ public class WxPayValidator implements Validator { String body = getResponseBody(response); return timestamp + "\n" - + nonce + "\n" - + body + "\n"; + + nonce + "\n" + + body + "\n"; } protected final String getResponseBody(CloseableHttpResponse response) throws IOException {