| | |
| | | package com.stylefeng.guns.modular.system.util.zhenglian; |
| | | |
| | | import cn.hutool.http.HttpRequest; |
| | | import cn.hutool.http.HttpUtil; |
| | | import com.alibaba.fastjson.JSON; |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.stylefeng.guns.modular.system.util.SpringContextsUtil; |
| | | import com.stylefeng.guns.modular.system.util.UUIDUtil; |
| | | import com.stylefeng.guns.modular.system.util.zhenglian.model.MessageBody; |
| | | import com.stylefeng.guns.modular.system.util.zhenglian.model.TokenRequest; |
| | | import com.zlpay.assist.encrypt.sm4.SM4Util; |
| | | import com.zlpay.assist.sign.sm2.SM2Util; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | |
| | | import java.time.LocalDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * @author zhibing.pu |
| | | * @Date 2025/7/24 14:06 |
| | | */ |
| | | @Slf4j |
| | | public class TokenUtil { |
| | | |
| | | |
| | | private static ZhengLianConfig zhengLianConfig = SpringContextsUtil.getBean(ZhengLianConfig.class).getZhengLianConfig(); |
| | | |
| | | |
| | | |
| | | public static void getToken(TokenRequest request){ |
| | | request.setAppid(zhengLianConfig.getAppid()); |
| | | HttpRequest post = HttpUtil.createPost(zhengLianConfig.getUrl()); |
| | | String timestamp = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss")); |
| | | post.header("msgId", UUIDUtil.getRandomCode()); |
| | | post.header("merchNo", zhengLianConfig.getMerchNo()); |
| | | post.header("txCode","ZLPAY.ACC.T0001"); |
| | | post.header("version","1.0.1"); |
| | | post.header("encrp","1"); |
| | | post.header("signa","1"); |
| | | post.header("encrpNo","123456"); |
| | | post.header("signNo","123456"); |
| | | post.header("timestamp", timestamp); |
| | | post.header("Content-Length", ""); |
| | | post.header("Content-Type", "application/json;charset=utf-8"); |
| | | |
| | | public static String getToken(TokenRequest tokenRequest) throws Exception { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | Map<String,String> headerMap = new HashMap<String,String>(); |
| | | MessageBody body = new MessageBody(); |
| | | headerMap.put("msgId", UUIDUtil.getRandomCode()); |
| | | headerMap.put("merchNo", "B00000871"); |
| | | headerMap.put("txCode", "ZLPAY.ACC.T0001"); |
| | | headerMap.put("version", "1.0.1"); |
| | | headerMap.put("signa", "1"); |
| | | headerMap.put("signNo", zhengLianConfig.getSignNo()); |
| | | headerMap.put("encrp", "1"); |
| | | headerMap.put("encrpNo", zhengLianConfig.getEncrpNo()); |
| | | headerMap.put("timestamp", sdf.format(new Date())); |
| | | |
| | | |
| | | tokenRequest.setAppId(zhengLianConfig.getAppid()); |
| | | tokenRequest.setType("04"); |
| | | String reqBO = JSON.toJSONString(tokenRequest); |
| | | // 生成对称加密秘钥 |
| | | String key = ZhengLianUtil.generateKey(16); |
| | | // 加密数据 |
| | | String jsonData = SM4Util.sm4EcbEncrypt(key, reqBO, "NoPadding"); |
| | | //加密对称加密的秘钥 |
| | | // 获取公钥 |
| | | String publicKey = ZhengLianUtil.getPublicKey(); |
| | | String secrtKey = SM2Util.encrypt(publicKey, key); |
| | | |
| | | post.body("{}"); |
| | | // 将密文放入body |
| | | body.setData(jsonData); |
| | | body.setSign(ZhengLianUtil.sign(jsonData)); |
| | | body.setSecret(secrtKey); |
| | | String result = ZLHttpClientUtil.doPost(zhengLianConfig.getUrl(), headerMap, JSON.toJSONString(body)); |
| | | |
| | | |
| | | System.out.println("应答内容:"+ result); |
| | | MessageBody respBody = JSON.parseObject(result,MessageBody.class); |
| | | // 验签 |
| | | boolean checkResult = SM2Util.verify(publicKey, zhengLianConfig.getEncrpNo(), respBody.getSign(), respBody.getData()); |
| | | System.out.println("验签结果:" + checkResult); |
| | | // 获取私钥 |
| | | String privateKey = ZhengLianUtil.getPrivateKey(); |
| | | // 解密对称秘钥 |
| | | String k = SM2Util.decrypt(privateKey, respBody.getSecret()); |
| | | System.out.println("对称秘钥:" + k); |
| | | // 解密业务报文 |
| | | String backData = SM4Util.sm4EcbDecrypt(k, respBody.getData()); |
| | | System.out.println("返回业务报文:" + backData); |
| | | JSONObject jsonObject = JSON.parseObject(backData); |
| | | String sysRtnCode = jsonObject.getString("sysRtnCode"); |
| | | if(!"000000".equals(sysRtnCode)){ |
| | | log.error("获取token失败!{}", jsonObject.getString("sysRtnMsg")); |
| | | throw new Exception(jsonObject.getString("sysRtnMsg")); |
| | | } |
| | | JSONObject bizData = jsonObject.getJSONObject("bizData"); |
| | | String resCode = bizData.getString("resCode"); |
| | | if(!"S010000".equals(resCode)){ |
| | | log.error("获取token失败!{}", bizData.getString("resMsg")); |
| | | throw new Exception(jsonObject.getString("resMsg")); |
| | | } |
| | | return bizData.getString("token"); |
| | | } |
| | | |
| | | } |