区块链调用

This commit is contained in:
zhangqinbin
2021-08-31 18:11:11 +08:00
parent 91b3957c44
commit d51eb8dba5
4 changed files with 158 additions and 26 deletions

View File

@ -107,6 +107,19 @@ public class CrypConfigureController{
return BaseResponse.success(list);
}
/**
* 调用天擎接口
*
* @param bean
* @return
*/
@ApiOperation("调用天擎接口")
@PostMapping("/callUniInterface")
public BaseResponse<Boolean> callUniInterface(@RequestBody CrypBean bean) {
return BaseResponse.success(this.iCrypConfigureService.callUniInterface(bean));
}
/**
* 区块链数据加密
*

View File

@ -1,17 +1,7 @@
package com.chinaunicom.mall.ebtp.extend.crypconfigure.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.chinaunicom.mall.ebtp.common.base.entity.BaseEntity;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;
import java.io.Serializable;
import java.time.LocalDateTime;
/**
* 实体类 CrypConfigure-区块链参数配置表
@ -25,11 +15,16 @@ public class CrypBean {
* 签名
*/
@ApiModelProperty(value = "签名")
public String sing;
public String sign;
/**
* 待签名参数
* 待签名参数 Map
*/
@ApiModelProperty(value = "待签名参数")
public Object object;
/**
* 天擎接口地址
*/
@ApiModelProperty(value = "天擎接口地址")
public String url;
}

View File

@ -11,6 +11,12 @@ import com.chinaunicom.mall.ebtp.extend.crypconfigure.entity.CrypConfigure;
*
*/
public interface ICrypConfigureService extends IBaseService<CrypConfigure> {
/**
* 调用天擎接口
* @param bean
* @return
*/
Boolean callUniInterface(CrypBean bean);
/**
* 加密

View File

@ -1,18 +1,39 @@
package com.chinaunicom.mall.ebtp.extend.crypconfigure.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.chinaunicom.mall.ebtp.common.base.service.impl.BaseServiceImpl;
import com.chinaunicom.mall.ebtp.common.constant.CommonConstants;
import com.chinaunicom.mall.ebtp.common.crypto.service.CrypServiceImpl;
import com.chinaunicom.mall.ebtp.common.exception.common.CommonExceptionEnum;
import com.chinaunicom.mall.ebtp.common.uniBss.constant.UniBssConstant;
import com.chinaunicom.mall.ebtp.common.uniBss.entity.UniBss;
import com.chinaunicom.mall.ebtp.common.uniBss.entity.UniBssAttached;
import com.chinaunicom.mall.ebtp.common.uniBss.entity.UniBssBody;
import com.chinaunicom.mall.ebtp.common.uniBss.entity.UniBssHead;
import com.chinaunicom.mall.ebtp.common.uniBss.service.UniBssServiceImpl;
import com.chinaunicom.mall.ebtp.common.util.PropertyUtils;
import com.chinaunicom.mall.ebtp.extend.blockchain.entity.BlockChainLog;
import com.chinaunicom.mall.ebtp.extend.blockchain.service.IBlockChainLogService;
import com.chinaunicom.mall.ebtp.extend.crypconfigure.dao.CrypConfigureMapper;
import com.chinaunicom.mall.ebtp.extend.crypconfigure.entity.CrypBean;
import com.chinaunicom.mall.ebtp.extend.crypconfigure.entity.CrypConfigure;
import com.chinaunicom.mall.ebtp.extend.crypconfigure.service.ICrypConfigureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static com.chinaunicom.mall.ebtp.common.uniBss.service.UniBssServiceImpl.MD5min;
/**
* 对数据表 biz_bid_cryp_configure 操作的 serviceImpl
@ -21,6 +42,61 @@ import java.util.List;
*/
@Service
public class CrypConfigureServiceImpl extends BaseServiceImpl<CrypConfigureMapper,CrypConfigure> implements ICrypConfigureService {
@Autowired
private IBlockChainLogService iBlockChainLogService;
@Value("${mconfig.bss.app-id}")
private String app_id;
@Value("${mconfig.bss.app-secret}")
private String app_secret;
/**
* 调用天擎接口
* @param bean
* @return
*/
@Override
public Boolean callUniInterface(CrypBean bean){
BlockChainLog log = new BlockChainLog();
log.setId(PropertyUtils.getSnowflakeId());
//天擎地址
log.setInterfaceUrl(bean.getUrl());
try {
//传入数据解密
String sign = getSignValue(bean.getObject());
Map<String, String> map = JSONArray.parseObject(JSONArray.toJSONString(bean.getObject()), Map.class);
map.put("sign", sign);
String json = getUniBss(map);
log.setResult("天擎接口调用,地址:"+bean.getUrl()+",参数:"+json);//日志
log.setParam(json);
String str = UniBssServiceImpl.uniBssHttpPost(bean.getUrl(), json);
UniBss uniBssRsp = JSONArray.parseObject(str, UniBss.class);
if (uniBssRsp != null && UniBssConstant.RESP_CODE_00000.equals(uniBssRsp.getUniBssHead().getRespCode())) {
log.setStatus(0);//成功
return true;
} else {
log.setStatus(1);//失败
CommonExceptionEnum.FRAME_EXCEPTION_COMMON_DATA_OTHER_ERROR.assertStringNotNullByKey("天擎接口调用错误," +
"RESP_CODE:" + uniBssRsp.getUniBssHead().getRespCode() + "" +
"(" + UniBssConstant.getRESP_CODE_Map(uniBssRsp.getUniBssHead().getRespCode()) + ")。" +
"RESP_DESC:" + uniBssRsp.getUniBssHead().getRespDesc(), bean);
}
}catch (Exception e){
log.setStatus(1);
log.setResult(e.getMessage());
}
this.iBlockChainLogService.save(log);
return false;
}
/**
* 加密
* @param object
@ -31,30 +107,72 @@ public class CrypConfigureServiceImpl extends BaseServiceImpl<CrypConfigureMappe
CrypBean bean = new CrypBean();
try{
bean.setObject(object);
//查询
LambdaQueryWrapper<CrypConfigure> query = Wrappers.lambdaQuery();
query.eq(CrypConfigure::getState,"1")
.eq(CrypConfigure::getDeleteFlag, CommonConstants.STATUS_NORMAL)
.eq(CrypConfigure::getType,"1")
.eq(CrypConfigure::getCCode,"pem_key");
List<CrypConfigure> list = this.list(query);
String signValue = getSignValue(bean.getObject());
CrypConfigure crypConfigure = list.get(0);
String signValue = CrypServiceImpl.signObject2(bean.getObject(),crypConfigure.getCValue());
bean.setSing(signValue);
bean.setSign(signValue);
return bean;
}catch (Exception e){
log.error("加密异常:"+e);
}
return bean;
}
/**
* 获取加密签名
* @param object
* @return
*/
private String getSignValue(Object object){
//查询
LambdaQueryWrapper<CrypConfigure> query = Wrappers.lambdaQuery();
query.eq(CrypConfigure::getState,"1")
.eq(CrypConfigure::getDeleteFlag, CommonConstants.STATUS_NORMAL)
.eq(CrypConfigure::getType,"1")
.eq(CrypConfigure::getCCode,"pem_key");
List<CrypConfigure> list = this.list(query);
CrypConfigure crypConfigure = list.get(0);
String signValue = CrypServiceImpl.signObject2(object,crypConfigure.getCValue());
return signValue;
}
/**
* 获取加密签名
* @param map
* @return
*/
private String getUniBss(Map<String,String> map){
//获取token
Date date = new Date();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
SimpleDateFormat format2 = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String TIMESTAMP = format.format(date);
String TRANS_ID = format2.format(date) + (int) ((Math.random() * 9 + 1) * 100000);
String s = "APP_ID" + app_id + "TIMESTAMP" + TIMESTAMP + "TRANS_ID" + TRANS_ID + app_secret;
String token = MD5min(s);
UniBss uniBss = new UniBss();
uniBss.setUniBssAttached(new UniBssAttached().setMediaInf(""));
UniBssHead head = new UniBssHead();
head.setAppId(app_id);
head.setTimeStamp(TIMESTAMP);
head.setTransId(TRANS_ID);
head.setToken(token);
uniBss.setUniBssHead(head);
UniBssBody body = new UniBssBody();
body.setSingleOrderQryReq(map);
uniBss.setUniBssBody(body);
return JSON.toJSONString(uniBss);
}
/**
* 解密
* @param bean
@ -74,7 +192,7 @@ public class CrypConfigureServiceImpl extends BaseServiceImpl<CrypConfigureMappe
log.debug("---------获取解密秘钥--------");
CrypConfigure crypConfigure = list.get(0);
Boolean b = CrypServiceImpl.verifyValue(bean.getSing(),
Boolean b = CrypServiceImpl.verifyValue(bean.getSign(),
bean.getObject(),
crypConfigure.getCValue());
return b;