采用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 ...
随机推荐
- PHP中如何实现 “在页面中一边执行一边输出” 的效果
<?php set_time_limit(0); //在有关数据库的大量数据的时候,可以将其设置为0,表示无限制. ob_end_clean(); //在循环输出前,要关闭 ...
- OCP读书笔记(16) - 管理资源
使用者组 创建资源用户组OLTP_GRP,将用户HR,OE加入此组: BEGIN dbms_resource_manager.clear_pending_area(); dbms_resource_m ...
- hdu 4291 A Short problem(矩阵+取模循环节)
A Short problem Time Limit: 2000/1000 MS (J ...
- VSTO之旅系列(三):自定义Excel UI
原文:VSTO之旅系列(三):自定义Excel UI 本专题概要 引言 自定义任务窗体(Task Pane) 自定义选项卡,即Ribbon 自定义上下文菜单 小结 引言 在上一个专题中为大家介绍如何创 ...
- iOS 同步GET
(注意: 能够整片复制)
- zoj3471(状压dp)
题目连接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4257 题意:不超过10种气体,两两之间相互碰撞可以产生一定的能量,如 ...
- JavaScript中的对象(一)
Email:longsu2010 at yeah dot net 最近我和朋友谈起JavaScript中对象的问题.朋友以写JavaScript为生,而且生活的很好,然而我发现他并不真正懂这们语言的某 ...
- Codeforces Round #254 (Div. 1)-A,B
A:选取两点一边就能够了,非常明显能够想出来... 可是一開始看错题了,sad.... #include<stdio.h> #include<string.h> #includ ...
- 泥鳅般的const(一个小Demo彻底搞清楚)
#include<stdio.h> int main(){ int a = 3; int b = 5; /* C标准库函数中最常见格式, 目的是保护參数, ...
- [IOS]UIWebView实现保存页面和读取服务器端json数据
如何通过viewView保存访问过的页面?和如何获取并解析服务器端发送过来的json数据?通过一个简单的Demo来学习一下吧! 操作步骤: 1.创建SingleViewApplication应用,新建 ...