|
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