采用truelicense进行Java规划license控制 扩展可以验证后,license 开始结束日期,验证绑定一个给定的mac住址
采用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住址的更多相关文章
- Java高并发 -- 并发扩展
Java高并发 -- 并发扩展 主要是学习慕课网实战视频<Java并发编程入门与高并发面试>的笔记 死锁 死锁是指两个或两个以上的事务在执行过程中,因争夺锁资源而造成的一种互相等待的现象, ...
- Sentinel基本使用--基于QPS流量控制(二), 采用Warm Up预热/冷启动方式控制突增流量
Sentinel基本使用--基于QPS流量控制(二), 采用Warm Up预热/冷启动方式控制突增流量 2019年02月18日 23:52:37 xiongxianze 阅读数 398更多 分类专栏: ...
- Java的流程控制和C++的异同
Java的流程控制和C++基本相似 现将不同的地方总结一下,以便快速掌握. Java的特殊流程控制的特殊部分: 1.顺序结构 -- 没有区别 2.分之结构 -- 没有区别 3.循环结构 1> ...
- 【Java规划】DOM XML Parser分解、遍历、创XML
1.前言 DOM XML Parser简介 DOM 它是 W3C 处理 XML 规范 API,这是很多其他的 XML 地基处理相关标准,不仅是 Java,其他的,如 Javascript,PHP.MS ...
- 使用XML布局文件和Java代码混合控制UI界面
完全使用Java代码来控制UI界面不仅烦琐.而且不利于解耦:而完全利用XML布局文件来控制UI界面虽然方便.便捷,但难免有失灵活.因此有些时候,可能需要混合使用XML布局文件和代码来控制UI界面. 当 ...
- Java安全套接字扩展——JSSE
上节已经介绍了SSL/TLS协议的通信模式,而对于这些底层协议,如果要每个开发者都自己去实现显然会带来不必要的麻烦,正是为了解决这个问题Java为广大开发者提供了Java安全套接字扩展--JSSE,它 ...
- 论Java访问权限控制的重要性
人在什么面前最容易失去抵抗力? 美色,算是一个,比如说西施的贡献薄就是忍辱负重.以身报国.助越灭吴:金钱,算是另外一个,我们古人常说“钱乃身外之物,生不带来死不带去”,但我们又都知道“有钱能使鬼推磨” ...
- java zookeeper权限控制ACL(digest,IP)
java zookeeper权限控制ACL(auth,digest,ip) 学习前请参考:https://www.cnblogs.com/zwcry/p/10407806.html zookeeper ...
- License控制实现原理(20140808)
近期须要做一个License控制的实现,做了一个设计,设计图例如以下: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGVjX2Zlbmc=/font/5 ...
随机推荐
- select下拉菜单反显不可改动,且submit能够提交数据
首先通过后台funcA()将下拉菜单反显不可改动的数据response到disable.jsp页面,disable.jsp: <script> var data1=${result.obj ...
- 使用CSVDE批量导入命令/出口AD用户
使用CSVDE批量导入命令/出口AD用户 CSVDE命令行工具可以与真实的用户信息CSV文件.批量导入/出口AD在. 导入的基本的语法命令是这种: csvde -i -f c:\fil ...
- 绝杀600元以下智能手机的夏新小V二代-专栏-速途网
绝杀600元以下智能手机的夏新小V二代-专栏-速途网 绝杀600元以下智能手机的夏新小V二代
- c#操作.mpp文件
原文地址:http://mjm13.iteye.com/blog/532404 所需设置 在工程中增加引用Microsoft Project 11.0 Object Library,该引用在co ...
- hdu1540(线段树)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1540 题意:是一条线上的点,D x是破坏这个点,Q x是表示查询以x所在的最长的连续的点的个数,R是恢 ...
- C++封装SQLite实例<三>
前一篇博客中介绍的是怎样依据sqlite3_get_table()函数来获取一张表的内容,就是一股脑的把表中的内容所有存储起来放在一个一维数组中,这其中的规则已经介绍过了.接下来讲的是怎样依据一个SQ ...
- iOS_25彩票_幸运转盘
终于效果图: 各个view的关系图: 背景圆盘(须要扣图处理)LuckyBaseBackground.png 盖在背景圆盘上面的转盘 LuckyRotateWheel.png watermark/2/ ...
- Android 一些错误
android fragment里面放viewpager 嵌套fragment 报错: 解决:在adapter的构造方法里加上 super(fragment.getChildFragmentManag ...
- C++ Primer 学习笔记_62_重载操作符与转换 --调用操作符和函数对象
重载操作符与转换 --调用操作符和函数对象 引言: 能够为类类型的对象重载函数调用操作符:一般为表示操作的类重载调用操作符! struct absInt { int operator() (int v ...
- maven 项目中使用 jstl标签
在pom.xml文件下面增加如下的依赖包: <dependency> <groupId>javax.servlet</groupId> <artifactId ...