采用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. loj1370(欧拉函数+线段树)

    传送门:Bi-shoe and Phi-shoe 题意:给出多个n(1<=n<=1e6),求满足phi(x)>=n的最小的x之和. 分析:先预处理出1~1e6的欧拉函数,然后建立一颗 ...

  2. Centos 6安装完美搭建mysql、php、apache之旅

    安装apache [root@centos share]# yum -y install httpd Loaded plugins: fastestmirror, refresh-packagekit ...

  3. ARM裸编程系列---UART

    S5PV210 UART说明 通用异步收发器缩写UART,这是UNIVERSAL ASYNCHRONOUS RECEIVER AND TRANSMITTER.它被用来传送串行数据.当发送数据,CPU将 ...

  4. 网络编程——XML与JSON

    概述 如上篇博客点击打开链接介绍.互联网公司发布Web服务,于是开发者们就可以将其用到自己的应用中.Web服务的工作方式类似于浏览器,输入请求,按下回车,等待远程计算机响应.当响应返回时,浏览器会使用 ...

  5. WPF界面设计技巧(4)—自定义列表项样式

    原文:WPF界面设计技巧(4)-自定义列表项样式 有前面修改按钮样式的基础,我们可以尝试来定制一个即好看又好用的 ListBox ,今天先来讲“好看”部分. 打开 Microsoft Visual S ...

  6. python手记(52)

    python将信息加密进图片 从图片中解密信息 >>> runfile(r'K:\testpro\test1.py', wdir=r'K:\testpro') http://blog ...

  7. c++ 按行读取txt文本

    CStdioFile 类的声明保存在 afx.h 头文件中. CStdioFile 类继承自 CFile 类, CStdioFile 对象表示一个用运行时的函数 fopen 打开的 c 运行时的流式文 ...

  8. 存读Blob Oracle

  9. JDBC与反射

    什么是JDBC Java定义了一套关于连接使用数据库的规范(接口)叫做JDBC,许多数据库厂商实现了这个规范,所以我们可以通过Java提供的接口编程,使得我们更换数据库的时候不用修改原来的代码,只需要 ...

  10. 【ArcGIS 10.2新特性】ArcGIS 10.2 for Desktop 新特性(一)

    ArcGIS 10.2 for Desktop是在10.1的成功基础上进行的改进,它的改进包括:性能提升.附加的安全性.40多个新的分析工具.3D功能提高.栅格增强.新的地理数据管理能力以及其它更多的 ...