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 request = new HttpEntity<>(json, headers); RestTemplate restTemplate = new RestTemplate(); ResponseEntity 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 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 request = new HttpEntity<>(json, headers); ResponseEntity 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 request = new HttpEntity<>(json, headers); ResponseEntity 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; } }