|
| 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