|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 自定义LicenseManager,用于增加额外的服务器硬件信息校验 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public class CustomLicenseManager extends LicenseManager{ | 
|---|
|  |  |  | public class CustomLicenseManager extends LicenseManager { | 
|---|
|  |  |  | private static Logger logger = LogManager.getLogger(CustomLicenseManager.class); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //XML编码 | 
|---|
|  |  |  | 
|---|
|  |  |  | final GenericCertificate certificate = getPrivacyGuard().key2cert(key); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | notary.verify(certificate); | 
|---|
|  |  |  | final LicenseContent content = (LicenseContent)this.load(certificate.getEncoded()); | 
|---|
|  |  |  | final LicenseContent content = (LicenseContent) this.load(certificate.getEncoded()); | 
|---|
|  |  |  | this.validate(content); | 
|---|
|  |  |  | setLicenseKey(key); | 
|---|
|  |  |  | setCertificate(certificate); | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // Load license key from preferences, | 
|---|
|  |  |  | final byte[] key = getLicenseKey(); | 
|---|
|  |  |  | if (null == key){ | 
|---|
|  |  |  | if (null == key) { | 
|---|
|  |  |  | throw new NoLicenseInstalledException(getLicenseParam().getSubject()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | certificate = getPrivacyGuard().key2cert(key); | 
|---|
|  |  |  | notary.verify(certificate); | 
|---|
|  |  |  | final LicenseContent content = (LicenseContent)this.load(certificate.getEncoded()); | 
|---|
|  |  |  | final LicenseContent content = (LicenseContent) this.load(certificate.getEncoded()); | 
|---|
|  |  |  | this.validate(content); | 
|---|
|  |  |  | setCertificate(certificate); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | final Date now = new Date(); | 
|---|
|  |  |  | final Date notBefore = content.getNotBefore(); | 
|---|
|  |  |  | final Date notAfter = content.getNotAfter(); | 
|---|
|  |  |  | if (null != notAfter && now.after(notAfter)){ | 
|---|
|  |  |  | if (null != notAfter && now.after(notAfter)) { | 
|---|
|  |  |  | throw new LicenseContentException("证书失效时间不能早于当前时间"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if (null != notBefore && null != notAfter && notAfter.before(notBefore)){ | 
|---|
|  |  |  | if (null != notBefore && null != notAfter && notAfter.before(notBefore)) { | 
|---|
|  |  |  | throw new LicenseContentException("证书生效时间不能晚于证书失效时间"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | final String consumerType = content.getConsumerType(); | 
|---|
|  |  |  | if (null == consumerType){ | 
|---|
|  |  |  | if (null == consumerType) { | 
|---|
|  |  |  | throw new LicenseContentException("用户类型不能为空"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | //当前服务器真实的参数信息 | 
|---|
|  |  |  | LicenseCheck serverCheckModel = getServerInfos(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | if(expectedCheckModel != null && serverCheckModel != null){ | 
|---|
|  |  |  | if (expectedCheckModel != null && serverCheckModel != null) { | 
|---|
|  |  |  | //校验IP地址 | 
|---|
|  |  |  | if(!checkIpAddress(expectedCheckModel.getIpAddress(),serverCheckModel.getIpAddress())){ | 
|---|
|  |  |  | if (!checkIpAddress(expectedCheckModel.getIpAddress(), serverCheckModel.getIpAddress())) { | 
|---|
|  |  |  | throw new LicenseContentException("当前服务器的IP没在授权范围内"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //校验Mac地址 | 
|---|
|  |  |  | if(!checkIpAddress(expectedCheckModel.getMacAddress(),serverCheckModel.getMacAddress())){ | 
|---|
|  |  |  | if (!checkIpAddress(expectedCheckModel.getMacAddress(), serverCheckModel.getMacAddress())) { | 
|---|
|  |  |  | throw new LicenseContentException("当前服务器的Mac地址没在授权范围内"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //校验主板序列号 | 
|---|
|  |  |  | if(!checkSerial(expectedCheckModel.getMainBoardSerial(),serverCheckModel.getMainBoardSerial())){ | 
|---|
|  |  |  | if (!checkSerial(expectedCheckModel.getMainBoardSerial(), serverCheckModel.getMainBoardSerial())) { | 
|---|
|  |  |  | throw new LicenseContentException("当前服务器的主板序列号没在授权范围内"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | //校验CPU序列号 | 
|---|
|  |  |  | if(!checkSerial(expectedCheckModel.getCpuSerial(),serverCheckModel.getCpuSerial())){ | 
|---|
|  |  |  | if (!checkSerial(expectedCheckModel.getCpuSerial(), serverCheckModel.getCpuSerial())) { | 
|---|
|  |  |  | throw new LicenseContentException("当前服务器的CPU序列号没在授权范围内"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | throw new LicenseContentException("不能获取服务器硬件信息"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 重写XMLDecoder解析XML | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private Object load(String encoded){ | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | decoder = new XMLDecoder(new BufferedInputStream(inputStream, DEFAULT_BUFSIZE), null, null); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return decoder.readObject(); | 
|---|
|  |  |  | } catch (UnsupportedEncodingException e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | } finally { | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | if(decoder != null){ | 
|---|
|  |  |  | if (decoder != null) { | 
|---|
|  |  |  | decoder.close(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(inputStream != null){ | 
|---|
|  |  |  | if (inputStream != null) { | 
|---|
|  |  |  | inputStream.close(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } catch (Exception e) { | 
|---|
|  |  |  | logger.error("XMLDecoder解析XML失败",e); | 
|---|
|  |  |  | logger.error("XMLDecoder解析XML失败", e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 获取当前服务器需要额外校验的License参数 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private LicenseCheck getServerInfos(){ | 
|---|
|  |  |  | private LicenseCheck getServerInfos() { | 
|---|
|  |  |  | //操作系统类型 | 
|---|
|  |  |  | String osName = System.getProperty("os.name").toLowerCase(); | 
|---|
|  |  |  | AbstractServerInfos abstractServerInfos = null; | 
|---|
|  |  |  | 
|---|
|  |  |  | abstractServerInfos = new WindowsServerInfos(); | 
|---|
|  |  |  | } else if (osName.startsWith("linux")) { | 
|---|
|  |  |  | abstractServerInfos = new LinuxServerInfos(); | 
|---|
|  |  |  | }else{//其他服务器类型 | 
|---|
|  |  |  | } else {//其他服务器类型 | 
|---|
|  |  |  | abstractServerInfos = new WindowsServerInfos(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | * 校验当前服务器的IP/Mac地址是否在可被允许的IP范围内<br/> | 
|---|
|  |  |  | * 如果存在IP在可被允许的IP/Mac地址范围内,则返回true | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | 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())){ | 
|---|
|  |  |  | 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 { | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 校验当前服务器硬件(主板、CPU等)序列号是否在可允许范围内 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | private boolean checkSerial(String expectedSerial,String serverSerial){ | 
|---|
|  |  |  | if(!Cools.isEmpty(expectedSerial)){ | 
|---|
|  |  |  | if(!Cools.isEmpty(serverSerial)){ | 
|---|
|  |  |  | if(expectedSerial.equals(serverSerial)){ | 
|---|
|  |  |  | private boolean checkSerial(String expectedSerial, String serverSerial) { | 
|---|
|  |  |  | if (!Cools.isEmpty(expectedSerial)) { | 
|---|
|  |  |  | if (!Cools.isEmpty(serverSerial)) { | 
|---|
|  |  |  | if (expectedSerial.equals(serverSerial)) { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | } else { | 
|---|
|  |  |  | return true; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|