SYUtils.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. package com.kingdee.eas.custom.jiuzhoutong.utils;
  2. import com.kingdee.bos.BOSException;
  3. import com.kingdee.bos.Context;
  4. import com.kingdee.eas.base.permission.UserInfo;
  5. import com.kingdee.eas.common.EASBizException;
  6. import com.kingdee.eas.custom.shuiyou.interfaceiog.LogInfoFactory;
  7. import com.kingdee.eas.custom.shuiyou.interfaceiog.LogInfoInfo;
  8. import org.apache.log4j.Logger;
  9. import javax.crypto.Mac;
  10. import javax.crypto.spec.SecretKeySpec;
  11. import java.io.FileInputStream;
  12. import java.io.FileNotFoundException;
  13. import java.io.IOException;
  14. import java.io.UnsupportedEncodingException;
  15. import java.net.URLEncoder;
  16. import java.security.InvalidKeyException;
  17. import java.security.NoSuchAlgorithmException;
  18. import java.security.SignatureException;
  19. import java.sql.Timestamp;
  20. import java.util.*;
  21. public class SYUtils {
  22. private Properties propt = new Properties();//共用参数
  23. private static Logger logger = Logger.getLogger("com.kingdee.eas.custom.jiuzhoutong.utils.SYUtils");
  24. public SYUtils(Properties propt){
  25. this.propt=propt;
  26. }
  27. public SYUtils(String address) throws IOException {
  28. this.propt.load(new FileInputStream(address));
  29. }
  30. public SYUtils() throws IOException {
  31. this.propt.load(new FileInputStream(System.getProperty("EAS_HOME") + "/server/properties/sy/syConfig.properties"));
  32. }
  33. /**
  34. * 获取签名
  35. * @return
  36. * @throws IOException
  37. */
  38. public String getSignature(String xReqNonce,String timestamp,TreeMap<String, String> getParam) throws IOException {
  39. logger.error("====获取签名===进入到了com.kingdee.eas.custom.jiuzhoutong.utils.SYUtils.getSignature=======");
  40. TreeMap<String, String> treeMap = new TreeMap<>();
  41. String version = propt.getProperty("version");//版本
  42. String appKey = propt.getProperty("appKey");//身份标识
  43. String appSecret = propt.getProperty("appSecret");//秘钥
  44. logger.error("=============参数列================");
  45. logger.error("version:"+version);
  46. logger.error("appKey:"+appKey);
  47. logger.error("xReqNonce:"+xReqNonce);
  48. logger.error("appSecret:"+appSecret);
  49. logger.error("==================================");
  50. treeMap.put("version", version);
  51. treeMap.put("timestamp", String.valueOf(timestamp));
  52. treeMap.put("appKey", appKey);
  53. treeMap.put("xReqNonce", xReqNonce);
  54. treeMap.put("appSecret", appSecret);
  55. if (getParam!=null){
  56. treeMap.putAll(getParam);
  57. }
  58. StringBuilder mergeStr = new StringBuilder();
  59. for (Map.Entry<String, String> stringStringEntry : treeMap.entrySet()) {
  60. mergeStr.append(stringStringEntry.getValue());
  61. }
  62. logger.error("将以上key=value对的value进行合并,生成一下字符串mergeStr:"+mergeStr.toString());
  63. String encodedStr = null;
  64. try {
  65. encodedStr = URLEncoder.encode(mergeStr.toString(), "UTF-8");
  66. } catch (UnsupportedEncodingException e) {
  67. // URL 编码失败
  68. e.printStackTrace();
  69. throw new RuntimeException("url编码失败");
  70. }
  71. logger.error("将生成的mergeStr进行Url编码:"+encodedStr);
  72. // 4.利用HmacSHA256算法对signStr进行哈希运算生成消息摘要,摘要结果以Base64结果形式返回,signStr即为请求参数中的signature字段
  73. String signatureResult = "";
  74. try {
  75. Mac mac = Mac.getInstance("HmacSHA256");
  76. SecretKeySpec signingKey = new SecretKeySpec(appSecret.getBytes(), "HmacSHA256");
  77. mac.init(signingKey);
  78. byte[] signData = mac.doFinal(encodedStr.getBytes());
  79. byte[] resultBytes = Base64.getEncoder().encode(signData);
  80. signatureResult = new String(resultBytes, "UTF-8");
  81. } catch (NoSuchAlgorithmException e) {
  82. throw new RuntimeException("平台不支持 HmacSHA 摘要方式");
  83. } catch (InvalidKeyException e) {
  84. throw new RuntimeException("Speicified access secret is not valid.");
  85. } catch (UnsupportedEncodingException e) {
  86. throw new RuntimeException("转码失败");
  87. }
  88. logger.error("用HmacSHA256算法对signStr进行哈希运算生成消息摘要,摘要结果以Base64结果形式返回,signStr即为请求参数中的signature字段"+signatureResult);
  89. logger.error("==========================退出================================");
  90. return signatureResult;
  91. }
  92. public Map<String,String> getCommonParameter(TreeMap<String, String> getParam) throws IOException {
  93. String appKey = propt.getProperty("appKey");
  94. String timestamp = String.valueOf(new Date().getTime());
  95. String version = propt.getProperty("version");
  96. String xReqNonce = UUID.randomUUID().toString().replace("-", "");//调用者生成的 UUID(32位),结合时间戳timestamp 防重放
  97. String signature = this.getSignature(xReqNonce,timestamp,getParam);
  98. Map<String,String> parameters = new HashMap<String,String>();
  99. parameters.put("appKey",appKey);
  100. parameters.put("timestamp",timestamp);
  101. parameters.put("version",version);
  102. parameters.put("xReqNonce",xReqNonce);
  103. parameters.put("signature",signature);
  104. return parameters;
  105. }
  106. /**
  107. *
  108. * @param context 上下文
  109. * @param error 错误信息
  110. * @param interfaceAddress 接口地址
  111. * @param interfaceName 接口名
  112. * @param inParameter 入参
  113. * @param outParameter 回参
  114. * @param entrance 入口
  115. * @param xReqNonce uuid
  116. * @throws BOSException
  117. * @throws EASBizException
  118. */
  119. public void interfaceLog(Context context,String error,String interfaceAddress
  120. ,String interfaceName,String inParameter,String outParameter
  121. ,String entrance,String xReqNonce) {
  122. LogInfoInfo logInfo = new LogInfoInfo();
  123. logInfo.setErrorInfo(error);//错误信息
  124. logInfo.setInterfaceAddress(interfaceAddress);//接口地址
  125. logInfo.setInterfaceName(interfaceName);//接口名
  126. logInfo.setInParameter(inParameter);//入参
  127. logInfo.setOutParameter(outParameter);//回参
  128. logInfo.setEntrance(entrance);//入口
  129. UserInfo userInfo = (UserInfo)context.get("UserInfo");
  130. logInfo.setCreator(userInfo);
  131. Date date = new Date();
  132. Timestamp timestamp = new Timestamp(date.getTime());
  133. logInfo.setCreateTime(timestamp);
  134. logInfo.setLastUpdateTime(timestamp);
  135. logInfo.setLastUpdateUser(userInfo);
  136. logInfo.setBizDate(date);
  137. logInfo.setXReqNonce(xReqNonce);
  138. try {
  139. LogInfoFactory.getLocalInstance(context).save(logInfo);
  140. } catch (BOSException e) {
  141. throw new RuntimeException(e);
  142. } catch (EASBizException e) {
  143. throw new RuntimeException(e);
  144. }
  145. }
  146. }