采用truelicense进行Java规划license控制

扩展可以验证后,license 开始结束日期,验证绑定一个给定的mac住址。

Truelicense 它是一个开源java license 检验项目。

使用truelicense实现用于JAVAprojectlicense机制(包含license生成和验证)请參考http://www.it165.net/pro/html/201404/11540.html

当中包含license授权机制的原理和制作license的详细步骤

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

在createparam.properties

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

##########common parameters###########
#alias
PRIVATEALIAS=privateKeys
#key(important!)
KEYPWD=iptv1234
#STOREPWD
STOREPWD=iptv1234
#SUBJECT
SUBJECT=bigdata
#licPath
licPath=C:/9exce/license/license.lic
#priPath
priPath=C:/9exce/license/PrivateKeys.keystore
##########license content###########
#issuedTime
issuedTime=2015-03-09
#notBeforeTime
notBefore=2015-03-09
#notAfterTime
notAfter=2016-03-20
#ip address
ipAddress=150.236.220.200
#mac address
macAddress=80-00-0B-56-3B-32
#consumerType
consumerType=user
#ConsumerAmount
consumerAmount=1
#info
info=this is a license

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

	protected synchronized void create_validate(LicenseContent paramLicenseContent)
throws LicenseContentException {
LicenseParam localLicenseParam = getLicenseParam();
if (!localLicenseParam.getSubject().equals(
paramLicenseContent.getSubject())) {
throw new LicenseContentException(EXC_INVALID_SUBJECT);
}
if (paramLicenseContent.getHolder() == null) {
throw new LicenseContentException(EXC_HOLDER_IS_NULL);
}
if (paramLicenseContent.getIssuer() == null) {
throw new LicenseContentException(EXC_ISSUER_IS_NULL);
}
if (paramLicenseContent.getIssued() == null) {
throw new LicenseContentException(EXC_ISSUED_IS_NULL);
}
Date localDate1 = new Date();
Date localDate2 = paramLicenseContent.getNotBefore();
if ((localDate2 != null) && (localDate1.before(localDate2))) {
throw new LicenseContentException(EXC_LICENSE_IS_NOT_YET_VALID);
}
Date localDate3 = paramLicenseContent.getNotAfter();
if ((localDate3 != null) && (localDate1.after(localDate3))) {
throw new LicenseContentException(EXC_LICENSE_HAS_EXPIRED);
} String str = paramLicenseContent.getConsumerType();
if (str == null) {
throw new LicenseContentException(EXC_CONSUMER_TYPE_IS_NULL);
}
Preferences localPreferences = localLicenseParam.getPreferences();
if ((localPreferences != null) && (localPreferences.isUserNode())) {
if (!USER.equalsIgnoreCase(str)) {
throw new LicenseContentException(EXC_CONSUMER_TYPE_IS_NOT_USER);
}
if (paramLicenseContent.getConsumerAmount() != 1) {
throw new LicenseContentException(
EXC_CONSUMER_AMOUNT_IS_NOT_ONE);
}
} else if (paramLicenseContent.getConsumerAmount() <= 0) {
throw new LicenseContentException(
EXC_CONSUMER_AMOUNT_IS_NOT_POSITIVE);
}
}

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

protected synchronized void validate(LicenseContent paramLicenseContent)
throws LicenseContentException {
LicenseParam localLicenseParam = getLicenseParam();
if (!localLicenseParam.getSubject().equals(
paramLicenseContent.getSubject())) {
throw new LicenseContentException(EXC_INVALID_SUBJECT);
}
if (paramLicenseContent.getHolder() == null) {
throw new LicenseContentException(EXC_HOLDER_IS_NULL);
}
if (paramLicenseContent.getIssuer() == null) {
throw new LicenseContentException(EXC_ISSUER_IS_NULL);
}
if (paramLicenseContent.getIssued() == null) {
throw new LicenseContentException(EXC_ISSUED_IS_NULL);
}
Date localDate1 = new Date();
Date localDate2 = paramLicenseContent.getNotBefore();
if ((localDate2 != null) && (localDate1.before(localDate2))) {
throw new LicenseContentException(EXC_LICENSE_IS_NOT_YET_VALID);
}
Date localDate3 = paramLicenseContent.getNotAfter();
if ((localDate3 != null) && (localDate1.after(localDate3))) {
throw new LicenseContentException(EXC_LICENSE_HAS_EXPIRED);
} LicenseCheckModel licenseCheckModel = (LicenseCheckModel)paramLicenseContent.getExtra();
String macAddress = licenseCheckModel.getIpMacAddress(); try {
if (!ListNets.validateMacAddress(macAddress)) {
throw new LicenseContentException(EXC_LICENSE_HAS_EXPIRED);
}
} catch (SocketException e) {
// TODO Auto-generated catch block
throw new LicenseContentException(EXC_LICENSE_HAS_EXPIRED);
} String str = paramLicenseContent.getConsumerType();
if (str == null) {
throw new LicenseContentException(EXC_CONSUMER_TYPE_IS_NULL);
}
Preferences localPreferences = localLicenseParam.getPreferences();
if ((localPreferences != null) && (localPreferences.isUserNode())) {
if (!USER.equalsIgnoreCase(str)) {
throw new LicenseContentException(EXC_CONSUMER_TYPE_IS_NOT_USER);
}
if (paramLicenseContent.getConsumerAmount() != 1) {
throw new LicenseContentException(
EXC_CONSUMER_AMOUNT_IS_NOT_ONE);
}
} else if (paramLicenseContent.getConsumerAmount() <= 0) {
throw new LicenseContentException(
EXC_CONSUMER_AMOUNT_IS_NOT_POSITIVE);
}
}

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

package zlicense.util;

import java.net.*;
import java.util.*; import static java.lang.System.out; public class ListNets { public static void main(String args[]) throws SocketException {
String ip = "150.236.220.200";
String mac = "80-00-0B-56-3B-32";
boolean flag = validatoIpAndMacAddress(ip, mac);
boolean macflag = validateMacAddress( mac);
out.printf("validatoMacAddress flag=%s\n", macflag);
out.printf("validatoIpAndMacAddress flag=%s\n", flag);
} static void displayInterfaceInformation(NetworkInterface netint)
throws SocketException {
out.printf("Display name: %s\n", netint.getDisplayName());
out.printf("Name: %s\n", netint.getName());
byte[] mac = netint.getHardwareAddress();
if (mac != null) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i],
(i < mac.length - 1) ? "-" : ""));
}
System.out.println("mac=" + sb.toString());
} Enumeration<InetAddress> inetAddresses = netint.getInetAddresses();
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
out.printf("InetAddress: %s\n", inetAddress);
System.out
.println("InetAddress ip=" + inetAddress.getHostAddress());
}
out.printf("\n");
} public static boolean validateMacAddress(String macAddress)
throws SocketException {
boolean returnFlag = false;
Enumeration<NetworkInterface> nets = NetworkInterface
.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets)) {
byte[] mac = netint.getHardwareAddress();
StringBuilder sb = new StringBuilder();
if (mac != null) {
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i],
(i < mac.length - 1) ? "-" : ""));
}
System.out.println("mac=" + sb.toString());
}
if (sb.toString().equals(macAddress)) {
returnFlag = true;
}
}
return returnFlag; } public static boolean validatoIpAndMacAddress(String ipAddress,
String macAddress) throws SocketException {
boolean returnFlag = false;
Enumeration<NetworkInterface> nets = NetworkInterface
.getNetworkInterfaces();
for (NetworkInterface netint : Collections.list(nets)) {
byte[] mac = netint.getHardwareAddress();
StringBuilder sb = new StringBuilder();
if (mac != null) {
for (int i = 0; i < mac.length; i++) {
sb.append(String.format("%02X%s", mac[i],
(i < mac.length - 1) ? "-" : ""));
}
System.out.println("mac=" + sb.toString());
}
if (sb.toString().equals(macAddress)) {
Enumeration<InetAddress> inetAddresses = netint
.getInetAddresses();
String ip = "";
for (InetAddress inetAddress : Collections.list(inetAddresses)) {
ip = inetAddress.getHostAddress();
System.out.println("InetAddress ip="
+ inetAddress.getHostAddress());
if (ipAddress.toString().equals(ip)) {
returnFlag = true;
}
}
}
}
return returnFlag; }
}

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

package zlicense.util;

public class LicenseCheckModel {

	private String ipAddress;
private String ipMacAddress;
private String CPUSerial;
private String motherboardSN;
private String hardDiskSN; public String getIpAddress() {
return ipAddress;
}
public void setIpAddress(String ipAddress) {
this.ipAddress = ipAddress;
}
public String getIpMacAddress() {
return ipMacAddress;
}
public void setIpMacAddress(String ipMacAddress) {
this.ipMacAddress = ipMacAddress;
}
public String getCPUSerial() {
return CPUSerial;
}
public void setCPUSerial(String cPUSerial) {
CPUSerial = cPUSerial;
}
public String getMotherboardSN() {
return motherboardSN;
}
public void setMotherboardSN(String motherboardSN) {
this.motherboardSN = motherboardSN;
}
public String getHardDiskSN() {
return hardDiskSN;
}
public void setHardDiskSN(String hardDiskSN) {
this.hardDiskSN = hardDiskSN;
} }

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

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

改变了 truelicense 中全部的文件读取方式。採用绝对路径读取。

   Properties prop= new Properties();
//InputStream in= getClass().getResourceAsStream(propertiesPath); try {
InputStreamin = new FileInputStream(propertiesPath);
prop.load(in);
} catch(IOException e) {
// TODOAuto-generated catch block
e.printStackTrace();
}

全部的配置文件和我生成的 key stroe 在java-license-jar/src/main/resources目录下

项目code地址:

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

版权声明:本文博主原创文章,博客,未经同意不得转载。

采用truelicense进行Java规划license控制 扩展可以验证后,license 开始结束日期,验证绑定一个给定的mac住址的更多相关文章

  1. Java高并发 -- 并发扩展

    Java高并发 -- 并发扩展 主要是学习慕课网实战视频<Java并发编程入门与高并发面试>的笔记 死锁 死锁是指两个或两个以上的事务在执行过程中,因争夺锁资源而造成的一种互相等待的现象, ...

  2. Sentinel基本使用--基于QPS流量控制(二), 采用Warm Up预热/冷启动方式控制突增流量

    Sentinel基本使用--基于QPS流量控制(二), 采用Warm Up预热/冷启动方式控制突增流量 2019年02月18日 23:52:37 xiongxianze 阅读数 398更多 分类专栏: ...

  3. Java的流程控制和C++的异同

    Java的流程控制和C++基本相似 现将不同的地方总结一下,以便快速掌握. Java的特殊流程控制的特殊部分: 1.顺序结构  -- 没有区别 2.分之结构  -- 没有区别 3.循环结构 1> ...

  4. 【Java规划】DOM XML Parser分解、遍历、创XML

    1.前言 DOM XML Parser简介 DOM 它是 W3C 处理 XML 规范 API,这是很多其他的 XML 地基处理相关标准,不仅是 Java,其他的,如 Javascript,PHP.MS ...

  5. 使用XML布局文件和Java代码混合控制UI界面

    完全使用Java代码来控制UI界面不仅烦琐.而且不利于解耦:而完全利用XML布局文件来控制UI界面虽然方便.便捷,但难免有失灵活.因此有些时候,可能需要混合使用XML布局文件和代码来控制UI界面. 当 ...

  6. Java安全套接字扩展——JSSE

    上节已经介绍了SSL/TLS协议的通信模式,而对于这些底层协议,如果要每个开发者都自己去实现显然会带来不必要的麻烦,正是为了解决这个问题Java为广大开发者提供了Java安全套接字扩展--JSSE,它 ...

  7. 论Java访问权限控制的重要性

    人在什么面前最容易失去抵抗力? 美色,算是一个,比如说西施的贡献薄就是忍辱负重.以身报国.助越灭吴:金钱,算是另外一个,我们古人常说“钱乃身外之物,生不带来死不带去”,但我们又都知道“有钱能使鬼推磨” ...

  8. java zookeeper权限控制ACL(digest,IP)

    java zookeeper权限控制ACL(auth,digest,ip) 学习前请参考:https://www.cnblogs.com/zwcry/p/10407806.html zookeeper ...

  9. License控制实现原理(20140808)

    近期须要做一个License控制的实现,做了一个设计,设计图例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGVjX2Zlbmc=/font/5 ...

随机推荐

  1. OCP读书笔记(6) - 手动恢复操作

    6.Restore and Recovery Task 非关键性文件丢失的恢复 临时文件丢失的恢复 临时表空间文件丢失的恢复: 查看数据库中的临时文件: SQL> select file#,ST ...

  2. MATLAB——scatter的简单应用

    scatter可用于描绘散点图. 1.scatter(X,Y) X和Y是数据向量,以X中数据为横坐标,以Y中数据位纵坐标描绘散点图,点的形状默认使用圈. 样例: X = [1:10];  Y = X ...

  3. Bean-Query 一个把对象转换为Map的Java工具库

    刚开源了一个经过完整測试的Java工具类. 地址例如以下: https://github.com/Jimmy-Shi/bean-query 使用说明例如以下: Bean-query Click Her ...

  4. SRM 638 Div2

    2333... 因为TC过少的参与者.加上不断fst 我掉了div2该. 幸运的是完成的背div1该.. 250 水的问题 500 水的问题.. 直接bfs扩展即可了 注意判重.  我还用康托展开了真 ...

  5. linux LVS DR模式配置

    拓扑图: 测试环境:CentOS 6.5 X86 64位 配置步骤: 1.  安装测试环境 [root@UCS-1 ~]# yum -y install httpd [root@UCS-1 ~]# c ...

  6. hdu5172(线段树)

    传送门:GTY's gay friends 题意:判断区间[l,r]内的数字是否符合1~len(r-l+1)的一个全排列. 分析:pos[i]记录数字i出现的最大位置,pre[i]记录在位置i的数字a ...

  7. Libevent使用样例,从简单到复杂

            转载请注明出处:http://blog.csdn.net/luotuo44/article/details/39670221 本文从简单到复杂.展示怎样使用libevent.网上的很多 ...

  8. Qt笔记——MOC(莫克)

    moc 代表 Meta-Object Compiler,"元对象编译器".Qt 程序在交由标准编译器编译之前,先要使用 moc 分析 C++ 源文件. 假设它发如今一个头文件里包括 ...

  9. 收集经常使用的.net开源项目

    Json.NET http://json.codeplex.com/ Json.Net是一个读写Json效率比較高的.Net框架.Json.Net 使得在.Net环境下使用Json更加简单.通过Lin ...

  10. 4句代码读取Excel到DataSet(非Excel组件)

    Toxy是继NPOI之后主推的还有一个项目,主要目的是为了解决文档的抽取问题.其支持的格式包括全部docx.xlsx.xls.csv.vcard等. 以下是一个简单但非常实用的样例 ParserCon ...