|
@@ -0,0 +1,105 @@
|
|
|
+package com.kingdee.eas.custom.jiuzhoutong.utils;
|
|
|
+
|
|
|
+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.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() throws IOException {
|
|
|
+ logger.error("====获取签名===进入到了com.kingdee.eas.custom.jiuzhoutong.utils.SYUtils.getSignature=======");
|
|
|
+ TreeMap<String, String> treeMap = new TreeMap<>();
|
|
|
+ String version = propt.getProperty("version");//版本
|
|
|
+ Long timestamp = new Date().getTime();//时间差
|
|
|
+ String appKey = propt.getProperty("appKey");//身份标识
|
|
|
+ String xReqNonce = UUID.randomUUID().toString().replace("-", "");//调用者生成的 UUID(32位),结合时间戳timestamp 防重放
|
|
|
+ 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);
|
|
|
+ StringBuilder mergeStr = new StringBuilder();
|
|
|
+ for (Map.Entry<String, String> 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<String,String> getCommonParameter() 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();
|
|
|
+ Map<String,String> parameters = new HashMap<String,String>();
|
|
|
+ parameters.put("appKey",appKey);
|
|
|
+ parameters.put("timestamp",timestamp);
|
|
|
+ parameters.put("version",version);
|
|
|
+ parameters.put("xReqNonce",xReqNonce);
|
|
|
+ parameters.put("signature",signature);
|
|
|
+ return parameters;
|
|
|
+ }
|
|
|
+}
|