wjc
2023-07-07 22494af577e21a930abef309f2f60c03c9615bd1
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package com.hdl.linkpm.sdk.core.interceptor;
 
import android.net.Uri;
import android.text.TextUtils;
 
import androidx.annotation.NonNull;
 
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.hdl.hdlhttp.utils.GsonConvert;
import com.hdl.linkpm.sdk.HDLLinkPMSdk;
import com.hdl.linkpm.sdk.core.interceptor.HDLSmartHeader;
import com.hdl.linkpm.sdk.utils.HDLMD5Utils;
import com.hdl.linkpm.sdk.utils.HDLSDKLog;
 
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
import okhttp3.FormBody;
import okhttp3.Headers;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okio.Buffer;
 
/**
 * Created by Tong on 2021/11/4.
 * 加密并自动补充参数,appKey、timestamp
 * 只支持表单、json
 * 最终json方式提交
 */
public class HDLEncryptInterceptor implements Interceptor {
 
 
    @NonNull
    @Override
    public Response intercept(@NonNull Chain chain) throws IOException {
        Request request = chain.request();
        Headers headers = request.headers();
        if (!isIgnoreSignHeader(headers)) {
            return chain.proceed(encrypt(request));
        }
 
        return chain.proceed(request);
    }
 
 
    /**
     * 添加sign字段
     */
    private Request encrypt(Request request) {
        final String timestamp = String.valueOf(System.currentTimeMillis());
        final String appKey = HDLLinkPMSdk.getAppKey();
        final String appSecret = HDLLinkPMSdk.getAppSecret();
        final JsonObject json = getBodyJson(request);
        if (json != null) {
            json.addProperty("appKey", appKey);
            json.addProperty("timestamp", timestamp);
            json.addProperty("sign", getSign(json, appSecret));
            final RequestBody requestBody = RequestBody.create(json.toString(), MediaType.parse("application/json;charset=UTF-8"));
            return request.newBuilder().post(requestBody)
                    .build();
        }
        return request;
    }
 
 
    /**
     * 是否忽略自定义的加密头
     */
    private boolean isIgnoreSignHeader(Headers headers) {
        String signHeader = headers.get(HDLSmartHeader.IGNORE_SIGN_HEADER);
 
        return !TextUtils.isEmpty(signHeader);
    }
 
    private JsonObject getBodyJson(Request request) {
 
        RequestBody body = request.body();
        if (body instanceof FormBody) {
            JsonObject object = new JsonObject();
            FormBody formBody = (FormBody) body;
            for (int i = 0; i < formBody.size(); i++) {
                object.addProperty(formBody.encodedName(i), formBody.value(i));
            }
            return object;
        } else {
            //json格式
            try {
                String bodyString = getBodyString(request);
                if (!TextUtils.isEmpty(bodyString)) {
                    final JsonElement parseString = JsonParser.parseString(bodyString);
                    return parseString.getAsJsonObject();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
 
        }
        return null;
    }
 
    private String getBodyString(Request request) {
        try {
            RequestBody body = request.body();
            if (body != null) {
                Buffer buffer = new Buffer();
                body.writeTo(buffer);
                Charset charset = Charset.forName("UTF-8");
                MediaType contentType = body.contentType();
                charset = contentType.charset(charset);
                return buffer.readString(charset);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
 
    /**
     * 需要按字母排序
     *
     * @param json 所有字段使用urlParameter拼接,除了appSecret
     */
    private String getSign(JsonObject json, String appSecret) {
        String builder = jsonToUrlParameter(json) +
                appSecret;
        return HDLMD5Utils.encodeMD5(builder);
    }
 
 
    private String jsonToUrlParameter(JsonObject object) {
        final Map<String, String> map = GsonConvert.getGson().fromJson(object, new TypeToken<Map<String, String>>() {
        }.getType());
        final Uri.Builder builder = new Uri.Builder();
        List<String> list = new ArrayList<>(map.keySet());
        Collections.sort(list);
        for (String key : list) {
            //判断当前值是否需要参与签名,保持跟云端一致
            if (IfValueNeedSign(map.get(key))) {
                builder.appendQueryParameter(key, map.get(key));
//                HDLSDKLog.e("要签名:" + key + " :" + map.get(key));
            } else {
//                HDLSDKLog.e("不需要签名:" + key + " :" + map.get(key));
            }
        }
        return builder.build().getQuery();
    }
 
    /**
     * 判断当前值是否需要参与签名,保持跟云端一致
     * 空字符串不参与
     * 数组,集合,对象不参与
     *
     * @param valueStr
     * @return
     */
    private static boolean IfValueNeedSign(String valueStr) {
        if (TextUtils.isEmpty(valueStr))
            return false;
        final char[] strChar = valueStr.substring(0, 1).toCharArray();
        final char firstChar = strChar[0];
        //System.out.println("getJSONType firstChar = "+firstChar);
        if (firstChar != '{' && firstChar != '[')
            return true;
 
        return false;
    }
//    /**
//     * 判断当前值是否需要参与签名,保持跟云端一致
//     * 空字符串不参与
//     * 数组,集合,对象不参与
//     *
//     * @param inputValueStr
//     * @return
//     */
//    private static boolean IfValueNeedSign(String inputValueStr) {
//        if (TextUtils.isEmpty(inputValueStr))
//            return false;
//        try {
//            //先解码
//            String valueStr = URLDecoder.decode(inputValueStr, "utf-8");
//            final char[] strChar = valueStr.substring(0, 1).toCharArray();
//            final char firstChar = strChar[0];
//            //System.out.println("getJSONType firstChar = "+firstChar);
//            if (firstChar != '{' && firstChar != '[') {
//                return true;
//            }
//            return false;
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//            return false;
//        }
//
//    }
 
}