lxw
2023-05-25 f3c27fb78447449a950ba73c5e72ceda64ad8a12
src/main/java/com/whyc/controller/LicenseController.java
@@ -6,14 +6,18 @@
import com.whyc.pojo.License;
import com.whyc.service.LicenseService;
import com.whyc.util.AESUtil;
import com.whyc.util.ActionUtil;
import com.whyc.util.SerialNumberUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.bouncycastle.math.ec.ECPoint;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.*;
import java.math.BigInteger;
@RequestMapping("license")
@@ -22,6 +26,12 @@
public class LicenseController {
    @Resource
    private LicenseService service;
    /**
     * 类加载时初始化sm2的公私钥
     */
    final static ECPoint publicKey = SM2.getPublicKey();
    final static BigInteger privateKey = SM2.getPrivateKey();
    /**
     * 检验服务器是否注册,是否已存在序列号
@@ -32,6 +42,7 @@
    @GetMapping("/checkRegistered")
    @ApiOperation(value = "校验服务器是否注册")
    public Response checkRegistered(){
        //return new Response().set(1,true);    //测试环境
        return service.checkLicenseExpired();
    }
@@ -53,6 +64,7 @@
        Response model=LicenseController.createLicense(System.currentTimeMillis()+"createTime"+ SerialNumberUtil.getSerialNumber());
        //同时,将序列号生成时间记录到application域中
        //getApplication().setAttribute("serialNumberLicenseTime",System.currentTimeMillis());
        //ActionUtil.getApplication().setAttribute("serialNumberLicenseTime",System.currentTimeMillis());
        return model;
    }
@@ -60,17 +72,22 @@
    public static Response createLicense(String serialNumber){
        //初始化sm2参数x
        SM2 x = new SM2();
        String realPath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        /*String realPath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        System.out.println("realPath: "+realPath);
        BigInteger privKey = x.importPrivateKey(realPath+"config/pri_key.ksm");
        ECPoint pubKey = x.importPublicKey(realPath+"config/pub_key.ksm");
        ECPoint pubKey = x.importPublicKey(realPath+"config/pub_key.ksm");*/
        //旧版本
        /*String fileDirName=LicenseController.getRealPath("pub_key.ksm");
        ECPoint pubKey = x.importPublicKey(fileDirName+"/pub_key.ksm");*/
        //System.out.println("pubKey "+pubKey);
        /*String origin = "Company: Fuguang Electronic\n"
                + "Project:BTS monitor platform\n"
                + "Licence type:Permanent";*/
        //获取加密列表
        //System.out.println("origin "+origin);
        byte[] encryptResult = x.encrypt(serialNumber, pubKey);
        //byte[] encryptResult = x.encrypt(serialNumber, pubKey);
        byte[] encryptResult = x.encrypt(serialNumber, publicKey);
        String encrypt = ByteConvertUtil.bytesToHexString(encryptResult);
        //System.out.println("encrypt:"+encrypt);
        return new Response().set(1,encrypt);
@@ -89,16 +106,18 @@
        boolean res = false;
        //初始化sm2参数x
        SM2 x = new SM2();
        String realPath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        BigInteger privKey = x.importPrivateKey(realPath + "config/pri_key.ksm");
        ECPoint pubKey = x.importPublicKey(realPath + "config/pub_key.ksm");
        //String realPath = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        //ECPoint pubKey = x.importPublicKey(realPath + "config/pub_key.ksm");
        /*//旧版本
        String fileDirName=LicenseController.getRealPath("pri_key.ksm");
        BigInteger privKey = x.importPrivateKey(fileDirName + "/pri_key.ksm");*/
        String origin = "Company: Fuguang Electronic\n"
                + "Project:BTS monitor platform\n"
                + "Licence duration:";
        //获取解密后license,附带校验license编码格式
        String decryptResult = null;
        byte[] bytes = ByteConvertUtil.hexToByteArray(license);
        decryptResult = x.decrypt(bytes, privKey);
        decryptResult = x.decrypt(bytes, privateKey);
        //用户只能往小调时间
        String[] split1 = decryptResult.split("machineCode:");
        Long registerCodeTime = Long.valueOf(split1[0]);
@@ -144,7 +163,8 @@
            //model.setData(decryptResult);
            //model.setMsgN(serialNumberStr);
        }
       /* model.setCode(1);   //测试环境
        model.setData(true);*/
        return model;
    }
@@ -155,5 +175,35 @@
    }
    //将pri_key.ksm。pub_key.ksm文件拷贝至ksm文件下然后读取fileName:/config/pri_key.ksm
    public static String getRealPath(String fileName){
        ClassPathResource classPathResource = new ClassPathResource("/config/"+fileName);
        InputStream inputStream_pub = null;
        ApplicationHome applicationHome = new ApplicationHome(LicenseController.class);
        File jarFile = applicationHome.getDir();
        String fileDirName = jarFile.getParentFile().toString()+ File.separator+"ksm";
        //String fileDirName = jarFile.toString()+File.separator+"ksm";//打包版本
        createFile(fileDirName);//创建文件夹ksm
        try {
            inputStream_pub = classPathResource.getInputStream();
            FileOutputStream fos = new FileOutputStream(fileDirName+"/"+fileName);
            byte[] b = new byte[1024];
            int length;
            while((length = inputStream_pub.read(b))>0){
                fos.write(b,0,length);
            }
            inputStream_pub.close();
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return fileDirName;
    }
    public static void createFile(String pathName) {
        File dir = new File(pathName);
        if (!dir.exists()) {// 判断目录是否存在
            dir.mkdir();
        }
    }
}