张洋洋
2025-02-11 49aa6ed510d18506cc11d24299899ab0b35047b3
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
package com.se.simu.utils;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.io.Resources;
import com.se.simu.domain.dto.GridDto;
import org.apache.commons.codec.binary.Base64;
import org.springframework.http.*;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriUtils;
 
import javax.crypto.Cipher;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.List;
 
/**
 * 实体库请求
 */
public class EntityLibraryUtils {
    /**
     * 获取加密公钥
     *
     * @return 共钥
     */
    public static String getPublicKey() {
        JSONObject jsonObject = new JSONObject();
        String json = jsonObject.toJSONString();
        // 发送JSON格式的POST请求
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity<>(json, headers);
        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://106.120.22.26:8024/geo-service/setting/publickey", request, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            String body = responseEntity.getBody();
            return JSONObject.parseObject(body).getString("data");
        }
        return null;
    }
 
    public static String encrypt(String str, String publicKey) throws Exception {
        //Base64编码的公钥
        byte[] decoded = Base64.decodeBase64(publicKey);
        RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
        // RSA加密:RSA/ECB/NoPadding
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.ENCRYPT_MODE, pubKey);
        String outstr = Base64.encodeBase64String(cipher.doFinal(str.getBytes(StandardCharsets.UTF_8)));
        return outstr;
    }
 
    public static String decrypt(String str, String privateKey) throws Exception {
        if (str == null || "".equals(str)) {
            return str;
        }
        //64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(str.getBytes(StandardCharsets.UTF_8));
        //Base64编码的私钥
        byte[] decoded = Base64.decodeBase64(privateKey);
        RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
        //RSA解密:RSA/ECB/NoPadding
        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, priKey);
        String outstn = new String(cipher.doFinal(inputByte), StandardCharsets.UTF_8);
        return outstn;
    }
 
    public static void main(String[] args) throws Exception {
        String publickey = getPublicKey();
        System.out.println("公钥=" + publickey);
        String en = encrypt("id in ('46235','49876')", publickey);
        System.out.println("加密字符串=" + en);
        String encode = UriUtils.encode(en, StandardCharsets.UTF_8);
        System.out.println("encode字符串=" + encode);
        String decode = UriUtils.decode(encode, StandardCharsets.UTF_8);
        System.out.println("decode字符串=" + decode);
        System.out.println("解密字符串=" + decrypt(decode, "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAJiF6WkdgHlimpakWJMvH3Xnjwws7qqoo1rhbg/iLzLGly/EKLvUzD6D7FUFk+GAP/sOKh1cZZEvi1KkGd6OufqMKdDvRvVtGjXExI5MLJDVDaYTsqhzNF8maB2H7dwR+iiDGph2DMPQtuV/k/dalXMxR6O8Q2MPQfaUiOVebKBbAgMBAAECgYARrPs21ldsOdQmfxdQv1ZLCLHYPGDQYEjGIHfr2U+U99TPkVETK38cA5fg5ouTx5QimSqiSnHu2G6x/hiNZUcCJp/1agvsFEI2FLokShYbitOYa07Da1eIKpZA5F+P5j5/QOVpVsWOxvEDA/dkF+vrV4vU/iV7H3QJqBfsLSxEeQJBAO43SVkzJM6dqnRw7sib7SOvguicnr7UKt0Tadc1PwLMWCCFu+4p3iN9Zj/7K5VHjomW+NIv7UAGynDfHBpv1x0CQQCj6ONm+m4FlaTdg+Pc36BBy4Gd6ucm6WfjDTHMaZi3uzINjTPBut6DmrUt68dyHdGpo6OIjCdX3xU27HtmjiPXAkEAwjdhHdCM2cfCCV1p0TUPimC2ImBPLNZefBAv4r4OuYFQ+HMQXYTVD6pViySEzBijJZppEzTwAZuHwa6lgwhcIQJBAI8PBOssSDq3kV2Fb6unwseqR0b9byKXNQUGzyAKSjCSQe1yAGpmHy/eJ6Qc1cbUH9pf6KuVKAGZw3pcjJfGF6ECQQCx9dJ2qJjm2R78/HH0SkOATpMATgewtHH5tRJfCJv7NLveuYNRFH1An4APWYq9IdVfyn+4gyXz3OClteE7jAkt"));
    }
 
    /**
     * 管线列表
     *
     * @param dtos 四个坐标点
     * @return 框选内部管线
     */
    public static JSONArray getPointInfo(List<GridDto> dtos) {
        JSONArray paramArray = new JSONArray();
        for (GridDto dto : dtos
        ) {
            paramArray.add(ProjectionToGeographicUtil.get4548Point(dto.getLon(), dto.getLat()));
        }
        paramArray.add(ProjectionToGeographicUtil.get4548Point(dtos.get(0).getLon(), dtos.get(0).getLat()));
        JSONObject jsonObject = getModule("layerQueryParams.json");
        jsonObject.getJSONObject("geometry").getJSONArray("coordinates").add(paramArray);
        RestTemplate restTemplate = new RestTemplate();
        // 发送JSON格式的POST请求
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String json = jsonObject.toJSONString();
        HttpEntity<String> request = new HttpEntity<>(json, headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity("http://106.120.22.26:8024/geo-service/entitydbdata/layer/query", request, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            String body = responseEntity.getBody();
            return JSONObject.parseObject(body).getJSONObject("data").getJSONArray("items");
        }
        return null;
    }
 
    /**
     * 管线列表详情
     *
     * @param param 查询参数
     * @return 参数内详情
     */
    public static JSONArray getPointDetail(String param) {
        JSONObject jsonObject = getModule("layerQueryDetailParams.json");
        jsonObject.put("where", param);
        RestTemplate restTemplate = new RestTemplate();
        // 发送JSON格式的POST请求
        StringHttpMessageConverter converter = new StringHttpMessageConverter(StandardCharsets.UTF_8);
        converter.setSupportedMediaTypes(MediaType.parseMediaTypes("text/plain;charset=UTF-8"));
        restTemplate.getMessageConverters().add(0, converter);
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        String json = jsonObject.toJSONString();
        HttpEntity<String> request = new HttpEntity<>(json, headers);
        ResponseEntity<String> responseEntity = restTemplate.exchange("http://106.120.22.26:8024/geo-service/entitydbdata/layer/query", HttpMethod.POST, request, String.class);
        if (responseEntity.getStatusCode().is2xxSuccessful()) {
            String body = responseEntity.getBody();
            return JSONObject.parseObject(body).getJSONArray("features");
        }
        return null;
    }
 
    /**
     * 获取请求json
     *
     * @param moduleName json名
     * @return json内容
     */
    private static JSONObject getModule(String moduleName) {
        JSONObject jsonObject = new JSONObject();
        try {
            URL resource = Resources.getResource(moduleName);
            String fileContent = Resources.toString(resource, StandardCharsets.UTF_8);
            jsonObject = JSONObject.parseObject(fileContent);
            System.out.println(fileContent);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
}