|
@ -0,0 +1,123 @@
|
|
|
package com.yihu.jw.entrance.util.zysoft;
|
|
|
|
|
|
import cn.com.zoe.crypto.Base64;
|
|
|
import cn.com.zoe.crypto.jna.CryptoType;
|
|
|
import com.yihu.jw.entrance.config.SystemConfig;
|
|
|
import com.zoe.phip.ssp.sdk.ApiException;
|
|
|
import com.zoe.phip.ssp.sdk.HeaderValue;
|
|
|
import com.zoe.phip.ssp.sdk.RequestMethod;
|
|
|
import com.zoe.phip.ssp.sdk.RequestValue;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import java.io.InputStream;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* SDK调用代码参考
|
|
|
*/
|
|
|
@Service
|
|
|
public class SDKRunnerService {
|
|
|
|
|
|
private static String platformPublicKey;
|
|
|
private static String companyPrivateKeyBase64String;
|
|
|
private static String companyPublicKeyBase64String;
|
|
|
private static String authorizationCode;
|
|
|
private static String gateway_url;
|
|
|
|
|
|
static {
|
|
|
platformPublicKey = getEncryptData(SystemConfig.getInstance().getPlatformPublicKey());
|
|
|
companyPrivateKeyBase64String = getEncryptData(SystemConfig.getInstance().getCompanyPrivateKey());
|
|
|
companyPublicKeyBase64String = getEncryptData(SystemConfig.getInstance().getCompanyPublicKey());
|
|
|
// platformPublicKey = SystemConfig.getInstance().getPlatformPublicKey();
|
|
|
// companyPrivateKeyBase64String = SystemConfig.getInstance().getCompanyPrivateKey();
|
|
|
// companyPublicKeyBase64String = SystemConfig.getInstance().getCompanyPublicKey();
|
|
|
// 平台分配的授权码
|
|
|
authorizationCode = SystemConfig.getInstance().getAuthorizationCode();
|
|
|
gateway_url = SystemConfig.getInstance().getGatewayUrl();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
*
|
|
|
* @param api
|
|
|
* @param params
|
|
|
* @param header
|
|
|
* @param openCrypto 是否启用平台加密
|
|
|
* @param urlEncode url编码
|
|
|
* @return
|
|
|
*/
|
|
|
public String post(String api, Map<String,String> params,Map<String,String> header,boolean openCrypto,boolean urlEncode){
|
|
|
String result = null;
|
|
|
try {
|
|
|
// 密钥类型(RSA、SM2、SM2V2)
|
|
|
CryptoType type = CryptoType.SM2_V2;
|
|
|
// 接口地址
|
|
|
String apiUrl = gateway_url+api;
|
|
|
// 参数
|
|
|
List<RequestValue> parameters = new ArrayList<>();
|
|
|
if(params!=null)
|
|
|
{
|
|
|
// 普通参数请求示例
|
|
|
// new RequestValue("msgHeader", "<?xml version=\"1.0\" encoding=\"utf-8\"?><ROOT></ROOT>", true, openCrypto),
|
|
|
// new RequestValue("msgBody", "<?xml version=\"1.0\" encoding=\"utf-8\"?><ROOT></ROOT>", true, openCrypto)
|
|
|
// new RequestValue("idCard", "123", true, openCrypto)
|
|
|
//实体参数请求示例
|
|
|
// new RequestValue("{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":\"value3\"}")
|
|
|
for(String key :params.keySet())
|
|
|
{
|
|
|
RequestValue obj = new RequestValue(key,params.get(key),urlEncode,openCrypto);
|
|
|
parameters.add(obj);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
HeaderValue[] headers = new HeaderValue[header.size()];
|
|
|
// headers[0] = new HeaderValue("Content-Type","application/json",true);
|
|
|
int i = 0;
|
|
|
for(String key :header.keySet())
|
|
|
{
|
|
|
headers[i] = new HeaderValue(key,header.get(key),urlEncode);
|
|
|
i++;
|
|
|
}
|
|
|
// 请求方式
|
|
|
RequestMethod method = RequestMethod.POST;
|
|
|
//实例化需要使用的接口对象
|
|
|
CallApiGatewaySample runnable =
|
|
|
new CallApiGatewaySample(platformPublicKey, companyPublicKeyBase64String, companyPrivateKeyBase64String)
|
|
|
.setApiUrl(apiUrl)
|
|
|
.setParameters(parameters)
|
|
|
.setHeaders(headers)
|
|
|
.setMethod(method);
|
|
|
|
|
|
// 运行Api进行数据请求
|
|
|
result = runnable.run(authorizationCode, openCrypto, type);
|
|
|
} catch (ApiException e) {
|
|
|
System.out.println("Api请求失败:");
|
|
|
System.out.println("\t 编码: " + e.errorCode());
|
|
|
System.out.println("\t 信息: " + e.getMessage());
|
|
|
e.printStackTrace();
|
|
|
} catch (Exception ex) {
|
|
|
ex.printStackTrace();
|
|
|
}
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
private static String getEncryptData(String value) {
|
|
|
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(value);
|
|
|
try {
|
|
|
if (stream != null && stream.available() > 0) {
|
|
|
byte[] bytes = new byte[stream.available()];
|
|
|
stream.read(bytes);
|
|
|
return Base64.getEncoder().encodeToString(bytes);
|
|
|
} else {
|
|
|
System.out.println("未找到密钥文件:"+value);
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
|
System.out.println("读取密钥文件出错:"+value);
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
}
|