前几天和其他公司系统做对接,每隔5分钟把数据推到对方系统。
sign生成规则:sm3(年月日+单位编码+byzl@smart)
sid=sm4(
{
"depaCode":"4403",
"sign":"11111",
"registerDeskAmount":1,
"inoculateDeskAmount":1,
"watchingAmount":1,
"serviceAmount":1
})
secretKey密钥
V3UvdKISG6BnBEqq
示例
http://192.168.199.62:9778/smart/api/getSmartScreenDigital
引入maven依赖
<!--使用SM工具类 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.2.5</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.62</version>
</dependency>
封装SM加密工具类
package com.jwx.digital.client.util; import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.StrUtil; import cn.hutool.crypto.SmUtil; import cn.hutool.crypto.asymmetric.KeyType; import cn.hutool.crypto.asymmetric.SM2; import cn.hutool.crypto.symmetric.SymmetricCrypto; import lombok.extern.slf4j.Slf4j; /** * SMUtils:SM加密工具类 * @Date 2020-10-29 * @version 1.0 */ @Slf4j public class SMUtils { /** * 默认的 sm4加密key */ public static final String DEFAULT_SM4_SECRET_KEY = "123456"; /** * SM2加密例子 * @param data 需要加密的字符串 * @return 返回加密后的字符串 */ public static String toSM2Str(String data) { SM2 sm2 = SmUtil.sm2(); // 公钥加密,私钥解密 String encryptStr = sm2.encryptBcd(data, KeyType.PublicKey); String decryptStr = StrUtil.utf8Str(sm2.decryptFromBcd(encryptStr, KeyType.PrivateKey)); return encryptStr; } /** * SM3加密 * @param data 需要加密的字符串 * @return 返回加密后的字符串 */ public static String toSM3Str(String data) { return SmUtil.sm3(data).toUpperCase(); } /** * SM4加密 * @param data 需要加密的字符串 * @param secretKey 需要加密的字符串 * @return 返回加密后的字符串 */ public static String toSM4Str(String data,String secretKey) { SymmetricCrypto sm4 = SmUtil.sm4(secretKey.getBytes(CharsetUtil.CHARSET_UTF_8)); return sm4.encryptHex(data, CharsetUtil.CHARSET_UTF_8).toUpperCase(); } public static String toSM4Str(String data) { return toSM4Str(data,DEFAULT_SM4_SECRET_KEY); } /** * SM4解密 * @param data 需要加密的字符串 * @param secretKey 需要加密的字符串 * @return 返回加密后的字符串 */ public static String parseSM4Str(String data,String secretKey) { SymmetricCrypto sm4 = SmUtil.sm4(secretKey.getBytes(CharsetUtil.CHARSET_UTF_8)); return sm4.decryptStr(data, CharsetUtil.CHARSET_UTF_8); } public static String parseSM4Str(String data) { return parseSM4Str(data,DEFAULT_SM4_SECRET_KEY); } }
参数实体
package com.jwx.digital.client.pojo.dto.ybzl; import com.jwx.commons.core.pojo.BaseDTO; import lombok.Data; /** * YbzlSmartScreenDigitalDTO * * @author 红叶如醉 * @version 1.0 * @Date 2020-10-28 */ @Data public class YbzlSmartScreenDigitalDTO extends BaseDTO { /** * 单位编码 */ private String depaCode; /** *校验码 */ private String sign; /** *当天(所有)登记台排队人数 */ private Integer registerDeskAmount; /** *当天(所有)接种台排队人数 */ private Integer inoculateDeskAmount; /** *当天(所有)留观排队人数 */ private Integer watchingAmount; /** *当天(所有)服务人数 */ private Integer serviceAmount; }
参数加密,调用外部接口
/** * 统计该门诊人数情况 * * @return */ @Override public YbzlSmartScreenDigitalDTO CountSmartScreenDigital() { YbzlSmartScreenDigitalDTO ybzlSmartScreenDigitalDTO = new YbzlSmartScreenDigitalDTO(); ybzlSmartScreenDigitalDTO.setDepaCode(depaCode); // sign生成规则:sm3(年月日+单位编码+ybzl@cybermax) SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(format.format(new Date())).append(depaCode).append(serverUrlProperties.getSignSecret()); ybzlSmartScreenDigitalDTO.setSign(SMUtils.toSM3Str(stringBuilder.toString())); Example example = new Example(TicketActionDO.class); example.createCriteria().andEqualTo("prtyCode", ProcedureTypeEnum.REGISTRATION.getValue()) .andGreaterThan("tiacCreateTime", DateUtil.getCurrentDay()); //登记总人数 int registerDeskAmount = ticketActionMapper.selectCountByExample(example); ybzlSmartScreenDigitalDTO.setRegisterDeskAmount(registerDeskAmount); example.clear(); example.createCriteria().andEqualTo("prtyCode", ProcedureTypeEnum.VACCINATION.getValue()) .andGreaterThan("tiacCreateTime", DateUtil.getCurrentDay()); //接种总人数 ybzlSmartScreenDigitalDTO.setInoculateDeskAmount(ticketActionMapper.selectCountByExample(example)); example.clear(); example.createCriteria().andEqualTo("prtyCode", ProcedureTypeEnum.WATCH.getValue()) .andEqualTo("tiacStatus", TicketActionStatusEnum.PROCESSING.getValue()) .andGreaterThan("tiacCreateTime", DateUtil.getCurrentDay()); //正在留观人数,不包括完成 ybzlSmartScreenDigitalDTO.setWatchingAmount(ticketActionMapper.selectCountByExample(example)); //当天服务人数 == 登记的人数 ybzlSmartScreenDigitalDTO.setServiceAmount(registerDeskAmount); String sid = SMUtils.toSM4Str(JSON.toJSONString(ybzlSmartScreenDigitalDTO), serverUrlProperties.getSecretKey()); Response<OuterResult> resultResponse; try { ybzlInterface = RetrofitFactory.getYbzlApi(); log.info("推送登记接种留观服务人数到服务{} 数据内容{} ====== start", serverUrlProperties.getYbzlUrl(),JSON.toJSONString(ybzlSmartScreenDigitalDTO)); resultResponse = ybzlInterface.getSmartScreenDigital(sid).execute(); if(resultResponse.body()==null){ log.error("推送登记接种留观服务人数到服务{} 返回消息体为空",serverUrlProperties.getYbzlUrl()); }else if (BasicEcode.SUCCESS.equals(resultResponse.body().getEcode())) { log.info("推送登记接种留观服务人数到服务{}成功",serverUrlProperties.getYbzlUrl()); }else { log.error("推送登记接种留观服务人数到服务{}异常 错误码{}", serverUrlProperties.getYbzlUrl(),resultResponse.body().getEcode()); } } catch (Exception e) { log.error("推送登记接种留观服务人数到服务{}异常{}", serverUrlProperties.getYbzlUrl(),e); } log.info("推送登记接种留观服务人数 ====== end"); return ybzlSmartScreenDigitalDTO; }