Skip to content

增加日志打印 #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ WechatPayHttpClientBuilder builder = WechatPayHttpClientBuilder.create()
CloseableHttpClient httpClient = builder.build();

// 后面跟使用Apache HttpClient一样
ClosableHttpResponse response = httpClient.execute(...);
CloseableHttpResponse response = httpClient.execute(...);
```

参数说明:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.apache.http.HttpStatus.SC_OK;

import java.io.IOException;
import java.util.Arrays;
import org.apache.http.HttpEntity;
import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpException;
Expand All @@ -16,13 +17,17 @@
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.execchain.ClientExecChain;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author xy-peng
*/
public class SignatureExec implements ClientExecChain {

private static final String WECHAT_PAY_HOST_NAME_SUFFIX = ".mch.weixin.qq.com";
private static final Logger log = LoggerFactory.getLogger(SignatureExec.class);
private final ClientExecChain mainExec;
private final Credentials credentials;
private final Validator validator;
Expand All @@ -41,7 +46,7 @@ protected void convertToRepeatableResponseEntity(CloseableHttpResponse response)
}

protected void convertToRepeatableRequestEntity(HttpRequestWrapper request) throws IOException {
if (request instanceof HttpEntityEnclosingRequest) {
if (isEntityEnclosing(request)) {
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
if (entity != null) {
((HttpEntityEnclosingRequest) request).setEntity(new BufferedHttpEntity(entity));
Expand All @@ -59,26 +64,41 @@ public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request
}
}

private boolean isEntityEnclosing(HttpRequestWrapper request) {
return request instanceof HttpEntityEnclosingRequest;
}

private boolean isUploadHttpPost(HttpRequestWrapper request) {
return request.getOriginal() instanceof WechatPayUploadHttpPost;
}

private CloseableHttpResponse executeWithSignature(HttpRoute route, HttpRequestWrapper request,
HttpClientContext context,
HttpExecutionAware execAware) throws IOException, HttpException {
// 上传类不需要消耗两次故不做转换
if (!(request.getOriginal() instanceof WechatPayUploadHttpPost)) {
if (!isUploadHttpPost(request)) {
convertToRepeatableRequestEntity(request);
}
// 添加认证信息
request.addHeader(AUTHORIZATION, credentials.getSchema() + " " + credentials.getToken(request));

// 执行
CloseableHttpResponse response = mainExec.execute(route, request, context, execAware);

// 对成功应答验签
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() >= SC_OK && statusLine.getStatusCode() < SC_MULTIPLE_CHOICES) {
convertToRepeatableResponseEntity(response);
if (!validator.validate(response)) {
throw new HttpException("应答的微信支付签名验证失败");
}
} else {
// 错误应答需要打日志
log.error("应答的状态码不为200-299。status code[{}]\trequest headers[{}]", statusLine.getStatusCode(),
Arrays.toString(request.getAllHeaders()));
if (isEntityEnclosing(request) && !isUploadHttpPost(request)) {
HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
String body = EntityUtils.toString(entity);
log.error("应答的状态码不为200-299。request body[{}]", body);
}
}
return response;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
import java.io.InputStream;
import java.net.URI;
import java.net.URLConnection;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author xy-peng
*/
public class WechatPayUploadHttpPost extends HttpPost {

private final String meta;
private static final Logger log = LoggerFactory.getLogger(WechatPayUploadHttpPost.class);

private WechatPayUploadHttpPost(URI uri, String meta) {
super(uri);
Expand Down Expand Up @@ -80,10 +84,12 @@ public WechatPayUploadHttpPost build() {
WechatPayUploadHttpPost request = new WechatPayUploadHttpPost(uri, meta);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.RFC6532)
.addBinaryBody("file", fileInputStream, fileContentType, fileName)
.addTextBody("meta", meta, APPLICATION_JSON);
request.setEntity(entityBuilder.build());
.addTextBody("meta", meta, APPLICATION_JSON)
.addBinaryBody("file", fileInputStream, fileContentType, fileName);
HttpEntity entity = entityBuilder.build();
request.setEntity(entity);
request.addHeader(ACCEPT, APPLICATION_JSON.toString());
log.debug("request content-type[{}]", entity.getContentType());
return request;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author xy-peng
*/
public class CertificatesVerifier implements Verifier {

private static final Logger log = LoggerFactory.getLogger(CertificatesVerifier.class);
protected final HashMap<BigInteger, X509Certificate> certificates = new HashMap<>();

public CertificatesVerifier(List<X509Certificate> list) {
Expand Down Expand Up @@ -57,7 +60,11 @@ protected boolean verify(X509Certificate certificate, byte[] message, String sig
public boolean verify(String serialNumber, byte[] message, String signature) {
BigInteger val = new BigInteger(serialNumber, 16);
X509Certificate cert = certificates.get(val);
return cert != null && verify(cert, message, signature);
if (cert == null) {
log.error("找不到证书序列号对应的证书,序列号:{}", serialNumber);
return false;
}
return verify(cert, message, signature);
}

@Override
Expand Down