使用truelicense进行Java程序license控制 经过扩张可以验证license 开始结束日期,验证绑定给定mac地址...

使用truelicense进行Java程序license控制

经过扩张可以验证license 开始结束日期,验证绑定给定mac地址。

 

Truelicense 是一个开源的Java license 验证项目。

使用truelicense实现用于JAVA工程license机制(包括license生成和验证)请参考http://www.it165.net/pro/html/201404/11540.html

其中包括license授权机制的原理和制作license的具体步骤

 

本文主要是在此文基础上增加了mac 地址验证:

在createparam.properties

中增加 ip地址和 mac 地址的配置

 

[html] view plain copy

  1. ##########common parameters###########  
  2. #alias  
  3. PRIVATEALIAS=privateKeys  
  4. #key(important!)  
  5. KEYPWD=iptv1234  
  6. #STOREPWD  
  7. STOREPWD=iptv1234  
  8. #SUBJECT  
  9. SUBJECT=bigdata  
  10. #licPath  
  11. licPath=C:/9exce/license/license.lic  
  12. #priPath  
  13. priPath=C:/9exce/license/PrivateKeys.keystore  
  14. ##########license content###########  
  15. #issuedTime  
  16. issuedTime=2015-03-09  
  17. #notBeforeTime  
  18. notBefore=2015-03-09  
  19. #notAfterTime  
  20. notAfter=2016-03-20  
  21. #ip address  
  22. ipAddress=150.236.220.200  
  23. #mac address  
  24. macAddress=80-00-0B-56-3B-32  
  25. #consumerType  
  26. consumerType=user  
  27. #ConsumerAmount  
  28. consumerAmount=1  
  29. #info  
  30. info=this is a license  


 

 

 

因为增加了mac地址,需要改变LicenseManager中的validate函数增加create_validate 用于create时的verify, 因为创建证书的时候 不能验证mac地址。

 

[java] view plain copy

  1. protected synchronized void create_validate(LicenseContent paramLicenseContent)  
  2.         throws LicenseContentException {  
  3.     LicenseParam localLicenseParam = getLicenseParam();  
  4.     if (!localLicenseParam.getSubject().equals(  
  5.             paramLicenseContent.getSubject())) {  
  6.         throw new LicenseContentException(EXC_INVALID_SUBJECT);  
  7.     }  
  8.     if (paramLicenseContent.getHolder() == null) {  
  9.         throw new LicenseContentException(EXC_HOLDER_IS_NULL);  
  10.     }  
  11.     if (paramLicenseContent.getIssuer() == null) {  
  12.         throw new LicenseContentException(EXC_ISSUER_IS_NULL);  
  13.     }  
  14.     if (paramLicenseContent.getIssued() == null) {  
  15.         throw new LicenseContentException(EXC_ISSUED_IS_NULL);  
  16.     }  
  17.     Date localDate1 = new Date();  
  18.     Date localDate2 = paramLicenseContent.getNotBefore();  
  19.     if ((localDate2 != null) && (localDate1.before(localDate2))) {  
  20.         throw new LicenseContentException(EXC_LICENSE_IS_NOT_YET_VALID);  
  21.     }  
  22.     Date localDate3 = paramLicenseContent.getNotAfter();  
  23.     if ((localDate3 != null) && (localDate1.after(localDate3))) {  
  24.         throw new LicenseContentException(EXC_LICENSE_HAS_EXPIRED);  
  25.     }     
  26.       
  27.     String str = paramLicenseContent.getConsumerType();  
  28.     if (str == null) {  
  29.         throw new LicenseContentException(EXC_CONSUMER_TYPE_IS_NULL);  
  30.     }  
  31.     Preferences localPreferences = localLicenseParam.getPreferences();  
  32.     if ((localPreferences != null) && (localPreferences.isUserNode())) {  
  33.         if (!USER.equalsIgnoreCase(str)) {  
  34.             throw new LicenseContentException(EXC_CONSUMER_TYPE_IS_NOT_USER);  
  35.         }  
  36.         if (paramLicenseContent.getConsumerAmount() != 1) {  
  37.             throw new LicenseContentException(  
  38.                     EXC_CONSUMER_AMOUNT_IS_NOT_ONE);  
  39.         }  
  40.     } else if (paramLicenseContent.getConsumerAmount() <= 0) {  
  41.         throw new LicenseContentException(  
  42.                 EXC_CONSUMER_AMOUNT_IS_NOT_POSITIVE);  
  43.     }  
  44. }  

 

 

更新validate增加验证客户server的mac地址。

 

[java] view plain copy

  1. protected synchronized void validate(LicenseContent paramLicenseContent)  
  2.             throws LicenseContentException {  
  3.         LicenseParam localLicenseParam = getLicenseParam();  
  4.         if (!localLicenseParam.getSubject().equals(  
  5.                 paramLicenseContent.getSubject())) {  
  6.             throw new LicenseContentException(EXC_INVALID_SUBJECT);  
  7.         }  
  8.         if (paramLicenseContent.getHolder() == null) {  
  9.             throw new LicenseContentException(EXC_HOLDER_IS_NULL);  
  10.         }  
  11.         if (paramLicenseContent.getIssuer() == null) {  
  12.             throw new LicenseContentException(EXC_ISSUER_IS_NULL);  
  13.         }  
  14.         if (paramLicenseContent.getIssued() == null) {  
  15.             throw new LicenseContentException(EXC_ISSUED_IS_NULL);  
  16.         }  
  17.         Date localDate1 = new Date();  
  18.         Date localDate2 = paramLicenseContent.getNotBefore();  
  19.         if ((localDate2 != null) && (localDate1.before(localDate2))) {  
  20.             throw new LicenseContentException(EXC_LICENSE_IS_NOT_YET_VALID);  
  21.         }  
  22.         Date localDate3 = paramLicenseContent.getNotAfter();  
  23.         if ((localDate3 != null) && (localDate1.after(localDate3))) {  
  24.             throw new LicenseContentException(EXC_LICENSE_HAS_EXPIRED);  
  25.         }  
  26.           
  27.         LicenseCheckModel licenseCheckModel = (LicenseCheckModel)paramLicenseContent.getExtra();  
  28.         String macAddress = licenseCheckModel.getIpMacAddress();  
  29.           
  30.         try {  
  31.             if (!ListNets.validateMacAddress(macAddress)) {  
  32.                 throw new LicenseContentException(EXC_LICENSE_HAS_EXPIRED);  
  33.             }  
  34.         } catch (SocketException e) {  
  35.             // TODO Auto-generated catch block  
  36.             throw new LicenseContentException(EXC_LICENSE_HAS_EXPIRED);  
  37.         }  
  38.           
  39.           
  40.         String str = paramLicenseContent.getConsumerType();  
  41.         if (str == null) {  
  42.             throw new LicenseContentException(EXC_CONSUMER_TYPE_IS_NULL);  
  43.         }  
  44.         Preferences localPreferences = localLicenseParam.getPreferences();  
  45.         if ((localPreferences != null) && (localPreferences.isUserNode())) {  
  46.             if (!USER.equalsIgnoreCase(str)) {  
  47.                 throw new LicenseContentException(EXC_CONSUMER_TYPE_IS_NOT_USER);  
  48.             }  
  49.             if (paramLicenseContent.getConsumerAmount() != 1) {  
  50.                 throw new LicenseContentException(  
  51.                         EXC_CONSUMER_AMOUNT_IS_NOT_ONE);  
  52.             }  
  53.         } else if (paramLicenseContent.getConsumerAmount() <= 0) {  
  54.             throw new LicenseContentException(  
  55.                     EXC_CONSUMER_AMOUNT_IS_NOT_POSITIVE);  
  56.         }  
  57.     }  


 

 

创建类ListNets 用于读取客户server的IP地址和mac地址进行验证,笔者使用了验证mac地址的函数,毕竟客户有可能更改机器的ip地址的

 

[java] view plain copy

  1. package zlicense.util;  
  2.   
  3.   
  4. import java.net.*;  
  5. import java.util.*;  
  6.   
  7. import static java.lang.System.out;  
  8.   
  9. public class ListNets {  
  10.   
  11.     public static void main(String args[]) throws SocketException {   
  12.         String ip = "150.236.220.200";  
  13.         String mac = "80-00-0B-56-3B-32";  
  14.         boolean flag = validatoIpAndMacAddress(ip, mac);  
  15.         boolean macflag = validateMacAddress( mac);  
  16.         out.printf("validatoMacAddress flag=%s\n", macflag);  
  17.         out.printf("validatoIpAndMacAddress flag=%s\n", flag);        
  18.     }  
  19.   
  20.     static void displayInterfaceInformation(NetworkInterface netint)  
  21.             throws SocketException {  
  22.         out.printf("Display name: %s\n", netint.getDisplayName());  
  23.         out.printf("Name: %s\n", netint.getName());  
  24.         byte[] mac = netint.getHardwareAddress();  
  25.         if (mac != null) {  
  26.             StringBuilder sb = new StringBuilder();  
  27.             for (int i = 0; i < mac.length; i++) {  
  28.                 sb.append(String.format("%02X%s", mac[i],  
  29.                         (i < mac.length - 1) ? "-" : ""));  
  30.             }  
  31.             System.out.println("mac=" + sb.toString());  
  32.         }  
  33.   
  34.         Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();  
  35.         for (InetAddress inetAddress : Collections.list(inetAddresses)) {  
  36.             out.printf("InetAddress: %s\n", inetAddress);  
  37.             System.out  
  38.                     .println("InetAddress ip=" + inetAddress.getHostAddress());  
  39.         }  
  40.         out.printf("\n");  
  41.     }  
  42.   
  43.     public static boolean validateMacAddress(String macAddress)  
  44.             throws SocketException {  
  45.         boolean returnFlag = false;  
  46.         Enumeration<NetworkInterface> nets = NetworkInterface  
  47.                 .getNetworkInterfaces();  
  48.         for (NetworkInterface netint : Collections.list(nets)) {  
  49.             byte[] mac = netint.getHardwareAddress();  
  50.             StringBuilder sb = new StringBuilder();  
  51.             if (mac != null) {  
  52.                 for (int i = 0; i < mac.length; i++) {  
  53.                     sb.append(String.format("%02X%s", mac[i],  
  54.                             (i < mac.length - 1) ? "-" : ""));  
  55.                 }  
  56.                 System.out.println("mac=" + sb.toString());  
  57.             }  
  58.             if (sb.toString().equals(macAddress)) {  
  59.                 returnFlag = true;  
  60.             }  
  61.         }  
  62.         return returnFlag;  
  63.   
  64.     }  
  65.   
  66.     public static boolean validatoIpAndMacAddress(String ipAddress,  
  67.             String macAddress) throws SocketException {  
  68.         boolean returnFlag = false;  
  69.         Enumeration<NetworkInterface> nets = NetworkInterface  
  70.                 .getNetworkInterfaces();  
  71.         for (NetworkInterface netint : Collections.list(nets)) {  
  72.             byte[] mac = netint.getHardwareAddress();  
  73.             StringBuilder sb = new StringBuilder();  
  74.             if (mac != null) {  
  75.                 for (int i = 0; i < mac.length; i++) {  
  76.                     sb.append(String.format("%02X%s", mac[i],  
  77.                             (i < mac.length - 1) ? "-" : ""));  
  78.                 }  
  79.                 System.out.println("mac=" + sb.toString());  
  80.             }  
  81.             if (sb.toString().equals(macAddress)) {  
  82.                 Enumeration<InetAddress> inetAddresses = netint  
  83.                         .getInetAddresses();  
  84.                 String ip = "";  
  85.                 for (InetAddress inetAddress : Collections.list(inetAddresses)) {  
  86.                     ip = inetAddress.getHostAddress();  
  87.                     System.out.println("InetAddress ip="  
  88.                             + inetAddress.getHostAddress());  
  89.                     if (ipAddress.toString().equals(ip)) {  
  90.                         returnFlag = true;  
  91.                     }  
  92.                 }  
  93.             }  
  94.         }  
  95.         return returnFlag;  
  96.   
  97.     }  
  98. }  


 

 

创建LicenseCheckModel 是一个model类就是存储 ip和mac地址

 

[java] view plain copy

  1. package zlicense.util;  
  2.   
  3. public class LicenseCheckModel {  
  4.       
  5.     private String ipAddress;      
  6.     private String ipMacAddress;  
  7.     private String CPUSerial;  
  8.     private String motherboardSN;  
  9.     private String hardDiskSN;  
  10.       
  11.       
  12.     public String getIpAddress() {  
  13.         return ipAddress;  
  14.     }  
  15.     public void setIpAddress(String ipAddress) {  
  16.         this.ipAddress = ipAddress;  
  17.     }  
  18.     public String getIpMacAddress() {  
  19.         return ipMacAddress;  
  20.     }  
  21.     public void setIpMacAddress(String ipMacAddress) {  
  22.         this.ipMacAddress = ipMacAddress;  
  23.     }  
  24.     public String getCPUSerial() {  
  25.         return CPUSerial;  
  26.     }  
  27.     public void setCPUSerial(String cPUSerial) {  
  28.         CPUSerial = cPUSerial;  
  29.     }  
  30.     public String getMotherboardSN() {  
  31.         return motherboardSN;  
  32.     }  
  33.     public void setMotherboardSN(String motherboardSN) {  
  34.         this.motherboardSN = motherboardSN;  
  35.     }  
  36.     public String getHardDiskSN() {  
  37.         return hardDiskSN;  
  38.     }  
  39.     public void setHardDiskSN(String hardDiskSN) {  
  40.         this.hardDiskSN = hardDiskSN;  
  41.     }  
  42.   
  43. }  


 

 

更新CreateLicense 增加mac和ip 的配置读取并写入license证书,采用了content.setExtra(licenseCheckModel);

将需要验证的信息 写入licenseCheckModel 然后set到content中。

 

改变了 truelicense 中所有的文件读取方式,采用绝对路径读取。

                

[java] view plain copy

  1. Properties prop= new Properties();  
  2.                 //InputStream in= getClass().getResourceAsStream(propertiesPath);  
  3.                  
  4.                 try {  
  5.                          InputStreamin = new FileInputStream(propertiesPath);  
  6.                          prop.load(in);  
  7.                 } catch(IOException e) {  
  8.                          // TODOAuto-generated catch block  
  9.                          e.printStackTrace();  
  10.                 }  

 

 

 

所有的配置文件和我生成的 key stroe 在java-license-jar/src/main/resources文件夹下

 

项目code地址:

https://github.com/jingshauizh/JavaSpringSurmmary/tree/master/java-license-jar

转载于:https://my.oschina.net/airship/blog/1527688

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值