mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-05-02 20:02:37 +08:00
🎨 优化代码,未正确配置 apiV3Key 时,173 行证书更新失败,未做 throw 等阻断动作,导致 verifier 未赋值,后续其它方法使用将出现空指针异常
This commit is contained in:
parent
c1e7d095ed
commit
10414d9971
@ -3,23 +3,22 @@ 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;
|
||||
import com.github.binarywang.wxpay.v3.util.AesUtils;
|
||||
import com.github.binarywang.wxpay.v3.util.PemUtils;
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import lombok.Getter;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
import me.chanjar.weixin.common.util.json.GsonParser;
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
@ -28,9 +27,12 @@ import java.security.GeneralSecurityException;
|
||||
import java.security.cert.CertificateExpiredException;
|
||||
import java.security.cert.CertificateNotYetValidException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
|
||||
/**
|
||||
@ -141,12 +143,7 @@ public class AutoUpdateCertificatesVerifier implements Verifier {
|
||||
private void autoUpdateCert() throws IOException, GeneralSecurityException {
|
||||
WxPayV3HttpClientBuilder wxPayV3HttpClientBuilder = WxPayV3HttpClientBuilder.create()
|
||||
.withCredentials(credentials)
|
||||
.withValidator(verifier == null ? new Validator() {
|
||||
@Override
|
||||
public boolean validate(CloseableHttpResponse response) throws IOException {
|
||||
return true;
|
||||
}
|
||||
} : new WxPayValidator(verifier));
|
||||
.withValidator(verifier == null ? response -> true : new WxPayValidator(verifier));
|
||||
|
||||
//调用自定义扩展设置设置HTTP PROXY对象
|
||||
HttpProxyUtils.initHttpProxy(wxPayV3HttpClientBuilder,this.wxPayHttpProxy);
|
||||
@ -162,15 +159,15 @@ public class AutoUpdateCertificatesVerifier implements Verifier {
|
||||
CloseableHttpResponse response = httpClient.execute(httpGet);
|
||||
int statusCode = response.getStatusLine().getStatusCode();
|
||||
String body = EntityUtils.toString(response.getEntity());
|
||||
if (statusCode == 200) {
|
||||
if (statusCode == HttpStatus.SC_OK) {
|
||||
List<X509Certificate> newCertList = deserializeToCerts(apiV3Key, body);
|
||||
if (newCertList.isEmpty()) {
|
||||
log.warn("Cert list is empty");
|
||||
return;
|
||||
throw new WxRuntimeException("Cert list is empty");
|
||||
}
|
||||
this.verifier = new CertificatesVerifier(newCertList);
|
||||
} else {
|
||||
log.warn("Auto update cert failed, statusCode = " + statusCode + ",body = " + body);
|
||||
throw new WxRuntimeException(this.getErrorMsg(body));
|
||||
}
|
||||
}
|
||||
|
||||
@ -223,4 +220,11 @@ public class AutoUpdateCertificatesVerifier implements Verifier {
|
||||
return verifier.getValidCertificate();
|
||||
}
|
||||
|
||||
private String getErrorMsg(String body) {
|
||||
return Optional
|
||||
.ofNullable(GsonParser.parse(body).getAsJsonObject())
|
||||
.map(resp -> resp.get("message"))
|
||||
.map(JsonElement::getAsString)
|
||||
.orElse("update cert failed");
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,55 @@
|
||||
package com.github.binarywang.wxpay.v3.auth;
|
||||
|
||||
import com.github.binarywang.wxpay.bean.merchanttransfer.TransferCreateRequest;
|
||||
import com.github.binarywang.wxpay.bean.merchanttransfer.TransferCreateRequest.TransferDetailList;
|
||||
import com.github.binarywang.wxpay.bean.merchanttransfer.TransferCreateResult;
|
||||
import com.github.binarywang.wxpay.exception.WxPayException;
|
||||
import com.github.binarywang.wxpay.service.WxPayService;
|
||||
import com.github.binarywang.wxpay.testbase.ApiTestModule;
|
||||
import com.google.inject.Inject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.http.util.Asserts;
|
||||
import org.assertj.core.util.Lists;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.testng.annotations.Guice;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
/**
|
||||
* 商家转账到零钱(直连商户)- 商户号配置信息错误时健壮性判断单元测试
|
||||
* @author imyzt
|
||||
* created on 2022/10/23
|
||||
*/
|
||||
@Slf4j
|
||||
@Test
|
||||
@Guice(modules = ApiTestModule.class)
|
||||
public class AutoUpdateCertificatesVerifierTest {
|
||||
|
||||
private final Logger logger = LoggerFactory.getLogger(this.getClass());
|
||||
|
||||
@Inject
|
||||
private WxPayService payService;
|
||||
|
||||
@Test
|
||||
public void testVerify() throws WxPayException {
|
||||
TransferDetailList transferDetailList = new TransferDetailList();
|
||||
transferDetailList.setOutDetailNo("test")
|
||||
.setOpenid("test")
|
||||
.setTransferAmount(1)
|
||||
.setOutDetailNo("test")
|
||||
.setUserName("test");
|
||||
TransferCreateRequest req = TransferCreateRequest.builder()
|
||||
.appid("wxd930ea5d5a258f4f")
|
||||
.batchName("test")
|
||||
.outBatchNo("")
|
||||
.totalAmount(1)
|
||||
.totalNum(1)
|
||||
.transferDetailList(Lists.newArrayList(transferDetailList))
|
||||
.build();
|
||||
TransferCreateResult transfer = payService.getMerchantTransferService().createTransfer(req);
|
||||
Asserts.notNull(transfer, "transfer");
|
||||
|
||||
// 商户未申请过证书。请到商户平台上申请证书授权机构颁发的证书。详情可参考:http://kf.qq.com/faq/180824JvUZ3i180824YvMNJj.html
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user