liujie
2026-09-17 819124be8f16794d409a67f58d5257aef37d9b64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package com.tourguide.common.huifu;
 
import com.alibaba.fastjson.JSONObject;
import com.huifu.bspay.sdk.opps.core.utils.RsaUtils;
import com.tourguide.common.huifu.properties.HuiFuProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
 
@Component
public class HuifuCallbackVerifier {
    private final HuiFuProperties properties;
 
    public HuifuCallbackVerifier(HuiFuProperties properties) {
        this.properties = properties;
    }
 
    public JSONObject verify(String sign, String respData) {
        if (!StringUtils.hasText(sign) || !StringUtils.hasText(respData)) {
            throw new IllegalArgumentException("汇付回调缺少签名参数");
        }
        if (!StringUtils.hasText(properties.getRsaPublicKey())) {
            throw new IllegalStateException("汇付公钥未配置");
        }
        String normalized = normalize(respData);
        if (!RsaUtils.verify(normalized, properties.getRsaPublicKey(), sign)) {
            throw new IllegalArgumentException("汇付回调验签失败");
        }
        return JSONObject.parseObject(normalized);
    }
 
    private String normalize(String respData) {
        return respData
                .replace("(", "(")
                .replace(")", ")")
                .replace("(", "(")
                .replace(")", ")")
                .replace("& #40;", "(")
                .replace("& #41;", ")");
    }
}