package com.kingdee.eas.custom.jiuzhoutong.utils; import com.kingdee.bos.BOSException; import com.kingdee.bos.Context; import com.kingdee.eas.base.permission.UserInfo; import com.kingdee.eas.common.EASBizException; import com.kingdee.eas.custom.shuiyou.interfaceiog.LogInfoFactory; import com.kingdee.eas.custom.shuiyou.interfaceiog.LogInfoInfo; import org.apache.log4j.Logger; import javax.crypto.Mac; import javax.crypto.spec.SecretKeySpec; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; import java.security.InvalidKeyException; import java.security.NoSuchAlgorithmException; import java.security.SignatureException; import java.sql.Timestamp; import java.util.*; public class SYUtils { private Properties propt = new Properties();//共用参数 private static Logger logger = Logger.getLogger("com.kingdee.eas.custom.jiuzhoutong.utils.SYUtils"); public SYUtils(Properties propt){ this.propt=propt; } public SYUtils(String address) throws IOException { this.propt.load(new FileInputStream(address)); } public SYUtils() throws IOException { this.propt.load(new FileInputStream(System.getProperty("EAS_HOME") + "/server/properties/sy/syConfig.properties")); } /** * 获取签名 * @return * @throws IOException */ public String getSignature(String xReqNonce,String timestamp,TreeMap getParam) throws IOException { logger.error("====获取签名===进入到了com.kingdee.eas.custom.jiuzhoutong.utils.SYUtils.getSignature======="); TreeMap treeMap = new TreeMap<>(); String version = propt.getProperty("version");//版本 String appKey = propt.getProperty("appKey");//身份标识 String appSecret = propt.getProperty("appSecret");//秘钥 logger.error("=============参数列================"); logger.error("version:"+version); logger.error("appKey:"+appKey); logger.error("xReqNonce:"+xReqNonce); logger.error("appSecret:"+appSecret); logger.error("=================================="); treeMap.put("version", version); treeMap.put("timestamp", String.valueOf(timestamp)); treeMap.put("appKey", appKey); treeMap.put("xReqNonce", xReqNonce); treeMap.put("appSecret", appSecret); if (getParam!=null){ treeMap.putAll(getParam); } StringBuilder mergeStr = new StringBuilder(); for (Map.Entry stringStringEntry : treeMap.entrySet()) { mergeStr.append(stringStringEntry.getValue()); } logger.error("将以上key=value对的value进行合并,生成一下字符串mergeStr:"+mergeStr.toString()); String encodedStr = null; try { encodedStr = URLEncoder.encode(mergeStr.toString(), "UTF-8"); } catch (UnsupportedEncodingException e) { // URL 编码失败 e.printStackTrace(); throw new RuntimeException("url编码失败"); } logger.error("将生成的mergeStr进行Url编码:"+encodedStr); // 4.利用HmacSHA256算法对signStr进行哈希运算生成消息摘要,摘要结果以Base64结果形式返回,signStr即为请求参数中的signature字段 String signatureResult = ""; try { Mac mac = Mac.getInstance("HmacSHA256"); SecretKeySpec signingKey = new SecretKeySpec(appSecret.getBytes(), "HmacSHA256"); mac.init(signingKey); byte[] signData = mac.doFinal(encodedStr.getBytes()); byte[] resultBytes = Base64.getEncoder().encode(signData); signatureResult = new String(resultBytes, "UTF-8"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("平台不支持 HmacSHA 摘要方式"); } catch (InvalidKeyException e) { throw new RuntimeException("Speicified access secret is not valid."); } catch (UnsupportedEncodingException e) { throw new RuntimeException("转码失败"); } logger.error("用HmacSHA256算法对signStr进行哈希运算生成消息摘要,摘要结果以Base64结果形式返回,signStr即为请求参数中的signature字段"+signatureResult); logger.error("==========================退出================================"); return signatureResult; } public Map getCommonParameter(TreeMap getParam) throws IOException { String appKey = propt.getProperty("appKey"); String timestamp = String.valueOf(new Date().getTime()); String version = propt.getProperty("version"); String xReqNonce = UUID.randomUUID().toString().replace("-", "");//调用者生成的 UUID(32位),结合时间戳timestamp 防重放 String signature = this.getSignature(xReqNonce,timestamp,getParam); Map parameters = new HashMap(); parameters.put("appKey",appKey); parameters.put("timestamp",timestamp); parameters.put("version",version); parameters.put("xReqNonce",xReqNonce); parameters.put("signature",signature); return parameters; } /** * * @param context 上下文 * @param error 错误信息 * @param interfaceAddress 接口地址 * @param interfaceName 接口名 * @param inParameter 入参 * @param outParameter 回参 * @param entrance 入口 * @param xReqNonce uuid * @throws BOSException * @throws EASBizException */ public void interfaceLog(Context context,String error,String interfaceAddress ,String interfaceName,String inParameter,String outParameter ,String entrance,String xReqNonce) { LogInfoInfo logInfo = new LogInfoInfo(); logInfo.setErrorInfo(error);//错误信息 logInfo.setInterfaceAddress(interfaceAddress);//接口地址 logInfo.setInterfaceName(interfaceName);//接口名 logInfo.setInParameter(inParameter);//入参 logInfo.setOutParameter(outParameter);//回参 logInfo.setEntrance(entrance);//入口 UserInfo userInfo = (UserInfo)context.get("UserInfo"); logInfo.setCreator(userInfo); Date date = new Date(); Timestamp timestamp = new Timestamp(date.getTime()); logInfo.setCreateTime(timestamp); logInfo.setLastUpdateTime(timestamp); logInfo.setLastUpdateUser(userInfo); logInfo.setBizDate(date); logInfo.setXReqNonce(xReqNonce); try { LogInfoFactory.getLocalInstance(context).save(logInfo); } catch (BOSException e) { throw new RuntimeException(e); } catch (EASBizException e) { throw new RuntimeException(e); } } }