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;", ")"); } }