问题遇到的现象和发生背景
执行顺序如图所示(2为方法名),第一次请求为200,没有进五号哪个if
我考虑是不是2是又调用了一次这个方法,但是通过debug发现再次运行到5时是没有statuscode等参数的,而且debug时他是直接跳到5的
问题相关代码
private void autoUpdateCert() throws IOException, GeneralSecurityException {
CloseableHttpClient httpClient = WechatPayHttpClientBuilder.create().withCredentials(this.credentials).withValidator((Validator)(this.verifier == null ? (responsex) -> {
return true;
} : new WechatPay2Validator(this.verifier))).build();
try {
HttpGet httpGet = new HttpGet("https://api.mch.weixin.qq.com/v3/certificates");
httpGet.addHeader("Accept", "application/json");
CloseableHttpResponse response = httpClient.execute(httpGet);
try {
int statusCode = response.getStatusLine().getStatusCode();
String body = EntityUtils.toString(response.getEntity());
if (statusCode != 200) {
log.warn("Auto update cert failed, statusCode = " + statusCode + ",body = " + body);
return;
}
List<X509Certificate> newCertList = this.deserializeToCerts(this.apiV3Key, body);
if (!newCertList.isEmpty()) {
this.verifier = new CertificatesVerifier(newCertList);
return;
}
log.warn("Cert list is empty");
} finally {
response.close();
}
} finally {
httpClient.close();
}
这是调用他的方法:
public AutoUpdateCertificatesVerifier(Credentials credentials, byte[] apiV3Key, int minutesInterval) {
this.lock = new ReentrantLock();
this.credentials = credentials;
this.apiV3Key = apiV3Key;
this.minutesInterval = minutesInterval;
try {
this.autoUpdateCert();
this.instant = Instant.now();
} catch (GeneralSecurityException | IOException var5) {
throw new RuntimeException(var5);
}
}
这是执行1之前的方法:
private List<X509Certificate> deserializeToCerts(byte[] apiV3Key, String body) throws GeneralSecurityException, IOException {
AesUtil decryptor = new AesUtil(apiV3Key);
ObjectMapper mapper = new ObjectMapper();
JsonNode dataNode = mapper.readTree(body).get("data");
List<X509Certificate> newCertList = new ArrayList();
if (dataNode != null) {
int i = 0;
for(int count = dataNode.size(); i < count; ++i) {
JsonNode encryptCertificateNode = dataNode.get(i).get("encrypt_certificate");
String cert = decryptor.decryptToString(encryptCertificateNode.get("associated_data").toString().replaceAll("\"", "").getBytes("utf-8"), encryptCertificateNode.get("nonce").toString().replaceAll("\"", "").getBytes("utf-8"), encryptCertificateNode.get("ciphertext").toString().replaceAll("\"", ""));
CertificateFactory cf = CertificateFactory.getInstance("X509");
X509Certificate x509Cert = (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(cert.getBytes("utf-8")));
try {
x509Cert.checkValidity();
} catch (CertificateNotYetValidException | CertificateExpiredException var14) {
continue;
}
newCertList.add(x509Cert);
}
}
return newCertList;
}