From c635d78b479510ebe2556a420948effcd30a0731 Mon Sep 17 00:00:00 2001 From: skyouc Date: 星期六, 21 十二月 2024 18:40:43 +0800 Subject: [PATCH] 新建德森项目分支 --- zy-asrs-wms/src/main/java/com/zy/asrs/wms/system/license/entity/license/CustomLicenseManager.java | 484 ++++++++++++++++++++++++++-------------------------- 1 files changed, 242 insertions(+), 242 deletions(-) diff --git a/zy-asrs-wms/src/main/java/com/zy/asrs/wms/system/license/entity/license/CustomLicenseManager.java b/zy-asrs-wms/src/main/java/com/zy/asrs/wms/system/license/entity/license/CustomLicenseManager.java index 4704e30..463c50f 100644 --- a/zy-asrs-wms/src/main/java/com/zy/asrs/wms/system/license/entity/license/CustomLicenseManager.java +++ b/zy-asrs-wms/src/main/java/com/zy/asrs/wms/system/license/entity/license/CustomLicenseManager.java @@ -1,243 +1,243 @@ -package com.zy.asrs.wms.system.license.entity.license; - -import com.alibaba.fastjson.JSON; -import com.zy.asrs.framework.common.Cools; -import de.schlichtherle.license.*; -import de.schlichtherle.xml.GenericCertificate; -import org.apache.logging.log4j.LogManager; -import org.apache.logging.log4j.Logger; - -import java.beans.XMLDecoder; -import java.io.BufferedInputStream; -import java.io.ByteArrayInputStream; -import java.io.UnsupportedEncodingException; -import java.util.Date; -import java.util.List; - -/** - * 鑷畾涔塋icenseManager锛岀敤浜庡鍔犻澶栫殑鏈嶅姟鍣ㄧ‖浠朵俊鎭牎楠� - */ -public class CustomLicenseManager extends LicenseManager{ - private static Logger logger = LogManager.getLogger(CustomLicenseManager.class); - - //XML缂栫爜 - private static final String XML_CHARSET = "UTF-8"; - //榛樿BUFSIZE - private static final int DEFAULT_BUFSIZE = 8 * 1024; - - public CustomLicenseManager() { - - } - - public CustomLicenseManager(LicenseParam param) { - super(param); - } - - /** - * 澶嶅啓create鏂规硶 - */ - @Override - protected synchronized byte[] create( - LicenseContent content, - LicenseNotary notary) - throws Exception { - initialize(content); - this.validateCreate(content); - final GenericCertificate certificate = notary.sign(content); - return getPrivacyGuard().cert2key(certificate); - } - - /** - * 澶嶅啓install鏂规硶锛屽叾涓璿alidate鏂规硶璋冪敤鏈被涓殑validate鏂规硶锛屾牎楠孖P鍦板潃銆丮ac鍦板潃绛夊叾浠栦俊鎭� - */ - @Override - protected synchronized LicenseContent install( - final byte[] key, - final LicenseNotary notary) - throws Exception { - final GenericCertificate certificate = getPrivacyGuard().key2cert(key); - - notary.verify(certificate); - final LicenseContent content = (LicenseContent)this.load(certificate.getEncoded()); - this.validate(content); - setLicenseKey(key); - setCertificate(certificate); - - return content; - } - - /** - * 澶嶅啓verify鏂规硶锛岃皟鐢ㄦ湰绫讳腑鐨剉alidate鏂规硶锛屾牎楠孖P鍦板潃銆丮ac鍦板潃绛夊叾浠栦俊鎭� - */ - @Override - protected synchronized LicenseContent verify(final LicenseNotary notary) - throws Exception { - GenericCertificate certificate = getCertificate(); - - // Load license key from preferences, - final byte[] key = getLicenseKey(); - if (null == key){ - throw new NoLicenseInstalledException(getLicenseParam().getSubject()); - } - - certificate = getPrivacyGuard().key2cert(key); - notary.verify(certificate); - final LicenseContent content = (LicenseContent)this.load(certificate.getEncoded()); - this.validate(content); - setCertificate(certificate); - - return content; - } - - /** - * 鏍¢獙鐢熸垚璇佷功鐨勫弬鏁颁俊鎭� - */ - protected synchronized void validateCreate(final LicenseContent content) - throws LicenseContentException { - final LicenseParam param = getLicenseParam(); - - final Date now = new Date(); - final Date notBefore = content.getNotBefore(); - final Date notAfter = content.getNotAfter(); - if (null != notAfter && now.after(notAfter)){ - throw new LicenseContentException("璇佷功澶辨晥鏃堕棿涓嶈兘鏃╀簬褰撳墠鏃堕棿"); - } - if (null != notBefore && null != notAfter && notAfter.before(notBefore)){ - throw new LicenseContentException("璇佷功鐢熸晥鏃堕棿涓嶈兘鏅氫簬璇佷功澶辨晥鏃堕棿"); - } - final String consumerType = content.getConsumerType(); - if (null == consumerType){ - throw new LicenseContentException("鐢ㄦ埛绫诲瀷涓嶈兘涓虹┖"); - } - } - - - /** - * 澶嶅啓validate鏂规硶锛屽鍔營P鍦板潃銆丮ac鍦板潃绛夊叾浠栦俊鎭牎楠� - */ - @Override - protected synchronized void validate(final LicenseContent content) - throws LicenseContentException { - //1. 棣栧厛璋冪敤鐖剁被鐨剉alidate鏂规硶 - super.validate(content); - - //2. 鐒跺悗鏍¢獙鑷畾涔夌殑License鍙傛暟 - //License涓彲琚厑璁哥殑鍙傛暟淇℃伅 -// LicenseCheck expectedCheckModel = (LicenseCheck) content.getExtra(); - LicenseCheck expectedCheckModel = JSON.parseObject(content.getExtra().toString(), LicenseCheck.class); - //褰撳墠鏈嶅姟鍣ㄧ湡瀹炵殑鍙傛暟淇℃伅 - LicenseCheck serverCheckModel = getServerInfos(); - - if(expectedCheckModel != null && serverCheckModel != null){ - //鏍¢獙IP鍦板潃 - if(!checkIpAddress(expectedCheckModel.getIpAddress(),serverCheckModel.getIpAddress())){ - throw new LicenseContentException("褰撳墠鏈嶅姟鍣ㄧ殑IP娌″湪鎺堟潈鑼冨洿鍐�"); - } - - //鏍¢獙Mac鍦板潃 - if(!checkIpAddress(expectedCheckModel.getMacAddress(),serverCheckModel.getMacAddress())){ - throw new LicenseContentException("褰撳墠鏈嶅姟鍣ㄧ殑Mac鍦板潃娌″湪鎺堟潈鑼冨洿鍐�"); - } - - //鏍¢獙涓绘澘搴忓垪鍙� - if(!checkSerial(expectedCheckModel.getMainBoardSerial(),serverCheckModel.getMainBoardSerial())){ - throw new LicenseContentException("褰撳墠鏈嶅姟鍣ㄧ殑涓绘澘搴忓垪鍙锋病鍦ㄦ巿鏉冭寖鍥村唴"); - } - - //鏍¢獙CPU搴忓垪鍙� - if(!checkSerial(expectedCheckModel.getCpuSerial(),serverCheckModel.getCpuSerial())){ - throw new LicenseContentException("褰撳墠鏈嶅姟鍣ㄧ殑CPU搴忓垪鍙锋病鍦ㄦ巿鏉冭寖鍥村唴"); - } - }else{ - throw new LicenseContentException("涓嶈兘鑾峰彇鏈嶅姟鍣ㄧ‖浠朵俊鎭�"); - } - } - - - /** - * 閲嶅啓XMLDecoder瑙f瀽XML - */ - private Object load(String encoded){ - BufferedInputStream inputStream = null; - XMLDecoder decoder = null; - try { - inputStream = new BufferedInputStream(new ByteArrayInputStream(encoded.getBytes(XML_CHARSET))); - - decoder = new XMLDecoder(new BufferedInputStream(inputStream, DEFAULT_BUFSIZE),null,null); - - return decoder.readObject(); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } finally { - try { - if(decoder != null){ - decoder.close(); - } - if(inputStream != null){ - inputStream.close(); - } - } catch (Exception e) { - logger.error("XMLDecoder瑙f瀽XML澶辫触",e); - } - } - - return null; - } - - /** - * 鑾峰彇褰撳墠鏈嶅姟鍣ㄩ渶瑕侀澶栨牎楠岀殑License鍙傛暟 - */ - private LicenseCheck getServerInfos(){ - //鎿嶄綔绯荤粺绫诲瀷 - String osName = System.getProperty("os.name").toLowerCase(); - AbstractServerInfos abstractServerInfos = null; - - //鏍规嵁涓嶅悓鎿嶄綔绯荤粺绫诲瀷閫夋嫨涓嶅悓鐨勬暟鎹幏鍙栨柟娉� - if (osName.startsWith("windows")) { - abstractServerInfos = new WindowsServerInfos(); - } else if (osName.startsWith("linux")) { - abstractServerInfos = new LinuxServerInfos(); - }else{//鍏朵粬鏈嶅姟鍣ㄧ被鍨� - abstractServerInfos = new WindowsServerInfos(); - } - - return abstractServerInfos.getServerInfos(); - } - - /** - * 鏍¢獙褰撳墠鏈嶅姟鍣ㄧ殑IP/Mac鍦板潃鏄惁鍦ㄥ彲琚厑璁哥殑IP鑼冨洿鍐�<br/> - * 濡傛灉瀛樺湪IP鍦ㄥ彲琚厑璁哥殑IP/Mac鍦板潃鑼冨洿鍐咃紝鍒欒繑鍥瀟rue - */ - private boolean checkIpAddress(List<String> expectedList,List<String> serverList){ - if(expectedList != null && expectedList.size() > 0){ - if(serverList != null && serverList.size() > 0){ - for(String expected : expectedList){ - if(serverList.contains(expected.trim())){ - return true; - } - } - } - - return false; - }else { - return true; - } - } - - /** - * 鏍¢獙褰撳墠鏈嶅姟鍣ㄧ‖浠讹紙涓绘澘銆丆PU绛夛級搴忓垪鍙锋槸鍚﹀湪鍙厑璁歌寖鍥村唴 - */ - private boolean checkSerial(String expectedSerial,String serverSerial){ - if(!Cools.isEmpty(expectedSerial)){ - if(!Cools.isEmpty(serverSerial)){ - if(expectedSerial.equals(serverSerial)){ - return true; - } - } - return false; - }else{ - return true; - } - } - +package com.zy.asrs.wms.system.license.entity.license; + +import com.alibaba.fastjson.JSON; +import com.zy.asrs.framework.common.Cools; +import de.schlichtherle.license.*; +import de.schlichtherle.xml.GenericCertificate; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.beans.XMLDecoder; +import java.io.BufferedInputStream; +import java.io.ByteArrayInputStream; +import java.io.UnsupportedEncodingException; +import java.util.Date; +import java.util.List; + +/** + * 鑷畾涔塋icenseManager锛岀敤浜庡鍔犻澶栫殑鏈嶅姟鍣ㄧ‖浠朵俊鎭牎楠� + */ +public class CustomLicenseManager extends LicenseManager{ + private static Logger logger = LogManager.getLogger(CustomLicenseManager.class); + + //XML缂栫爜 + private static final String XML_CHARSET = "UTF-8"; + //榛樿BUFSIZE + private static final int DEFAULT_BUFSIZE = 8 * 1024; + + public CustomLicenseManager() { + + } + + public CustomLicenseManager(LicenseParam param) { + super(param); + } + + /** + * 澶嶅啓create鏂规硶 + */ + @Override + protected synchronized byte[] create( + LicenseContent content, + LicenseNotary notary) + throws Exception { + initialize(content); + this.validateCreate(content); + final GenericCertificate certificate = notary.sign(content); + return getPrivacyGuard().cert2key(certificate); + } + + /** + * 澶嶅啓install鏂规硶锛屽叾涓璿alidate鏂规硶璋冪敤鏈被涓殑validate鏂规硶锛屾牎楠孖P鍦板潃銆丮ac鍦板潃绛夊叾浠栦俊鎭� + */ + @Override + protected synchronized LicenseContent install( + final byte[] key, + final LicenseNotary notary) + throws Exception { + final GenericCertificate certificate = getPrivacyGuard().key2cert(key); + + notary.verify(certificate); + final LicenseContent content = (LicenseContent)this.load(certificate.getEncoded()); + this.validate(content); + setLicenseKey(key); + setCertificate(certificate); + + return content; + } + + /** + * 澶嶅啓verify鏂规硶锛岃皟鐢ㄦ湰绫讳腑鐨剉alidate鏂规硶锛屾牎楠孖P鍦板潃銆丮ac鍦板潃绛夊叾浠栦俊鎭� + */ + @Override + protected synchronized LicenseContent verify(final LicenseNotary notary) + throws Exception { + GenericCertificate certificate = getCertificate(); + + // Load license key from preferences, + final byte[] key = getLicenseKey(); + if (null == key){ + throw new NoLicenseInstalledException(getLicenseParam().getSubject()); + } + + certificate = getPrivacyGuard().key2cert(key); + notary.verify(certificate); + final LicenseContent content = (LicenseContent)this.load(certificate.getEncoded()); + this.validate(content); + setCertificate(certificate); + + return content; + } + + /** + * 鏍¢獙鐢熸垚璇佷功鐨勫弬鏁颁俊鎭� + */ + protected synchronized void validateCreate(final LicenseContent content) + throws LicenseContentException { + final LicenseParam param = getLicenseParam(); + + final Date now = new Date(); + final Date notBefore = content.getNotBefore(); + final Date notAfter = content.getNotAfter(); + if (null != notAfter && now.after(notAfter)){ + throw new LicenseContentException("璇佷功澶辨晥鏃堕棿涓嶈兘鏃╀簬褰撳墠鏃堕棿"); + } + if (null != notBefore && null != notAfter && notAfter.before(notBefore)){ + throw new LicenseContentException("璇佷功鐢熸晥鏃堕棿涓嶈兘鏅氫簬璇佷功澶辨晥鏃堕棿"); + } + final String consumerType = content.getConsumerType(); + if (null == consumerType){ + throw new LicenseContentException("鐢ㄦ埛绫诲瀷涓嶈兘涓虹┖"); + } + } + + + /** + * 澶嶅啓validate鏂规硶锛屽鍔營P鍦板潃銆丮ac鍦板潃绛夊叾浠栦俊鎭牎楠� + */ + @Override + protected synchronized void validate(final LicenseContent content) + throws LicenseContentException { + //1. 棣栧厛璋冪敤鐖剁被鐨剉alidate鏂规硶 + super.validate(content); + + //2. 鐒跺悗鏍¢獙鑷畾涔夌殑License鍙傛暟 + //License涓彲琚厑璁哥殑鍙傛暟淇℃伅 +// LicenseCheck expectedCheckModel = (LicenseCheck) content.getExtra(); + LicenseCheck expectedCheckModel = JSON.parseObject(content.getExtra().toString(), LicenseCheck.class); + //褰撳墠鏈嶅姟鍣ㄧ湡瀹炵殑鍙傛暟淇℃伅 + LicenseCheck serverCheckModel = getServerInfos(); + + if(expectedCheckModel != null && serverCheckModel != null){ + //鏍¢獙IP鍦板潃 + if(!checkIpAddress(expectedCheckModel.getIpAddress(),serverCheckModel.getIpAddress())){ + throw new LicenseContentException("褰撳墠鏈嶅姟鍣ㄧ殑IP娌″湪鎺堟潈鑼冨洿鍐�"); + } + + //鏍¢獙Mac鍦板潃 + if(!checkIpAddress(expectedCheckModel.getMacAddress(),serverCheckModel.getMacAddress())){ + throw new LicenseContentException("褰撳墠鏈嶅姟鍣ㄧ殑Mac鍦板潃娌″湪鎺堟潈鑼冨洿鍐�"); + } + + //鏍¢獙涓绘澘搴忓垪鍙� + if(!checkSerial(expectedCheckModel.getMainBoardSerial(),serverCheckModel.getMainBoardSerial())){ + throw new LicenseContentException("褰撳墠鏈嶅姟鍣ㄧ殑涓绘澘搴忓垪鍙锋病鍦ㄦ巿鏉冭寖鍥村唴"); + } + + //鏍¢獙CPU搴忓垪鍙� + if(!checkSerial(expectedCheckModel.getCpuSerial(),serverCheckModel.getCpuSerial())){ + throw new LicenseContentException("褰撳墠鏈嶅姟鍣ㄧ殑CPU搴忓垪鍙锋病鍦ㄦ巿鏉冭寖鍥村唴"); + } + }else{ + throw new LicenseContentException("涓嶈兘鑾峰彇鏈嶅姟鍣ㄧ‖浠朵俊鎭�"); + } + } + + + /** + * 閲嶅啓XMLDecoder瑙f瀽XML + */ + private Object load(String encoded){ + BufferedInputStream inputStream = null; + XMLDecoder decoder = null; + try { + inputStream = new BufferedInputStream(new ByteArrayInputStream(encoded.getBytes(XML_CHARSET))); + + decoder = new XMLDecoder(new BufferedInputStream(inputStream, DEFAULT_BUFSIZE),null,null); + + return decoder.readObject(); + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); + } finally { + try { + if(decoder != null){ + decoder.close(); + } + if(inputStream != null){ + inputStream.close(); + } + } catch (Exception e) { + logger.error("XMLDecoder瑙f瀽XML澶辫触",e); + } + } + + return null; + } + + /** + * 鑾峰彇褰撳墠鏈嶅姟鍣ㄩ渶瑕侀澶栨牎楠岀殑License鍙傛暟 + */ + private LicenseCheck getServerInfos(){ + //鎿嶄綔绯荤粺绫诲瀷 + String osName = System.getProperty("os.name").toLowerCase(); + AbstractServerInfos abstractServerInfos = null; + + //鏍规嵁涓嶅悓鎿嶄綔绯荤粺绫诲瀷閫夋嫨涓嶅悓鐨勬暟鎹幏鍙栨柟娉� + if (osName.startsWith("windows")) { + abstractServerInfos = new WindowsServerInfos(); + } else if (osName.startsWith("linux")) { + abstractServerInfos = new LinuxServerInfos(); + }else{//鍏朵粬鏈嶅姟鍣ㄧ被鍨� + abstractServerInfos = new WindowsServerInfos(); + } + + return abstractServerInfos.getServerInfos(); + } + + /** + * 鏍¢獙褰撳墠鏈嶅姟鍣ㄧ殑IP/Mac鍦板潃鏄惁鍦ㄥ彲琚厑璁哥殑IP鑼冨洿鍐�<br/> + * 濡傛灉瀛樺湪IP鍦ㄥ彲琚厑璁哥殑IP/Mac鍦板潃鑼冨洿鍐咃紝鍒欒繑鍥瀟rue + */ + private boolean checkIpAddress(List<String> expectedList,List<String> serverList){ + if(expectedList != null && expectedList.size() > 0){ + if(serverList != null && serverList.size() > 0){ + for(String expected : expectedList){ + if(serverList.contains(expected.trim())){ + return true; + } + } + } + + return false; + }else { + return true; + } + } + + /** + * 鏍¢獙褰撳墠鏈嶅姟鍣ㄧ‖浠讹紙涓绘澘銆丆PU绛夛級搴忓垪鍙锋槸鍚﹀湪鍙厑璁歌寖鍥村唴 + */ + private boolean checkSerial(String expectedSerial,String serverSerial){ + if(!Cools.isEmpty(expectedSerial)){ + if(!Cools.isEmpty(serverSerial)){ + if(expectedSerial.equals(serverSerial)){ + return true; + } + } + return false; + }else{ + return true; + } + } + } \ No newline at end of file -- Gitblit v1.9.1