13693261870
2024-09-13 d68ba69e9f4d8edb7b62da57749de052f203c9f0
获取图层和字段信息
已添加2个文件
已修改3个文件
401 ■■■■■ 文件已修改
src/main/java/com/se/simu/controller/WaterController.java 7 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/domain/SedbLayer.java 66 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/helper/RsaHelper.java 215 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/service/SedbService.java 109 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/resources/application.yml 4 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/se/simu/controller/WaterController.java
@@ -147,6 +147,11 @@
    @ApiOperation(value = "* æµ‹è¯• *")
    @GetMapping("/test")
    public Object test() {
        return sedbService.test();
        try {
            return sedbService.test();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return ex.getMessage();
        }
    }
}
src/main/java/com/se/simu/domain/SedbLayer.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,66 @@
package com.se.simu.domain;
import cn.hutool.json.JSONArray;
/**
 * Sedb图层实体类
 *
 * @author WWW
 * @date 2024-09-13
 */
public class SedbLayer {
    private String id;
    private String name;
    private Integer dataType;
    private JSONArray fields;
    public SedbLayer() {
    }
    public SedbLayer(String id, String name) {
        this.id = id;
        this.name = name;
    }
    public SedbLayer(String id, String name, Integer dataType, JSONArray fields) {
        this.id = id;
        this.name = name;
        this.dataType = dataType;
        this.fields = fields;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getDataType() {
        return dataType;
    }
    public void setDataType(Integer dataType) {
        this.dataType = dataType;
    }
    public JSONArray getFields() {
        return fields;
    }
    public void setFields(JSONArray fields) {
        this.fields = fields;
    }
}
src/main/java/com/se/simu/helper/RsaHelper.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,215 @@
package com.se.simu.helper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;
import javax.crypto.Cipher;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
/**
 * RSA工具类
 *
 * @author WWW
 * @date   2024-09-13
 */
@Slf4j
public class RsaHelper {
    /**
     * ç§é’¥
     */
    private static String privateKey;
    /**
     * å…¬é’¥
     */
    private static String publicKey;
    /**
     * å¯†é’¥ç®—法
     */
    private static final String KEY_ALGORITHM = "RSA";
    /**
     * RSA密钥长度:1024 æˆ– 2048
     */
    private static final int DEFAULT_RSA_KEY_SIZE = 1024;
    /**
     * ç”Ÿæˆå…¬ç§é’¥
     */
    public static void generate() {
        Map<String, String> result = generateRsaKey(DEFAULT_RSA_KEY_SIZE);
        System.out.println("公钥为:" + result.get("publicKey"));
        System.out.println("私钥为:" + result.get("privateKey"));
    }
    /**
     * èŽ·å–RSA加密私钥
     *
     * @return
     * @throws IOException
     */
    public static String getPrivateKey() throws IOException {
        if (privateKey == null) {
            InputStream inPrivate = new ClassPathResource("config" + File.separator + "rsa_private_key.txt").getInputStream();
            privateKey = inputStream2String(inPrivate);
            inPrivate.close();
        }
        return privateKey;
    }
    public static void setPublicKey(String key) {
        publicKey = key;
    }
    /**
     * èŽ·å–RSA加密公钥
     *
     * @return
     * @throws IOException
     */
    public static String getPublicKey() throws IOException {
        if (publicKey == null) {
            InputStream inPrivate = new ClassPathResource("config" + File.separator + "rsa_public_key.txt").getInputStream();
            publicKey = inputStream2String(inPrivate);
            inPrivate.close();
        }
        return publicKey;
    }
    /**
     * è¯»å–文本文件
     *
     * @param fileName æ–‡ä»¶è·¯å¾„
     * @return
     * @throws IOException
     */
    public static String readFile(String fileName) throws IOException {
        File file = new File(fileName);
        BufferedReader br = new BufferedReader(new FileReader(file));
        StringBuilder result = new StringBuilder();
        String line = null;
        while ((line = br.readLine()) != null) {
            result.append(System.lineSeparator() + line);
        }
        br.close();
        return result.toString();
    }
    /**
     * æŠŠinputStream转成String
     *
     * @param is
     * @return
     * @throws IOException
     */
    private static String inputStream2String(InputStream is) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int i = -1;
        while ((i = is.read()) != -1) {
            baos.write(i);
        }
        String str = baos.toString();
        baos.close();
        return str;
    }
    /**
     * ç”ŸæˆRSA的公私钥
     *
     * @param keySize 1025 æˆ– 2048
     * @return
     */
    public static Map<String, String> generateRsaKey(int keySize) {
        Map<String, String> result = new HashMap<>(2);
        try {
            KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
            // åˆå§‹åŒ–密钥对生成器,密钥大小为1024 2048位
            keyPairGen.initialize(keySize, new SecureRandom());
            // ç”Ÿæˆä¸€ä¸ªå¯†é’¥å¯¹ï¼Œä¿å­˜åœ¨keyPair中
            KeyPair keyPair = keyPairGen.generateKeyPair();
            // å¾—到公钥字符串
            String pub = new String(Base64.encodeBase64(keyPair.getPublic().getEncoded()));
            result.put("publicKey", pub);
            // å¾—到私钥字符串
            String pri = new String(Base64.encodeBase64(keyPair.getPrivate().getEncoded()));
            result.put("privateKey", pri);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
        return result;
    }
    /**
     * RSA私钥解密
     *
     * @param str åŠ å¯†çš„å­—ç¬¦ä¸²
     * @return è§£å¯†å­—符串
     * @throws Exception åŠ å¯†è¿‡ç¨‹ä¸­çš„å¼‚å¸¸ä¿¡æ¯
     */
    public static String decrypt(String str) throws Exception {
        // 64位解码加密后的字符串
        byte[] inputByte = Base64.decodeBase64(str.getBytes(StandardCharsets.UTF_8));
        // Base64编码的私钥
        byte[] decoded = Base64.decodeBase64(getPrivateKey());
        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 outStr = new String(cipher.doFinal(inputByte));
        return outStr;
    }
    /**
     * RSA公钥加密
     *
     * @param str éœ€è¦åŠ å¯†çš„å­—ç¬¦ä¸²
     * @return å¯†æ–‡
     * @throws Exception åŠ å¯†è¿‡ç¨‹ä¸­çš„å¼‚å¸¸ä¿¡æ¯
     */
    public static String encrypt(String str) throws Exception {
        // Base64编码的公钥
        byte[] decoded = Base64.decodeBase64(getPublicKey());
        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;
    }
}
src/main/java/com/se/simu/service/SedbService.java
@@ -2,14 +2,17 @@
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.se.simu.domain.SedbLayer;
import com.se.simu.helper.RsaHelper;
import com.se.simu.helper.StringHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
/**
 * SDDB服务类
@@ -19,6 +22,7 @@
 */
@Slf4j
@Service
@SuppressWarnings("ALL")
public class SedbService {
    @Value("${sedb.host}")
    String host;
@@ -29,16 +33,44 @@
    @Value("${sedb.pwd}")
    String pwd;
    @Value("#{'${sedb.layerNames}'}")
    List<String> layerNames;
    String password;
    @Resource
    RestTemplate restTemplate;
    public String test() {
    public String test() throws Exception {
        String token = getToken();
        String dbid = getDbId(token);
        System.out.println(dbid);
        List<SedbLayer> layers = getLayers(token, dbid);
        return dbid;
    }
    public String getToken() throws Exception {
        Map<String, Object> map = new HashMap<>(2);
        map.put("userid", user);
        map.put("password", getPassword());
        JSONObject obj = restTemplate.postForObject(host + "account-service/security/login", map, JSONObject.class);
        JSONObject data = obj.getJSONObject("data");
        return data.getStr("token");
    }
    public String getPassword() throws Exception {
        if (StringUtils.isEmpty(password)) {
            String key = getPublicKey();
            RsaHelper.setPublicKey(key);
            password = RsaHelper.encrypt(pwd);
        }
        return password;
    }
    public String getPublicKey() {
@@ -46,20 +78,6 @@
        JSONObject obj = restTemplate.getForObject(host + "account-service/security/publickey", JSONObject.class);
        return obj.getStr("data");
    }
    public String getToken() {
        String key = getPublicKey();
        Map<String, Object> map = new HashMap<>(2);
        map.put("userid", user);
        map.put("password", pwd);
        JSONObject obj = restTemplate.postForObject(host + "account-service/security/login", map, JSONObject.class);
        JSONObject data = obj.getJSONObject("data");
        return data.getStr("token");
    }
    public String getDbId(String token) {
@@ -77,4 +95,59 @@
        return dbObj.getStr("dbid");
    }
    public List<SedbLayer> getLayers(String token, String dbid) {
        String uri = String.format("%sgeo-service/entitydb/map/config?dbid=%s&token=%s", host, dbid, token);
        JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
        JSONObject data = obj.getJSONObject("data");
        JSONArray arr = data.getJSONArray("layers");
        if (null == arr || arr.size() == 0) {
            return null;
        }
        List<SedbLayer> layers = new ArrayList<>();
        for (int i = 0, c = arr.size(); i < c; i++) {
            JSONObject jb = arr.getJSONObject(i);
            String name = jb.getStr("name");
            if (layerNames.contains(name)) {
                String id = jb.getStr("id");
                Integer dataType = jb.getInt("_data_type");
                JSONArray fields = jb.getJSONArray("fields");
                layers.add(new SedbLayer(id, name, dataType, fields));
            }
        }
        return layers;
    }
    public List<SedbLayer> getLayers2(String token, String dbid) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.add(Calendar.DAY_OF_MONTH, -1);
        String date = StringHelper.YMD_FORMAT.format(calendar.getTime());
        String uri = String.format("%sgeo-service/statis/layer/data/info?dbid=%s&token=%s&caldate=%s", host, dbid, token, date);
        JSONObject obj = restTemplate.getForObject(uri, JSONObject.class);
        JSONArray arr = obj.getJSONArray("data");
        if (null == arr || arr.size() == 0) {
            return null;
        }
        List<SedbLayer> layers = new ArrayList<>();
        for (int i = 0, c = arr.size(); i < c; i++) {
            JSONObject jb = arr.getJSONObject(i);
            String name = jb.getStr("name");
            if (layerNames.contains(name)) {
                String id = jb.getStr("layerid");
                layers.add(new SedbLayer(id, name));
            }
        }
        return layers;
    }
}
src/main/resources/application.yml
@@ -44,5 +44,5 @@
sedb:
  host: http://106.120.22.26:8013/
  user: WUWEIWEI
  #pwd: WUWEIWEI
  pwd: cDS4uMRTR2urgQDNpwkZhpnlzel+R3S9ChDlzk/UmcN8V5n30NwY4iJ9s8DtKr0oUdE7tQ3M+OZlYupw201unqxtUadiyCAme0F3W6eMvB5qanY0nao54TldiZJcIAlB1wqQgh9LinDILS4dRHy3jmwvc+5cOKMvy8WLJ3LZa4A=
  pwd: WUWEIWEI
  layerNames: ç®¡ç‚¹,管线,建筑物