AlipayHK 回调及相关数据处理

This commit is contained in:
2024-05-31 16:28:23 +08:00
parent 1aea8e93f2
commit 86e7119cfb
15 changed files with 84 additions and 60 deletions

View File

@@ -50,6 +50,9 @@ public class AlipayHKEncryptionUtil {
@Value("${alipayHK.rsaPublicKey}")
private String publicKeyPath;
@Value("${alipayHK.CODPublicKey}")
private String CODPublicKeyPath;
/**
* 加密
@@ -70,7 +73,8 @@ public class AlipayHKEncryptionUtil {
// The path to the rsa private key file, DO NOT save this key to a publicly accessible location
// 使用私钥创建数字签名
// Mode, aes-128-cbc for 128bit key
String mode = "aes-128-cbc";
// String mode = "aes-128-cbc";
String mode = "aes-256-cbc";
// Padding mode
String padding = "pkcs7";
// signature alogrithm
@@ -84,14 +88,6 @@ public class AlipayHKEncryptionUtil {
requestMessage.setRequest_uuid(UUID.randomUUID().toString());
// HashMap<String, Object> param = new HashMap<>();
requestMessage.setParameters(param);
/*String orderRef = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
param.put("order_ref", orderRef);
param.put("amount", 208.12);
param.put("subject", "四月帳單 April Bill");
param.put("wallet", "ALIPAYHK");
param.put("segment_id", segmentId);
param.put("payment_solution", "WAP");*/
// Serialize message body
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
@@ -342,18 +338,25 @@ public class AlipayHKEncryptionUtil {
Base64.Decoder decoder = Base64.getDecoder();
// Verify key
try {
// PublicKey publicKey = readPublicKey(new File(publicKeyPath));
InputStreamReader isrPub = new InputStreamReader(new FileInputStream(publicKeyPath));
// 从指定的路径读取公钥文件
InputStreamReader isrPub = new InputStreamReader(new FileInputStream(CODPublicKeyPath));
PEMParser pemParserPub = new PEMParser(isrPub);
// 使用 PEMParser 解析公钥文件
SubjectPublicKeyInfo pubInfo = (SubjectPublicKeyInfo) pemParserPub.readObject();
// 从 SubjectPublicKeyInfo 创建 AsymmetricKeyParameter 对象
AsymmetricKeyParameter pubKey = PublicKeyFactory.createKey(pubInfo);
// Verifying
// Verifying; 创建 RSADigestSigner 对象并使用 SHA-256 作为摘要算法
RSADigestSigner verifier = new RSADigestSigner(new SHA256Digest());
// 初始化 RSADigestSigner设置为验证模式并提供公钥参数
verifier.init(false, pubKey);
byte[] signMessageForVerifing = data.getBytes();
byte[] signatureBase64ForVerifing = decoder.decode(signatureBase64);
verifier.update(signMessageForVerifing, 0, signMessageForVerifing.length);
Boolean verifyResult = verifier.verifySignature(signatureBase64ForVerifing);
// 将要验证的数据转换为字节数组
byte[] signMessageForVerifying = data.getBytes();
// 将 Base64 编码的签名转换为字节数组
byte[] signatureBase64ForVerifying = decoder.decode(signatureBase64);
// 使用签名器更新要验证的数据
verifier.update(signMessageForVerifying, 0, signMessageForVerifying.length);
// 验证签名并返回结果
Boolean verifyResult = verifier.verifySignature(signatureBase64ForVerifying);
return verifyResult;
} catch (Exception e) {
throw new RuntimeException(e);

View File

@@ -17,7 +17,7 @@ import org.springframework.stereotype.Component;
@Component
public class AlipayHKRequestUtil {
public String requestAlipayHK(AlipayHKRequestDTO alipayHKRequestDTO) throws IOException {
public String requestAlipayHK(AlipayHKRequestDTO alipayHKRequestDTO) {
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
@@ -29,7 +29,7 @@ public class AlipayHKRequestUtil {
log.info("Alipay-HK send request unix timestamp: {}", epochMilli);
String jsonString = JSONObject.toJSONString(alipayHKRequestDTO, SerializerFeature.WriteMapNullValue);
log.info(jsonString);
// log.info(jsonString);
RequestBody body = RequestBody.create(mediaType, jsonString);