Commit 6a2bdb01 authored by 张庆's avatar 张庆

新增挡板规则

parent f89391bd
......@@ -2,9 +2,9 @@ package com.jqtx.windows.component.enums;
public enum ZNPostUrlEnum {
CREDIT_CALL("/test_1", "授信状态推送Url"),
LOAN_CALL("/test_2", "借款状态推送Url"),
PROTOCOL_CALL("/test_3", "协议状态推送Url"),
CREDIT_CALL("creditNotify", "授信状态推送Url"),
LOAN_CALL("loadNotify", "借款状态推送Url"),
PROTOCOL_CALL("guaranteeSign", "协议状态推送Url"),
;
ZNPostUrlEnum(String code, String msg) {
......
package com.jqtx.windows.component.model;
import lombok.Data;
@Data
public class ZyHttpBaseResponse {
private String clientld;
private String serviceld;
private String segNo;
private String transDateTime;
private String data;
private String sign;
private String returnCode;
private String returnMsg;
}
package com.jqtx.windows.component.model;
import lombok.Data;
@Data
public class ZyHttpBaseRquest {
private String clientld;
private String serviceld;
private String segNo;
private String transDateTime;
private String data;
private String sign;
}
package com.jqtx.windows.utils;
import cn.hutool.core.lang.UUID;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jqtx.windows.component.model.ZyHttpBaseResponse;
import com.jqtx.windows.component.model.ZyHttpBaseRquest;
import com.jqtx.windows.web.request.AbcRequest;
import com.jqtx.windows.web.response.JsonResult;
import lombok.extern.slf4j.Slf4j;
......@@ -11,6 +14,8 @@ import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.time.LocalDateTime;
@Slf4j
@Component
public class AbcHttpClient {
......@@ -23,25 +28,41 @@ public class AbcHttpClient {
@Value("${zhenong.publicKey}")
private String ypdPublicKey;
private String appId = "660010";
public JsonResult postRequest(String paramJson, String method) {
@Value("${zhenong.clientId}")
private String clientId;
public ZyHttpBaseResponse postRequest(String paramJson, String method) {
try {
final String url = posetUrl + method;
AbcRequest request = AbcRsaUtil.encodeParam(paramJson, ypdPublicKey, appId, privateKey);
final String url = posetUrl;
ZyHttpBaseRquest zyHttpBaseRquest = new ZyHttpBaseRquest();
zyHttpBaseRquest.setClientld(clientId);
zyHttpBaseRquest.setServiceld(method);
zyHttpBaseRquest.setSegNo(UUID.fastUUID().toString());
zyHttpBaseRquest.setTransDateTime(LocalDateTime.now().toString());
String responseParam = EncryptUtil.encrypt(paramJson, AbcRsaUtil.getPublicKey(ypdPublicKey));
String responseSign = SignUtil.sign(responseParam, AbcRsaUtil.getPrivateKey(privateKey));
zyHttpBaseRquest.setData(responseParam);
zyHttpBaseRquest.setSign(responseSign);
log.info("请求浙农未加密数据:{}", JSON.toJSONString(paramJson));
log.info("请求浙农加密数据:{}", JSON.toJSONString(request));
log.info("请求浙农加密数据:{}", JSON.toJSONString(zyHttpBaseRquest));
log.info("请求浙农url:{}", url);
HttpResponse response = HttpRequest.post(url).body(JSON.toJSONString(request)).setConnectionTimeout(5000).execute();
HttpResponse response = HttpRequest.post(url).body(JSON.toJSONString(zyHttpBaseRquest)).setConnectionTimeout(5000).execute();
log.info("请求浙农加密返回数据:{}", JSON.toJSONString(response.body()));
if (response.getStatus() == 200 && StringUtils.isNotBlank(response.body())) {
final String body = response.body();
JSONObject responseJson = JSONObject.parseObject(body);
String content = responseJson.getString("content");
String data = responseJson.getString("data");
String sign = responseJson.getString("sign");
String responseParam = AbcRsaUtil.decodeParam(content, sign, privateKey, ypdPublicKey);
log.info("请求浙农解密返回数据:{}", JSON.toJSONString(responseParam));
return JSONObject.parseObject(responseParam, JsonResult.class);
boolean f = SignUtil.veriSign(data, sign, AbcRsaUtil.getPublicKey(ypdPublicKey));
if (f) {
String dec = EncryptUtil.decrypt(data, AbcRsaUtil.getPrivateKey(privateKey));
log.info("请求浙农解密返回数据:{}", JSON.toJSONString(dec));
return JSONObject.parseObject(dec, ZyHttpBaseResponse.class);
} else {
log.info("验签异常");
}
} else {
throw new Exception("请求异常");
}
......@@ -49,5 +70,6 @@ public class AbcHttpClient {
log.error("请求浙农异常:{}", e.getMessage(), e);
return null;
}
return null;
}
}
package com.jqtx.windows.utils;
import org.springframework.util.Base64Utils;
import org.springframework.util.StringUtils;
import javax.crypto.Cipher;
import java.io.ByteArrayOutputStream;
import java.security.PrivateKey;
import java.security.PublicKey;
/**
* @author: maweijun
* @description:
* @create: 2021/1/12
*/
public class EncryptUtil {
private static final String encryptAlgorithm = "RSA"; // 加密算法
private static final String decryptAlgorithm = "RSA"; // 解密算法
private static final String charset = "UTF-8";
private static final int maxEncryptBlock = 234; //2048位rsa单次最大加密长度
private static final int max_decrypt_block = 256; //2048位rsa单次最大解密长度
public static String encrypt(String param, PublicKey publicKey) {
if (publicKey == null) {
throw new RuntimeException("公钥未初始化");
}
if (StringUtils.isEmpty(param)) {
throw new IllegalArgumentException("待加密数据为空");
}
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
Cipher cipher = Cipher.getInstance(encryptAlgorithm);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] data = param.getBytes(charset);
int inputLen = data.length;
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段加密
while (inputLen - offSet > 0) {
if (inputLen > maxEncryptBlock + offSet) {
cache = cipher.doFinal(data, offSet, maxEncryptBlock);
} else {
cache = cipher.doFinal(data, offSet, inputLen - offSet);
}
out.write(cache, 0, cache.length);
i++;
offSet = i * maxEncryptBlock;
}
return Base64Utils.encodeToString(out.toByteArray());
} catch (Exception e) {
throw new RuntimeException("加密异常", e);
}
}
public static String decrypt(String param, PrivateKey partnerPrivateKey) {
if (partnerPrivateKey== null) {
throw new RuntimeException("私钥未初始化");
}
if (StringUtils.isEmpty(param)) {
throw new IllegalArgumentException("待解密数据为空");
}
try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
Cipher cipher = Cipher.getInstance(decryptAlgorithm);
cipher.init(Cipher.DECRYPT_MODE, partnerPrivateKey);
byte[] data = Base64Utils.decodeFromString(param);
int inputLen = data.length;
int offSet = 0;
byte[] cache;
int i = 0;
// 对数据分段解密
while (inputLen - offSet > 0) {
if (inputLen > max_decrypt_block + offSet) {
cache = cipher.doFinal(data, offSet, max_decrypt_block);
} else {
cache = cipher.doFinal(data, offSet, inputLen - offSet);
}
i++;
out.write(cache, 0, cache.length);
offSet = i * max_decrypt_block;
}
return new String(out.toByteArray(), charset);
} catch (Exception e) {
throw new RuntimeException("解密处理异常", e);
}
}
}
package com.jqtx.windows.utils;
import org.springframework.util.Base64Utils;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Signature;
/**
* @author: maweijun
* @description:
* @create: 2021/1/12
*/
public class SignUtil {
private static final String signature_algorithm = "SHA256withRSA"; // 签名算法
private static final String charset = "UTF-8";
public static String sign(String param, PrivateKey partnerPrivateKey) {
try {
if (partnerPrivateKey == null) {
throw new RuntimeException("私钥未初始化");
}
Signature signature = Signature.getInstance(signature_algorithm);
signature.initSign(partnerPrivateKey);
signature.update(param.getBytes(charset));
return Base64Utils.encodeToString(signature.sign());
} catch (Exception e) {
throw new RuntimeException("签名异常", e);
}
}
public static boolean veriSign(String param, String sign, PublicKey signPartnerPublicKey) {
try {
if (signPartnerPublicKey == null) {
return false;
}
Signature signature = Signature.getInstance(signature_algorithm);
signature.initVerify(signPartnerPublicKey);
signature.update(param.getBytes(charset));
return signature.verify(Base64Utils.decodeFromString(sign));
} catch (Exception e) {
}
return false;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment