ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package jnpf.base.util.interfaceUtil;
 
import cn.hutool.core.collection.CollectionUtil;
import jnpf.util.DateUtil;
import jnpf.util.ServletUtil;
import jnpf.util.StringUtil;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.binary.Hex;
 
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.*;
 
/**
 * 接口工具类
 *
 * @author JNPF开发平台组
 * @version V3.4.2
 * @copyright 引迈信息技术有限公司
 * @date 2022/6/13 10:38
 */
public class InterfaceUtil {
    public static final String ALGORITH_FORMAC = "HmacSHA256";
    public static final String HOST = "Host";
    public static final String YMDATE = "YmDate";
    public static final String CONTENT_TYPE = " Content-Type";
    public static final String CHARSET_NAME = "utf-8";
    public static final String USERKEY = "UserKey";
 
    /**
     * 验证签名
     * @param
     * @return
     * @copyright 引迈信息技术有限公司
     * @date 2022/6/14
     */
    public static boolean verifySignature(String secret, String author) throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException {
        String method = ServletUtil.getRequest().getMethod();
        String url = "/api/system"+ServletUtil.getRequest().getRequestURI();
        String ymdate = ServletUtil.getRequest().getHeader(YMDATE);
        String host = ServletUtil.getRequestHost();
        String source = new StringBuilder()
                .append(method).append('\n')
                .append(url).append('\n')
                .append(ymdate).append('\n')
                .append(host).append('\n').toString();
        Mac mac = Mac.getInstance(ALGORITH_FORMAC);
        SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decodeBase64(secret), ALGORITH_FORMAC);
        mac.init(secretKeySpec);
        String signature = Hex.encodeHexString(mac.doFinal(source.getBytes(CHARSET_NAME)));
        if (author.equals(signature)) {
            return true;
        }
        return false;
    }
    /**
     * map转 name=value&name=value格式
     * @param
     * @return
     * @copyright 引迈信息技术有限公司
     * @date 2022/6/14
     */
    public static String createLinkStringByGet(Map<String, String> params) throws UnsupportedEncodingException {
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        String prestr = "";
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            value = URLEncoder.encode(value, "UTF-8");
            if (i == keys.size() - 1) {//拼接时,不包括最后一个&字符
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + key + "=" + value + "&";
            }
        }
        return prestr;
    }
 
    /**
     * 判断map内有没有指定key的值
     * @param
     * @return
     * @copyright 引迈信息技术有限公司
     * @date 2022/6/14
     */
    public static boolean checkParam(Map<String,String> map,String str){
        if(CollectionUtil.isEmpty(map)){
            return false;
        }
        if(StringUtil.isEmpty(str)){
            return false;
        }
        if(map.get(str)!=null&& StringUtil.isNotEmpty(map.get(str))){
            return true;
        }
        return false;
    }
 
//
    public static Map<String, String>   getAuthorization(String intefaceId, String appId, String appSecret, Map<String, String> map){
        Map<String, String> resultMap=new HashMap<>();
        try {
            String method = ServletUtil.getRequest().getMethod();
            String url ="/api/system/DataInterface/"+intefaceId+"/Actions/Response";
            String ymdate = ""+DateUtil.getNowDate().getTime();
            String host = ServletUtil.getRequestHost();
            String source = new StringBuilder()
                    .append(method).append('\n')
                    .append(url).append('\n')
                    .append(ymdate).append('\n')
                    .append(host).append('\n').toString();
            Mac mac = Mac.getInstance(ALGORITH_FORMAC);
            SecretKeySpec secretKeySpec = new SecretKeySpec(Base64.decodeBase64(appSecret), ALGORITH_FORMAC);
            mac.init(secretKeySpec);
            String signature = Hex.encodeHexString(mac.doFinal(source.getBytes(CHARSET_NAME)));
            resultMap.put("YmDate",ymdate);
            resultMap.put("Authorization",appId+"::"+signature);
            return resultMap;
        }catch (Exception e){}
        return resultMap;
    }
}