Skip to content

Commit d6db45d

Browse files
authored
Merge pull request #26 from alipay/optimize_webhook_signature
add webhook signature verify tool
2 parents 9161853 + 183fac3 commit d6db45d

File tree

5 files changed

+52
-9
lines changed

5 files changed

+52
-9
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ https://mvnrepository.com/artifact/com.alipay.global.sdk/global-open-sdk-java
1212
<dependency>
1313
<groupId>com.alipay.global.sdk</groupId>
1414
<artifactId>global-open-sdk-java</artifactId>
15-
<version>2.0.21</version>
15+
<version>2.0.22</version>
1616
</dependency>
1717
```
1818

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.alipay.global.sdk</groupId>
55
<artifactId>global-open-sdk-java</artifactId>
66
<packaging>jar</packaging>
7-
<version>2.0.21</version>
7+
<version>2.0.22</version>
88
<name>global-open-sdk-java</name>
99
<url>https://github.com/alipay/global-open-sdk-java</url>
1010
<description>

src/main/java/com/alipay/global/api/DefaultAlipayClient.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package com.alipay.global.api;
22

3+
import java.util.Map;
4+
35
import com.alipay.global.api.exception.AlipayApiException;
46
import com.alipay.global.api.net.DefaultHttpRPC;
57
import com.alipay.global.api.net.HttpRpcResult;
68

7-
import java.util.Map;
8-
9-
public class DefaultAlipayClient extends BaseAlipayClient{
9+
public class DefaultAlipayClient extends BaseAlipayClient {
1010

11-
public DefaultAlipayClient(String gatewayUrl, String merchantPrivateKey, String alipayPublicKey ){
11+
public DefaultAlipayClient(String gatewayUrl, String merchantPrivateKey, String alipayPublicKey) {
1212
super(gatewayUrl, merchantPrivateKey, alipayPublicKey);
1313
}
1414

@@ -17,11 +17,11 @@ public Map<String, String> buildCustomHeader() {
1717
return null;
1818
}
1919

20-
public HttpRpcResult sendRequest(String requestUrl, String httpMethod, Map<String, String> header, String reqBody)throws AlipayApiException {
20+
public HttpRpcResult sendRequest(String requestUrl, String httpMethod, Map<String, String> header, String reqBody) throws AlipayApiException {
2121
HttpRpcResult httpRpcResult;
2222
try {
2323
httpRpcResult = DefaultHttpRPC.doPost(requestUrl, header, reqBody);
24-
} catch (Exception e){
24+
} catch (Exception e) {
2525
throw new AlipayApiException(e);
2626
}
2727
return httpRpcResult;

src/main/java/com/alipay/global/api/model/constants/ProductSceneConstants.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.alipay.global.api.model.constants;
22

33
/**
4-
* @Author yanhong
4+
* @author yanhong
55
* @version $Id: productSceneConstants.java, v 0.1 2024年03月14日 5:02 PM yanhong Exp $
66
**/
77
public class ProductSceneConstants {
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.alipay.global.api.tools;
2+
3+
import com.alipay.global.api.exception.AlipayApiException;
4+
5+
public class WebhookTool {
6+
7+
/**
8+
* Check webhook signature
9+
*
10+
* @param requestUri your webhook endpoint, domain part excluded, sample: /payNotify
11+
* @param httpMethod http method
12+
* @param clientId your clientId, sample: SANDBOX_5X00000000000000
13+
* @param requestTime requestTime from http header, sample: 2019-01-01T01:01:01Z
14+
* @param signature signature from http header, sample: algorithm=RSA256,keyVersion=1,signature=xxx
15+
* @param notifyBody notify body
16+
* @param alipayPublicKey alipay public key
17+
* @return
18+
* @throws AlipayApiException
19+
*/
20+
public static boolean checkSignature(String requestUri, String httpMethod, String clientId, String requestTime,
21+
String signature, String notifyBody, String alipayPublicKey) throws AlipayApiException {
22+
String realSignature = "";
23+
24+
// get valid part from raw signature
25+
if (signature == null || signature.isEmpty()) {
26+
throw new RuntimeException("empty notify signature");
27+
} else {
28+
String[] parts = signature.split("signature=");
29+
if (parts.length > 1) {
30+
realSignature = parts[1];
31+
}
32+
}
33+
34+
try {
35+
// verify signature
36+
return SignatureTool.verify(httpMethod, requestUri, clientId, requestTime, notifyBody, realSignature, alipayPublicKey);
37+
} catch (Exception e) {
38+
throw new AlipayApiException(e);
39+
}
40+
41+
}
42+
43+
}

0 commit comments

Comments
 (0)