🎨 优化代码,未正确配置 apiV3Key 时,173 行证书更新失败,未做 throw 等阻断动作,导致 verifier 未赋值,后续其它方法使用将出现空指针异常

This commit is contained in:
杨镇涛
2022-10-23 14:38:13 +08:00
committed by GitHub
parent c1e7d095ed
commit 10414d9971
2 changed files with 71 additions and 12 deletions

View File

@@ -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");
}
}