From e16302f9d475c7cc4dd18c5abf1a23cb5502e362 Mon Sep 17 00:00:00 2001 From: whyclxw <810412026@qq.com> Date: 星期三, 28 五月 2025 14:57:56 +0800 Subject: [PATCH] 密码验证加- --- src/main/java/com/whyc/controller/LicenseController.java | 55 ++++++++++++++++++++++++++++++++++++++++++------------- 1 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/whyc/controller/LicenseController.java b/src/main/java/com/whyc/controller/LicenseController.java index 77aea27..d8da092 100644 --- a/src/main/java/com/whyc/controller/LicenseController.java +++ b/src/main/java/com/whyc/controller/LicenseController.java @@ -1,5 +1,6 @@ package com.whyc.controller; +import com.whyc.constant.YamlProperties; import com.whyc.dto.Response; import com.whyc.encryption.ByteConvertUtil; import com.whyc.encryption.SM2; @@ -17,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") @@ -29,6 +27,12 @@ public class LicenseController { @Resource private LicenseService service; + /** + * 绫诲姞杞芥椂鍒濆鍖杝m2鐨勫叕绉侀挜 + */ + final static ECPoint publicKey = SM2.getPublicKey(); + final static BigInteger privateKey = SM2.getPrivateKey(); + /** * 妫�楠屾湇鍔″櫒鏄惁娉ㄥ唽,鏄惁宸插瓨鍦ㄥ簭鍒楀彿 @@ -39,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(); } @@ -61,7 +67,7 @@ Response model=LicenseController.createLicense(System.currentTimeMillis()+"createTime"+ SerialNumberUtil.getSerialNumber()); //鍚屾椂,灏嗗簭鍒楀彿鐢熸垚鏃堕棿璁板綍鍒癮pplication鍩熶腑 //getApplication().setAttribute("serialNumberLicenseTime",System.currentTimeMillis()); - ActionUtil.getApplication().setAttribute("serialNumberLicenseTime",System.currentTimeMillis()); + //ActionUtil.getApplication().setAttribute("serialNumberLicenseTime",System.currentTimeMillis()); return model; } @@ -74,15 +80,17 @@ BigInteger privKey = x.importPrivateKey(realPath+"config/pri_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"); + //鏃х増鏈� + /*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); @@ -103,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:"; //鑾峰彇瑙e瘑鍚巐icense,闄勫甫鏍¢獙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]); @@ -171,8 +180,11 @@ //灏唒ri_key.ksm銆俻ub_key.ksm鏂囦欢鎷疯礉鑷砶sm鏂囦欢涓嬬劧鍚庤鍙杅ileName:/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"; @@ -180,20 +192,37 @@ createFile(fileDirName);//鍒涘缓鏂囦欢澶筴sm 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(); -- Gitblit v1.9.1