1
13693261870
2022-09-16 fee60c3e25fac0982f3b8cb8feea7225c4ed22f8
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package com.terra.proxy.controller;
 
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.terra.proxy.bean.CustomerToken;
import com.terra.proxy.bean.ServerRgBean;
import com.terra.proxy.bean.TokenRecord;
import com.terra.proxy.mapper.LogMapper;
import com.terra.proxy.properties.TerraProperties;
import com.terra.proxy.service.Impl.ServerRegisterServiceImpl;
import com.terra.proxy.util.JedisUtils;
import com.terra.proxy.util.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import redis.clients.jedis.Jedis;
import springfox.documentation.annotations.ApiIgnore;
 
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Controller
@RequestMapping("/serverRegister")
@CrossOrigin
public class ServiceRegisterController {
 
    @Autowired
    JedisUtils jedisUtils;
 
    @Autowired
    JwtUtils JwtUtils;
 
    @Autowired
    ServerRegisterServiceImpl serverRegisterService;
 
    @Autowired
    LogMapper logMapper;
 
    @Value("${sys.allowedips}")
    private String allowedips;
    @Value("${sys.jwt.expire}")
    private Long expireSeconds;
 
    @Value("${sys.visitname}")
    private String visitname;
    @Value("${sys.visitpwd}")
    private String visitpwd;
 
    private final static String[] hexDigits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
 
    private String salt = "abcde";
 
    private String pass = "HYFW";
    @Autowired
    private TerraProperties properties;
 
    /**
     * Arcgis Server 服务代理注册
     * @param request
     * @param zyid
     * @param serverUrl
     * @param contextPath
     * @param urlid
     * @param isPublic
     * @param suffix
     * @param fromsys
     * @param servername
     * @param enable
     * @return
     */
    @RequestMapping(value = "/addArcGisServer", method = RequestMethod.GET)
    @ResponseBody
    public String addArcgisServer(HttpServletRequest request, Integer zyid, String serverUrl,
                           String contextPath, Integer urlid, Integer isPublic, Integer suffix, String fromsys, String servername, Integer enable) {
        ServerRgBean serverRgBean = new ServerRgBean(serverUrl, zyid, isPublic, urlid, contextPath, suffix, fromsys, servername, enable != null ? enable : 1);
        String servertype = judgeServerType(serverUrl);
        serverRegisterService.addServer(serverRgBean);
        JSONObject json = new JSONObject();
        json.put("id", serverRgBean.getId());
        json.put("result", 0);
        String proxyurl = "";
        if (!StringUtils.isEmpty(suffix)) {
            proxyurl = new StringBuilder(properties.getProxy().getHost()).append("/arcgis/rest/services/").append(zyid).append("/").append(suffix.toString()).append("/").append(servertype).toString();
        } else {
            proxyurl = new StringBuilder(properties.getProxy().getHost()).append("/arcgis/rest/services/").append(zyid).append("/").append(servertype).toString();
        }
        json.put("proxyurl", proxyurl);
        return json.toJSONString(0);
    }
 
    public static String judgeServerType(String serverUrl) {
        //获取服务的path,按斜杠切割成数组
        String type = null;
        try {
            URI uri = new URI(serverUrl);
            String path = uri.getPath();
            String[] t = path.split("/");
            if (isdigit(t[t.length - 1])) {
                type = t[t.length - 2];
            } else {
                type = t[t.length - 1];
            }
            System.out.print(path);
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return type;
    }
 
    public static boolean isdigit(String str) {
 
        return str.matches("[0-9]+");
    }
 
    public static void main(String[] args) {
        String serverUrl = "http://18.1.1.55:6080/arcgis/rest/services/BaseMap_Remote_WGS1984_Mercator2016/ImageServer";
        judgeServerType(serverUrl);
    }
 
 
    /**
     * Arcgis Server 服务代理更新
     * @param request
     * @param zyid
     * @param serverUrl
     * @param contextPath
     * @param urlid
     * @param isPublic
     * @param suffix
     * @param id
     * @param fromsys
     * @param serverName
     * @param enable
     * @return
     */
    @RequestMapping(value = "/updateArcGisServer", method = RequestMethod.GET)
    @ApiIgnore
    @ResponseBody
    public String updateArcgisServer(HttpServletRequest request, Integer zyid, String serverUrl,
                              String contextPath, Integer urlid, Integer isPublic, Integer suffix, Integer id, String fromsys, String serverName, Integer enable) {
        Map<String, Object> params = new HashMap<>();
        // 获取源服务地址,上下文服务地址 ,资源id,将其持久化入库,并返回服务id
        params.put("serverurl", serverUrl);
        params.put("servername", serverName);
        params.put("resourceid", zyid);
        params.put("suffix", suffix);
        params.put("ispublic", isPublic);
        params.put("urlid", urlid);
        params.put("enable", enable);
        params.put("id", id);
        int result = serverRegisterService.updateServer(params);
        if (result > 0) {
            JSONObject json = new JSONObject();
            json.put("id", id);
            json.put("result", 0);
            return json.toJSONString(0);
        }
        return "error";
    }
 
 
    /**
     * ArcgisServer服务删除
     * @param id
     * @return
     */
    @RequestMapping(value = "/delArcGisServer", method = RequestMethod.GET)
    @ApiIgnore
    public ResponseEntity<String> delArcGisServer(Integer id) {
 
        int result = serverRegisterService.delServer(id);
        if (result > 0) {
            return new ResponseEntity<>("删除服务成功", HttpStatus.OK);
        }
        return new ResponseEntity<>("删除服务失败", HttpStatus.FAILED_DEPENDENCY);
    }
 
 
    @RequestMapping(value = "/genToken", method = RequestMethod.GET)
    @ResponseBody
    public String genToken(String userid, String password, String resourceid, HttpServletRequest req, Long expiration, String identyinfo, String appUrl, Boolean isPubzy, String appId, String applyuserid) {
        if (expiration == null || expiration == 0) expiration = this.expireSeconds;
        JSONObject json = new JSONObject();
 
        if (userid == null || userid.isEmpty() || password == null || password.isEmpty()) {
            json.put("code", 403);
            json.put("data", null);
            json.put("message", "用户名不能为空");
            return json.toJSONString(0);
        }
        if (!userid.equals(visitname) || !password.equals(visitpwd)) {
            json.put("code", 401);
            json.put("data", null);
            json.put("message", "用户名密码不正确");
            return json.toJSONString(0);
        }
        if (!resourceid.matches("[0-9]+")) {
            json.put("code", 401);
            json.put("data", null);
            json.put("message", "资源id格式不正确");
            return json.toJSONString(0);
        }
 
        Jedis jedis = jedisUtils.getJedis();
        try {
            CustomerToken cutToken = new CustomerToken();
            cutToken.setResourceId(resourceid);
            cutToken.setUserid(applyuserid == null ? "0" : applyuserid);
            cutToken.setAppUrl(appUrl == null ? "" : appUrl);
            cutToken.setAppId(appId == null ? "0" : appUrl);
            cutToken.setClientIp(req.getRemoteHost());
            cutToken.setRemark(identyinfo == null ? "" : identyinfo);
            String token = expiration != null ? JwtUtils.generateToken(cutToken.toString(), expiration) : JwtUtils.generateToken(cutToken.toString(), expireSeconds);
            TokenRecord record = new TokenRecord();
            record.setApplytime(new Date());
            record.setExpiration(expiration != null ? expiration.intValue() : expireSeconds.intValue());
            record.setIdentyinfo(identyinfo);
            record.setTokeninfo(cutToken.toString());
            if (!StringUtils.isEmpty(resourceid)) {
                record.setResourceid(Integer.valueOf(resourceid));
            }
            record.setToken(token);
            record.setTokenapplyer(req.getRemoteHost());
            logMapper.saveTokenRecord(record);
            jedis.setex("zytoken:" + token, expiration != null ? expiration.intValue() : expireSeconds.intValue(), "1");
            json.put("code", 200);
            json.put("data", token);
            json.put("message", "获取成功");
            return json.toJSONString(0);
        } catch (Exception e) {
 
            e.printStackTrace();
 
        } finally {
            jedis.close();
        }
        return "error";
    }
 
 
    /**
     * 获取token记录
     * @param token
     * @param identyinfo
     * @return
     */
    @RequestMapping(value = "/getTokenRecord", method = RequestMethod.GET)
    @ResponseBody
    public String getTokenRecord(String token, String identyinfo) {
        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("identyinfo", identyinfo);
        List<Map> list = logMapper.selectTokenRecord(map);
        return JSONUtil.toJsonStr(list);
 
    }
 
 
    /**
     * 应用系统申请token,时间传空,则使用各自默认的token时效值
     *
     * @param appId
     * @param req
     * @param expiration
     * @return
     */
    @RequestMapping(value = "/genAppToken", method = RequestMethod.GET)
    @ResponseBody
    public String genAppToken(Integer appId, HttpServletRequest req, Long expiration) {
        JSONObject json = new JSONObject();
        if (appId == null || appId == 0) {
            json.put("code", 403);
            json.put("data", null);
            json.put("message", "应用系统id不能为空");
            return json.toJSONString(0);
        }
        Jedis jedis = jedisUtils.getJedis();
        try {
 
            String token = JwtUtils.generateToken(expiration, appId);
            jedis.set(token, token);
            jedis.expire(token, expiration != null ? expiration.intValue() : expireSeconds.intValue());
            json.put("code", 200);
            json.put("data", token);
            json.put("message", "获取成功");
            return json.toJSONString(0);
        } catch (Exception e) {
            e.printStackTrace();
 
        } finally {
            jedis.close();
        }
 
        return "error";
    }
 
 
    @RequestMapping(value = "/genTokenByUserid", method = RequestMethod.GET)
    @ResponseBody
    public String genTokenForJGPT(Integer userid, HttpServletRequest req, Long expiration) {
        System.out.println(allowedips);
        JSONObject json = new JSONObject();
 
        Jedis jedis = jedisUtils.getJedis();
        try {
            String token = expiration != null ? JwtUtils.generateToken(userid, expiration) : JwtUtils.generateToken(userid, expireSeconds);
            jedis.set(token, token);
            jedis.expire(token, expiration != null ? expiration.intValue() : expireSeconds.intValue());
            return token;
        } catch (Exception e) {
            e.printStackTrace();
 
        } finally {
            jedis.close();
        }
        return "error";
    }
 
    @RequestMapping(value = "/queryServer")
    @ApiIgnore
    @ResponseBody
    public String queryServer(HttpServletRequest request, Integer zyid, String serverUrl,
                       String contextPath, Integer urlid, Integer isPublic, Integer suffix, String fromsys, Integer page, Integer limit, String servername) {
        Map<String, Object> params = new HashMap<>();
        Map<String, Object> resultmap = new HashMap<>();
        // 获取源服务地址,上下文服务地址 ,资源id,将其持久化入库,并返回服务id
        params.put("serverurl", serverUrl);
        params.put("resourceid", zyid);
        params.put("contextPath", contextPath);
        params.put("serverUrl", serverUrl);
        params.put("suffix", suffix);
        params.put("ispublic", isPublic);
        params.put("urlid", urlid);
        params.put("fromsys", fromsys);
        params.put("offSet", (page - 1) * limit);
        params.put("limit", limit);
        params.put("servername", servername);
        List<Map> list = serverRegisterService.queryServer(params);
        int total = serverRegisterService.queryTotalServer(params);
        resultmap.put("data", list);
        resultmap.put("totalpage", Math.ceil((double) total / (double) limit));
        resultmap.put("total", total);
        String str = JSONUtil.toJsonStr(resultmap);
        return str;
    }
 
    @RequestMapping(value = "/queryServer2", method = RequestMethod.POST)
    @ApiIgnore
    @ResponseBody
    public String queryServer2(HttpServletRequest request, Integer zyid, String serverUrl,
                        String contextPath, Integer urlid, Integer isPublic, String fromsys, Integer enable) {
        Map<String, Object> params = new HashMap<>();
        // 获取源服务地址,上下文服务地址 ,资源id,将其持久化入库,并返回服务id
        params.put("serverurl", serverUrl);
        params.put("resourceid", zyid);
        params.put("contextPath", contextPath);
        params.put("serverUrl", serverUrl);
        params.put("ispublic", isPublic);
        params.put("urlid", urlid);
        params.put("enable", enable);
        params.put("fromsys", fromsys);
        List<Map> list = serverRegisterService.queryServer2(params);
        return list.toString();
    }
 
    @RequestMapping(value = "/getProxyUrl", method = RequestMethod.POST)
    @ApiIgnore
    @ResponseBody
    public String getProxyUrl(HttpServletRequest request, String url) {
        Map<String, Object> params = new HashMap<>();
        // 获取源服务地址,上下文服务地址 ,资源id,将其持久化入库,并返回服务id
        URI uri = null;
        try {
            uri = new URI(url);
        } catch (URISyntaxException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        String path = uri.getPath();
        String param = path;
        path = path.replace("arcgis/rest/services/", "");
        param = param.replace("arcgis/rest/services/", "");
        path = path.substring(0, path.indexOf("MapServer"));
        param = param.substring(param.indexOf("/MapServer"));
        String result = "";
        try {
            result = byteArrayToHexString(path.getBytes("utf-8"));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
        StringBuilder serverurl = new StringBuilder(properties.getProxy().getHost());
        serverurl.append("/server/").append(result).append(param);
        return serverurl.toString();
    }
 
    private String mergePathAndSalt(String path) {
 
        if (path == null) {
            path = "";
        } else {
            path = path + salt;
        }
 
        return path;
 
    }
 
    private static String byteArrayToHexString(byte[] b) {
        StringBuffer resultsb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultsb.append(byteToHexString(b[i]));
        }
        return resultsb.toString();
    }
 
    private static String byteToHexString(byte b) {
        String hex = Integer.toHexString(b & 0xff);
        if (hex.length() < 2) {
            hex = "0" + hex;
        }
        return hex;
    }
 
 
}