123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- 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<String, String> getParam) throws IOException {
- logger.error("====获取签名===进入到了com.kingdee.eas.custom.jiuzhoutong.utils.SYUtils.getSignature=======");
- TreeMap<String, String> 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<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(TreeMap<String, String> 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<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;
- }
- /**
- *
- * @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);
- }
- }
- }
|