whyclxw
2025-05-28 e16302f9d475c7cc4dd18c5abf1a23cb5502e362
src/main/java/com/whyc/controller/LicenseController.java
@@ -1,11 +1,13 @@
package com.whyc.controller;
import com.whyc.constant.YamlProperties;
import com.whyc.dto.Response;
import com.whyc.encryption.ByteConvertUtil;
import com.whyc.encryption.SM2;
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;
@@ -16,10 +18,7 @@
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.math.BigInteger;
@RequestMapping("license")
@@ -28,6 +27,12 @@
public class LicenseController {
    @Resource
    private LicenseService service;
    /**
     * 类加载时初始化sm2的公私钥
     */
    final static ECPoint publicKey = SM2.getPublicKey();
    final static BigInteger privateKey = SM2.getPrivateKey();
    /**
     * 检验服务器是否注册,是否已存在序列号
@@ -38,7 +43,9 @@
    @GetMapping("/checkRegistered")
    @ApiOperation(value = "校验服务器是否注册")
    public Response checkRegistered(){
        //return new Response().set(1,true);    //测试环境
        if(YamlProperties.profileType.contains("dev")){
            return new Response().set(1, true);    //测试环境
        }
        return service.checkLicenseExpired();
    }
@@ -60,6 +67,7 @@
        Response model=LicenseController.createLicense(System.currentTimeMillis()+"createTime"+ SerialNumberUtil.getSerialNumber());
        //同时,将序列号生成时间记录到application域中
        //getApplication().setAttribute("serialNumberLicenseTime",System.currentTimeMillis());
        //ActionUtil.getApplication().setAttribute("serialNumberLicenseTime",System.currentTimeMillis());
        return model;
    }
@@ -71,34 +79,18 @@
        System.out.println("realPath: "+realPath);
        BigInteger privKey = x.importPrivateKey(realPath+"config/pri_key.ksm");
        ECPoint pubKey = x.importPublicKey(realPath+"config/pub_key.ksm");*/
        /*ClassPathResource classPathResource_pub = new ClassPathResource("/config/pub_key.ksm");
        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";//打包版本
        try {
            inputStream_pub = classPathResource_pub.getInputStream();
            FileOutputStream fos = new FileOutputStream(fileDirName+"/pub_key.ksm");
            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();
        }*/
        String fileDirName=LicenseController.getRealPath("pub_key.ksm");
        ECPoint pubKey = x.importPublicKey(fileDirName+"/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);
@@ -119,15 +111,16 @@
        SM2 x = new SM2();
        //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");
        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]);
@@ -187,26 +180,53 @@
    //将pri_key.ksm。pub_key.ksm文件拷贝至ksm文件下然后读取fileName:/config/pri_key.ksm
    public static String getRealPath(String fileName){
        //过滤特殊字符,避免路径遍历攻击
        fileName = ActionUtil.filterFileName(fileName);
        ClassPathResource classPathResource = new ClassPathResource("/config/"+fileName);
        InputStream inputStream_pub = null;
        FileOutputStream fos=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);
            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();
        }finally {
            if(inputStream_pub!=null){
                try {
                    inputStream_pub.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return fileDirName;
    }
    public static void createFile(String pathName) {
        //过滤特殊字符,避免路径遍历攻击
        pathName = ActionUtil.filterFileName(pathName);
        File dir = new File(pathName);
        if (!dir.exists()) {// 判断目录是否存在
            dir.mkdir();
        }
    }
}