H5JsApiPermissionUtil.java
8.95 KB
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
* Copyright (c) Wondertek Technologies Co., Ltd. 2019-2022. All rights reserved.
*/
package com.wd.common.utils;
import android.text.TextUtils;
import com.wd.foundation.wdkit.file.MyFileUtils;
import com.wd.foundation.wdkitcore.tools.AppContext;
import com.wd.foundation.wdkitcore.tools.ArrayUtils;
import com.wd.foundation.wdkitcore.tools.JsonUtils;
import com.wd.foundation.wdkitcore.tools.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
/**
* 描述:设置白名单
*
* @author : lvjinhui
* @since: 2022/5/26
*/
public class H5JsApiPermissionUtil {
/**
* 典型实现 HashSet()是一个无序,不可重复的集合
* */
private static final Set<String> WHITE_URL_LIST_FOR_JS = new HashSet<>();
private static final Set<String> WHITE_URL_LIST_FOR_OPEN = new HashSet<>();
private final String H5_WHITE_HOST_FILE_NAME = "h5_white_host.txt";
public static H5JsApiPermissionUtil instance;
/**
* false--不校验白名单;ture校验;
* 默认不用校验
*/
public static boolean enabled;
public static H5JsApiPermissionUtil getInstance() {
if (instance == null) {
synchronized (H5JsApiPermissionUtil.class) {
if (instance == null) {
instance = new H5JsApiPermissionUtil();
}
}
}
return instance;
}
/**
* 初始化添加默认白名单
*/
public void init(){
addDefaultWhiteList();
addDefaultWhiteListForOpen();
}
/**
* 默认白名单,1.js交互的
*/
private static void addDefaultWhiteList() {
if (WHITE_URL_LIST_FOR_JS != null) {
WHITE_URL_LIST_FOR_JS.add("cdnpeoplefrontdev.aikan.pdnews.cn");
WHITE_URL_LIST_FOR_JS.add("cdnpeoplefrontsit.aikan.pdnews.cn");
WHITE_URL_LIST_FOR_JS.add("cdnpeoplefrontuat.aikan.pdnews.cn");
WHITE_URL_LIST_FOR_JS.add("cdnpeoplefront.aikan.pdnews.cn");
WHITE_URL_LIST_FOR_JS.add("pd-people-uat.pdnews.cn");
WHITE_URL_LIST_FOR_JS.add("pd-people-sit.pdnews.cn");
WHITE_URL_LIST_FOR_JS.add("pd-people-dev.pdnews.cn");
WHITE_URL_LIST_FOR_JS.add("www.peopleapp.com");
WHITE_URL_LIST_FOR_JS.add("h5.peopleapp.com");
}
}
/**
* 默认白名单=.可以打开
*/
private static void addDefaultWhiteListForOpen() {
if (WHITE_URL_LIST_FOR_OPEN != null) {
WHITE_URL_LIST_FOR_OPEN.add("cdnpeoplefrontdev.aikan.pdnews.cn");
WHITE_URL_LIST_FOR_OPEN.add("cdnpeoplefrontsit.aikan.pdnews.cn");
WHITE_URL_LIST_FOR_OPEN.add("cdnpeoplefrontuat.aikan.pdnews.cn");
WHITE_URL_LIST_FOR_OPEN.add("cdnpeoplefront.aikan.pdnews.cn");
WHITE_URL_LIST_FOR_OPEN.add("pd-people-uat.pdnews.cn");
WHITE_URL_LIST_FOR_OPEN.add("pd-people-sit.pdnews.cn");
WHITE_URL_LIST_FOR_OPEN.add("pd-people-dev.pdnews.cn");
WHITE_URL_LIST_FOR_OPEN.add("www.peopleapp.com");
WHITE_URL_LIST_FOR_OPEN.add("h5.peopleapp.com");
}
}
/**
* 检测是否在白名单内
* @param inputUrl
* @return true不拦截
*/
public boolean isAppWhiteHostForJs(String inputUrl) {
if (!enabled){
//不校验
return true;
}
if (TextUtils.isEmpty(inputUrl)) {
return false;
}
if (ArrayUtils.isEmpty(WHITE_URL_LIST_FOR_JS)) {
return true;
}
for (String whiteUrl:WHITE_URL_LIST_FOR_JS) {
if (inputUrl.contains(whiteUrl)){
return true;
}
}
return false;
}
/**
* 添加新的白名单
*
* @param url
*/
public void addWhiteListUrlForJs(List<String> url) {
if (WHITE_URL_LIST_FOR_JS != null) {
WHITE_URL_LIST_FOR_JS.addAll(url);
}
}
/**
* 添加新的白名单
*
* @param url
*/
public void addWhiteListUrlForOpen(List<String> url) {
if (WHITE_URL_LIST_FOR_OPEN != null) {
WHITE_URL_LIST_FOR_OPEN.addAll(url);
}
}
/**
* 检测是否在白名单内,是否运行打开
*
* @param inputUrl
* @return true不拦截
*/
public boolean isAppWhiteHostForOpen(String inputUrl) {
if (!enabled){
//不校验
return true;
}
if (TextUtils.isEmpty(inputUrl)) {
return false;
}
if (ArrayUtils.isEmpty(WHITE_URL_LIST_FOR_OPEN)) {
return true;
}
for (String whiteUrl:WHITE_URL_LIST_FOR_OPEN) {
if (inputUrl.contains(whiteUrl)){
return true;
}
}
return false;
}
/**
* 保存H5白名单文件,enqueue异步,execute同步
* @param h5FileStr
*/
public void saveH5WhiteHostFile(String h5FileStr){
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(h5FileStr).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
InputStream is = null;
byte[] buf = new byte[2048];
int len = 0;
FileOutputStream fos = null;
try {
is = response.body().byteStream();
String fileName = getH5WhiteHostCachePath(H5_WHITE_HOST_FILE_NAME);
String tempFileName = "";
if(fileName.contains(".")){
tempFileName = fileName.substring(0,fileName.lastIndexOf("."))+".temp";
}
File file = new File(TextUtils.isEmpty(tempFileName) ? fileName : tempFileName);
fos = new FileOutputStream(file);
while ((len = is.read(buf)) != -1) {
fos.write(buf, 0, len);
}
fos.flush();
if(!TextUtils.isEmpty(tempFileName)){
file.renameTo(new File(fileName));
}
//然后读取文件内容添加到白名单
addH5WhiteHostList();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
}
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
}
}
}
});
}
/**
* 文件是否不存在
* @return true 不存在, false存在
*/
public boolean isH5WhiteHostFileNoExists(){
try {
File tempFile = getH5WhiteHostFile();
return !tempFile.exists();
}catch (Exception e){
e.printStackTrace();
}
return true;
}
private File getH5WhiteHostFile(){
String appCachePath = AppContext.getContext().getCacheDir().getAbsolutePath();
File tempFile = new File(appCachePath, H5_WHITE_HOST_FILE_NAME);
return tempFile;
}
public static String getH5WhiteHostCachePath(String fileName) {
return new File(AppContext.getContext().getCacheDir(), fileName).getAbsolutePath();
}
/**
* 读取白名单文件
* @return
*/
public void addH5WhiteHostList(){
List<String> hostList = new ArrayList<>();
File h5WhiteHostFile = getH5WhiteHostFile();
if (h5WhiteHostFile != null && h5WhiteHostFile.exists()){
String readJsonData = MyFileUtils.readJsonFile(h5WhiteHostFile);
if (StringUtils.isNotBlank(readJsonData)){
try {
JSONObject jsonObject = new JSONObject(readJsonData);
if (jsonObject != null) {
JSONArray jsonArray = jsonObject.optJSONArray("whiteurls");
hostList = JsonUtils.convertJsonArrayToObjList(jsonArray.toString(), String.class);
addWhiteListUrlForOpen(hostList);
addWhiteListUrlForJs(hostList);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}