Skip to content

Commit e24dd33

Browse files
authored
0.2.2 (#49)
* fix: #20, use getTarget() instead of getURI(), because the URI may be absolute URI (as above) or may be a relative URI. * fix: #42,#43 buildMessage()使用toString(entity, utf-8)从entity中获得请求报文 * update version to 0.2.2
1 parent a72e3db commit e24dd33

File tree

4 files changed

+103
-103
lines changed

4 files changed

+103
-103
lines changed

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
## 项目状态
1010

11-
当前版本`0.2.1`为测试版本。请商户的专业技术人员在使用时注意系统和软件的正确性和兼容性,以及带来的风险。
11+
当前版本`0.2.2`为测试版本。请商户的专业技术人员在使用时注意系统和软件的正确性和兼容性,以及带来的风险。
1212

1313
## 环境要求
1414

@@ -23,7 +23,7 @@
2323
在你的`build.gradle`文件中加入如下的依赖
2424

2525
```groovy
26-
implementation 'com.github.wechatpay-apiv3:wechatpay-apache-httpclient:0.2.1'
26+
implementation 'com.github.wechatpay-apiv3:wechatpay-apache-httpclient:0.2.2'
2727
```
2828

2929
### Maven
@@ -33,7 +33,7 @@ implementation 'com.github.wechatpay-apiv3:wechatpay-apache-httpclient:0.2.1'
3333
<dependency>
3434
<groupId>com.github.wechatpay-apiv3</groupId>
3535
<artifactId>wechatpay-apache-httpclient</artifactId>
36-
<version>0.2.1</version>
36+
<version>0.2.2</version>
3737
</dependency>
3838
```
3939

@@ -94,7 +94,7 @@ httpPost.addHeader("Content-type","application/json; charset=utf-8");
9494
ByteArrayOutputStream bos = new ByteArrayOutputStream();
9595
ObjectMapper objectMapper = new ObjectMapper();
9696

97-
ObjectNode rootNode = mapper.createObjectNode();
97+
ObjectNode rootNode = objectMapper.createObjectNode();
9898
rootNode.put("mchid","1900009191")
9999
.put("appid", "wxd678efh567hg6787")
100100
.put("description", "Image形象店-深圳腾大-QQ公仔")
@@ -105,7 +105,7 @@ rootNode.putObject("amount")
105105
rootNode.putObject("payer")
106106
.put("openid", "oUpF8uMuAJO_M2pxb1Q9zNjWeS6o");
107107

108-
mapper.writeValue(bos, rootNode);
108+
objectMapper.writeValue(bos, rootNode);
109109

110110
httpPost.setEntity(new StringEntity(bos.toString("UTF-8")));
111111
CloseableHttpResponse response = httpClient.execute(httpPost);
@@ -137,10 +137,10 @@ httpPost.addHeader("Content-type","application/json; charset=utf-8");
137137
ByteArrayOutputStream bos = new ByteArrayOutputStream();
138138
ObjectMapper objectMapper = new ObjectMapper();
139139

140-
ObjectNode rootNode = mapper.createObjectNode();
140+
ObjectNode rootNode = objectMapper.createObjectNode();
141141
rootNode.put("mchid","1900009191");
142142

143-
mapper.writeValue(bos, rootNode);
143+
objectMapper.writeValue(bos, rootNode);
144144

145145
httpPost.setEntity(new StringEntity(bos.toString("UTF-8")));
146146
CloseableHttpResponse response = httpClient.execute(httpPost);

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
}
66

77
group 'com.github.wechatpay-apiv3'
8-
version '0.2.1'
8+
version '0.2.2'
99

1010
sourceCompatibility = 1.8
1111
targetCompatibility = 1.8

src/main/java/com/wechat/pay/contrib/apache/httpclient/SignatureExec.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ protected void convertToRepeatableRequestEntity(HttpRequestWrapper request) thro
4545
@Override
4646
public CloseableHttpResponse execute(HttpRoute route, HttpRequestWrapper request,
4747
HttpClientContext context, HttpExecutionAware execAware) throws IOException, HttpException {
48-
if (request.getURI().getHost().endsWith(".mch.weixin.qq.com")) {
48+
if (request.getTarget().getHostName().endsWith(".mch.weixin.qq.com")) {
4949
return executeWithSignature(route, request, context, execAware);
5050
} else {
5151
return mainExec.execute(route, request, context, execAware);
Lines changed: 94 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,94 @@
1-
package com.wechat.pay.contrib.apache.httpclient.auth;
2-
3-
import com.wechat.pay.contrib.apache.httpclient.Credentials;
4-
import com.wechat.pay.contrib.apache.httpclient.WechatPayUploadHttpPost;
5-
import java.io.IOException;
6-
import java.net.URI;
7-
import java.nio.charset.StandardCharsets;
8-
import java.security.SecureRandom;
9-
10-
import org.apache.http.HttpEntityEnclosingRequest;
11-
import org.apache.http.client.methods.HttpRequestWrapper;
12-
import org.apache.http.util.EntityUtils;
13-
import org.slf4j.Logger;
14-
import org.slf4j.LoggerFactory;
15-
16-
public class WechatPay2Credentials implements Credentials {
17-
private static final Logger log = LoggerFactory.getLogger(WechatPay2Credentials.class);
18-
19-
private static final String SYMBOLS =
20-
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
21-
private static final SecureRandom RANDOM = new SecureRandom();
22-
protected String merchantId;
23-
protected Signer signer;
24-
25-
public WechatPay2Credentials(String merchantId, Signer signer) {
26-
this.merchantId = merchantId;
27-
this.signer = signer;
28-
}
29-
30-
public String getMerchantId() {
31-
return merchantId;
32-
}
33-
34-
protected long generateTimestamp() {
35-
return System.currentTimeMillis() / 1000;
36-
}
37-
38-
protected String generateNonceStr() {
39-
char[] nonceChars = new char[32];
40-
for (int index = 0; index < nonceChars.length; ++index) {
41-
nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
42-
}
43-
return new String(nonceChars);
44-
}
45-
46-
@Override
47-
public final String getSchema() {
48-
return "WECHATPAY2-SHA256-RSA2048";
49-
}
50-
51-
@Override
52-
public final String getToken(HttpRequestWrapper request) throws IOException {
53-
String nonceStr = generateNonceStr();
54-
long timestamp = generateTimestamp();
55-
56-
String message = buildMessage(nonceStr, timestamp, request);
57-
log.debug("authorization message=[{}]", message);
58-
59-
Signer.SignatureResult signature = signer.sign(message.getBytes(StandardCharsets.UTF_8));
60-
61-
String token = "mchid=\"" + getMerchantId() + "\","
62-
+ "nonce_str=\"" + nonceStr + "\","
63-
+ "timestamp=\"" + timestamp + "\","
64-
+ "serial_no=\"" + signature.certificateSerialNumber + "\","
65-
+ "signature=\"" + signature.sign + "\"";
66-
log.debug("authorization token=[{}]", token);
67-
68-
return token;
69-
}
70-
71-
protected final String buildMessage(String nonce, long timestamp, HttpRequestWrapper request)
72-
throws IOException {
73-
URI uri = request.getURI();
74-
String canonicalUrl = uri.getRawPath();
75-
if (uri.getQuery() != null) {
76-
canonicalUrl += "?" + uri.getRawQuery();
77-
}
78-
79-
String body = "";
80-
// PATCH,POST,PUT
81-
if (request.getOriginal() instanceof WechatPayUploadHttpPost) {
82-
body = ((WechatPayUploadHttpPost) request.getOriginal()).getMeta();
83-
} else if (request instanceof HttpEntityEnclosingRequest) {
84-
body = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity());
85-
}
86-
87-
return request.getRequestLine().getMethod() + "\n"
88-
+ canonicalUrl + "\n"
89-
+ timestamp + "\n"
90-
+ nonce + "\n"
91-
+ body + "\n";
92-
}
93-
94-
}
1+
package com.wechat.pay.contrib.apache.httpclient.auth;
2+
3+
import com.wechat.pay.contrib.apache.httpclient.Credentials;
4+
import com.wechat.pay.contrib.apache.httpclient.WechatPayUploadHttpPost;
5+
import org.apache.http.HttpEntityEnclosingRequest;
6+
import org.apache.http.client.methods.HttpRequestWrapper;
7+
import org.apache.http.util.EntityUtils;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
11+
import java.io.IOException;
12+
import java.net.URI;
13+
import java.nio.charset.StandardCharsets;
14+
import java.security.SecureRandom;
15+
16+
public class WechatPay2Credentials implements Credentials {
17+
private static final Logger log = LoggerFactory.getLogger(WechatPay2Credentials.class);
18+
19+
private static final String SYMBOLS =
20+
"0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
21+
private static final SecureRandom RANDOM = new SecureRandom();
22+
protected String merchantId;
23+
protected Signer signer;
24+
25+
public WechatPay2Credentials(String merchantId, Signer signer) {
26+
this.merchantId = merchantId;
27+
this.signer = signer;
28+
}
29+
30+
public String getMerchantId() {
31+
return merchantId;
32+
}
33+
34+
protected long generateTimestamp() {
35+
return System.currentTimeMillis() / 1000;
36+
}
37+
38+
protected String generateNonceStr() {
39+
char[] nonceChars = new char[32];
40+
for (int index = 0; index < nonceChars.length; ++index) {
41+
nonceChars[index] = SYMBOLS.charAt(RANDOM.nextInt(SYMBOLS.length()));
42+
}
43+
return new String(nonceChars);
44+
}
45+
46+
@Override
47+
public final String getSchema() {
48+
return "WECHATPAY2-SHA256-RSA2048";
49+
}
50+
51+
@Override
52+
public final String getToken(HttpRequestWrapper request) throws IOException {
53+
String nonceStr = generateNonceStr();
54+
long timestamp = generateTimestamp();
55+
56+
String message = buildMessage(nonceStr, timestamp, request);
57+
log.debug("authorization message=[{}]", message);
58+
59+
Signer.SignatureResult signature = signer.sign(message.getBytes(StandardCharsets.UTF_8));
60+
61+
String token = "mchid=\"" + getMerchantId() + "\","
62+
+ "nonce_str=\"" + nonceStr + "\","
63+
+ "timestamp=\"" + timestamp + "\","
64+
+ "serial_no=\"" + signature.certificateSerialNumber + "\","
65+
+ "signature=\"" + signature.sign + "\"";
66+
log.debug("authorization token=[{}]", token);
67+
68+
return token;
69+
}
70+
71+
protected final String buildMessage(String nonce, long timestamp, HttpRequestWrapper request)
72+
throws IOException {
73+
URI uri = request.getURI();
74+
String canonicalUrl = uri.getRawPath();
75+
if (uri.getQuery() != null) {
76+
canonicalUrl += "?" + uri.getRawQuery();
77+
}
78+
79+
String body = "";
80+
// PATCH,POST,PUT
81+
if (request.getOriginal() instanceof WechatPayUploadHttpPost) {
82+
body = ((WechatPayUploadHttpPost) request.getOriginal()).getMeta();
83+
} else if (request instanceof HttpEntityEnclosingRequest) {
84+
body = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity(), StandardCharsets.UTF_8);
85+
}
86+
87+
return request.getRequestLine().getMethod() + "\n"
88+
+ canonicalUrl + "\n"
89+
+ timestamp + "\n"
90+
+ nonce + "\n"
91+
+ body + "\n";
92+
}
93+
94+
}

0 commit comments

Comments
 (0)