受朋友所托,需要给产品加上License验证功能,进行试用期授权,在试用期过后,产品不再可用。

通过研究调查,可以利用Truelicense开源框架实现,下面分享一下如何利用Truelicense实现授权验证功能。

在此之前先介绍一下License授权和验证的原理:

1、  首先需要生成密钥对,方法有很多,JDK中提供的KeyTool即可生成。

2、  授权者保留私钥,使用私钥对包含授权信息(如截止日期,MAC地址等)的license进行数字签名。

3、  公钥交给使用者(放在验证的代码中使用),用于验证license是否符合使用条件。

实现步骤(代码参考前贤网上案例实现,不再赘写):

一、使用KeyTool生成密钥对

转到CMD命令行,切换到%JAVA_HOME%\jre\bin\security\ 目录(KeyTool工具一般在此目录),执行命令生成的密钥对:

1、首先利用KeyTool工具来生成私匙库:(-alias别名 –validity 3650表示10年有效)

keytool -genkey -alias privatekey -keystoreprivateKeys.store -validity 3650

2、然后把私匙库内的公匙导出到一个文件当中:

keytool -export -alias privatekey -file certfile.cer -keystore privateKeys.store

3、然后再把这个证书文件导入到公匙库:

keytool -import -alias publiccert -file certfile.cer -keystore publicCerts.store

最后生成文件privateKeys.store、publicCerts.store拷贝出来备用。

二、生成证书(该部分代码由授权者独立保管执行)

1、  首先是 LicenseManagerHolder.java 类

packagecn.melina.license;importde.schlichtherle.license.LicenseManager;importde.schlichtherle.license.LicenseParam;/*** LicenseManagerHolder

*@authormelina*/

public classLicenseManagerHolder {private staticLicenseManager licenseManager;public static synchronizedLicenseManager getLicenseManager(LicenseParam licenseParams) {if (licenseManager == null) {

licenseManager= newLicenseManager(licenseParams);

}returnlicenseManager;

}

}

2、  然后是主要生成 license 的代码 CreateLicense.java

packagecn.melina.license;importjava.io.File;importjava.io.IOException;importjava.io.InputStream;importjava.text.DateFormat;importjava.text.ParseException;importjava.text.SimpleDateFormat;importjava.util.Properties;importjava.util.prefs.Preferences;importjavax.security.auth.x500.X500Principal;importde.schlichtherle.license.CipherParam;importde.schlichtherle.license.DefaultCipherParam;importde.schlichtherle.license.DefaultKeyStoreParam;importde.schlichtherle.license.DefaultLicenseParam;importde.schlichtherle.license.KeyStoreParam;importde.schlichtherle.license.LicenseContent;importde.schlichtherle.license.LicenseParam;importde.schlichtherle.license.LicenseManager;/*** CreateLicense

*@authormelina*/

public classCreateLicense {//common param

private static String PRIVATEALIAS = "";private static String KEYPWD = "";private static String STOREPWD = "";private static String SUBJECT = "";private static String licPath = "";private static String priPath = "";//license content

private static String issuedTime = "";private static String notBefore = "";private static String notAfter = "";private static String consumerType = "";private static int consumerAmount = 0;private static String info = "";//为了方便直接用的API里的例子//X500Princal是一个证书文件的固有格式,详见API

private final static X500Principal DEFAULTHOLDERANDISSUER = newX500Principal("CN=Duke、OU=JavaSoft、O=Sun Microsystems、C=US");public voidsetParam(String propertiesPath) {//获取参数

Properties prop = newProperties();

InputStream in=getClass().getResourceAsStream(propertiesPath);try{

prop.load(in);

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

PRIVATEALIAS= prop.getProperty("PRIVATEALIAS");

KEYPWD= prop.getProperty("KEYPWD");

STOREPWD= prop.getProperty("STOREPWD");

SUBJECT= prop.getProperty("SUBJECT");

KEYPWD= prop.getProperty("KEYPWD");

licPath= prop.getProperty("licPath");

priPath= prop.getProperty("priPath");//license content

issuedTime = prop.getProperty("issuedTime");

notBefore= prop.getProperty("notBefore");

notAfter= prop.getProperty("notAfter");

consumerType= prop.getProperty("consumerType");

consumerAmount= Integer.valueOf(prop.getProperty("consumerAmount"));

info= prop.getProperty("info");

}public booleancreate() {try{/************** 证书发布者端执行 ******************/LicenseManager licenseManager=LicenseManagerHolder

.getLicenseManager(initLicenseParams0());

licenseManager.store((createLicenseContent()),newFile(licPath));

}catch(Exception e) {

e.printStackTrace();

System.out.println("客户端证书生成失败!");return false;

}

System.out.println("服务器端生成证书成功!");return true;

}//返回生成证书时需要的参数

private staticLicenseParam initLicenseParams0() {

Preferences preference=Preferences

.userNodeForPackage(CreateLicense.class);//设置对证书内容加密的对称密码

CipherParam cipherParam = newDefaultCipherParam(STOREPWD);//参数1,2从哪个Class.getResource()获得密钥库;参数3密钥库的别名;参数4密钥库存储密码;参数5密钥库密码

KeyStoreParam privateStoreParam = newDefaultKeyStoreParam(

CreateLicense.class, priPath, PRIVATEALIAS, STOREPWD, KEYPWD);

LicenseParam licenseParams= newDefaultLicenseParam(SUBJECT,

preference, privateStoreParam, cipherParam);returnlicenseParams;

}//从外部表单拿到证书的内容

public final staticLicenseContent createLicenseContent() {

DateFormat format= new SimpleDateFormat("yyyy-MM-dd");

LicenseContent content= null;

content= newLicenseContent();

content.setSubject(SUBJECT);

content.setHolder(DEFAULTHOLDERANDISSUER);

content.setIssuer(DEFAULTHOLDERANDISSUER);try{

content.setIssued(format.parse(issuedTime));

content.setNotBefore(format.parse(notBefore));

content.setNotAfter(format.parse(notAfter));

}catch(ParseException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

content.setConsumerType(consumerType);

content.setConsumerAmount(consumerAmount);

content.setInfo(info);//扩展

content.setExtra(newObject());returncontent;

}

}

3、  测试程序 licenseCreateTest.java

packagecn.melina.license;importcn.melina.license.CreateLicense;public classlicenseCreateTest {public static voidmain(String[] args){

CreateLicense cLicense= newCreateLicense();//获取参数

cLicense.setParam("./param.properties");//生成证书

cLicense.create();

}

}

4、  生成时使用到的 param.properties 文件如下

##########common parameters###########

#alias

PRIVATEALIAS=privatekey

#key(该密码生成密钥对的密码,需要妥善保管,不能让使用者知道)

KEYPWD=bigdata123456

#STOREPWD(该密码是在使用keytool生成密钥对时设置的密钥库的访问密码)

STOREPWD=abc123456

#SUBJECT

SUBJECT=bigdata

#licPath

licPath=bigdata.lic

#priPath

priPath=privateKeys.store

##########license content###########

#issuedTime

issuedTime=2014-04-01#notBeforeTime

notBefore=2014-04-01#notAfterTime

notAfter=2014-05-01#consumerType

consumerType=user

#ConsumerAmount

consumerAmount=1#info

info=this is a license

根据properties文件可以看出,这里只简单设置了使用时间的限制,当然可以自定义添加更多限制。该文件中表示授权者拥有私钥,并且知道生成密钥对的密码。并且设置license的内容。

三、验证证书(使用证书)(该部分代码结合需要授权的程序一起使用)

1、  首先 LicenseManagerHolder.java 类,同上。

2、  然后是主要验证 license 的代码 VerifyLicense.java

packagecn.melina.license;importjava.io.File;importjava.io.IOException;importjava.io.InputStream;importjava.util.Properties;importjava.util.prefs.Preferences;importde.schlichtherle.license.CipherParam;importde.schlichtherle.license.DefaultCipherParam;importde.schlichtherle.license.DefaultKeyStoreParam;importde.schlichtherle.license.DefaultLicenseParam;importde.schlichtherle.license.KeyStoreParam;importde.schlichtherle.license.LicenseParam;importde.schlichtherle.license.LicenseManager;/*** VerifyLicense

*@authormelina*/

public classVerifyLicense {//common param

private static String PUBLICALIAS = "";private static String STOREPWD = "";private static String SUBJECT = "";private static String licPath = "";private static String pubPath = "";public voidsetParam(String propertiesPath) {//获取参数

Properties prop = newProperties();

InputStream in=getClass().getResourceAsStream(propertiesPath);try{

prop.load(in);

}catch(IOException e) {//TODO Auto-generated catch block

e.printStackTrace();

}

PUBLICALIAS= prop.getProperty("PUBLICALIAS");

STOREPWD= prop.getProperty("STOREPWD");

SUBJECT= prop.getProperty("SUBJECT");

licPath= prop.getProperty("licPath");

pubPath= prop.getProperty("pubPath");

}public booleanverify() {/************** 证书使用者端执行 ******************/LicenseManager licenseManager=LicenseManagerHolder

.getLicenseManager(initLicenseParams());//安装证书

try{

licenseManager.install(newFile(licPath));

System.out.println("客户端安装证书成功!");

}catch(Exception e) {

e.printStackTrace();

System.out.println("客户端证书安装失败!");return false;

}//验证证书

try{

licenseManager.verify();

System.out.println("客户端验证证书成功!");

}catch(Exception e) {

e.printStackTrace();

System.out.println("客户端证书验证失效!");return false;

}return true;

}//返回验证证书需要的参数

private staticLicenseParam initLicenseParams() {

Preferences preference=Preferences

.userNodeForPackage(VerifyLicense.class);

CipherParam cipherParam= newDefaultCipherParam(STOREPWD);

KeyStoreParam privateStoreParam= newDefaultKeyStoreParam(

VerifyLicense.class, pubPath, PUBLICALIAS, STOREPWD, null);

LicenseParam licenseParams= newDefaultLicenseParam(SUBJECT,

preference, privateStoreParam, cipherParam);returnlicenseParams;

}

}

3、  验证测试程序 licenseVerifyTest.java

packagecn.melina.license;public classlicenseVerifyTest {public static voidmain(String[] args){

VerifyLicense vLicense= newVerifyLicense();//获取参数

vLicense.setParam("./param.properties");//验证证书

vLicense.verify();

}

}

4、  验证时使用到的Properties文件

##########common parameters###########

#alias

PUBLICALIAS=publiccert

#STOREPWD(该密码是在使用keytool生成密钥对时设置的密钥库的访问密码)

STOREPWD=abc123456

#SUBJECT

SUBJECT=bigdata

#licPath

licPath=bigdata.lic

#pubPath

pubPath=publicCerts.store

根据该验证的properties可以看出,使用者只拥有公钥,没有私钥,并且也只知道访问密钥库的密码,而不能知道生成密钥对的密码。

四、参考资料

truelicense官网

https://sourceforge.net/projects/truelicense/

luckymelina专栏

https://blog.csdn.net/luckymelina/article/details/22870665

老张专栏(支持绑定MAC地址)

https://blog.csdn.net/jingshuaizh/article/details/44461289

java程序license验证_基于TrueLicense实现产品License验证功能相关推荐

  1. keytool生成证书_基于 TrueLicense 的项目证书验证

    使用场景 1. 开发的软件产品在交付使用的时候,往往有一段时间的试用期,这期间我们不希望自己的代码被客户二次拷贝,这个时候 license 就派上用场了,license 的功能包括设定有效期.绑定 i ...

  2. 扫雷java程序算法设计_基于Java的Windows扫雷游戏的设计与实现毕业论文+任务书+翻译及原文+源码+辅导视频...

    基于Java的Windows扫雷游戏的设计与实现 摘 要 扫雷这款游戏有着很长的历史,从扫雷被开发出来到现在进行了无数次的优化,这款游戏变得越来越让人爱不释手了,简单的玩法在加上一个好看的游戏界面,每 ...

  3. TrueLicense实现产品License验证

    技术:apache-maven-3.3.9 +jdk1.8.0_102 运行环境:ideaIC-2020.1.3 + apache-maven-3.3.9+ jdk1.8.0_102 家精品内容,核心 ...

  4. 基于 TrueLicense 的项目证书验证

    基于 TrueLicense 的项目证书验证 使用场景 1. 开发的软件产品在交付使用的时候,往往有一段时间的试用期,这期间我们不希望自己的代码被客户二次拷贝,这个时候 license 就派上用场了, ...

  5. java项目----教务管理系统_基于Java的教务管理系统

    java项目----教务管理系统_基于Java的教务管理系统 2022-04-22 18:18·java基础 最近为客户开发了一套学校用教务管理系统,主要实现学生.课程.老师.选课等相关的信息化管理功 ...

  6. java工程license机制_使用truelicense实现用于JAVA工程license机制(包括license生成和验证)...

    开发的软件产品在交付使用的时候,往往会授权一段时间的试用期,这个时候license就派上用场了.不同于在代码中直接加上时间约束,需要重新授权的时候使用license可以避免修改源码,改动部署,授权方直 ...

  7. java调用c 串口_基于C语言的java串口通信程序

    目录 1.前言 2.windows  串口通信API 3.C/C++封装  动态运行库 4.JAVA-JNI  java程序调用C++程序 一.前言 &ensp ;写这个博客主要是因为自己想用 ...

  8. java 程序增加 防盗_防盗Java EE –保护Java EE企业应用程序的安全

    java 程序增加 防盗 Øredev离我们仅有几天的路程,我受邀作了两次演讲. 其中之一是关于我最喜欢的主题:安全性和Java EE. 它旨在实现两个目标. 一方面向典型的Java EE开发人员介绍 ...

  9. java程序员封闭_变态级JAVA程序员面试32问(转)

    第一,谈谈final, finally, finalize的区别. final?修饰符(关键字)如果一个类被声明为final,意味着它不能再派生出新的子类,不能作为父类被继承.因此一个类不能既被声明为 ...

最新文章

  1. php jwt使用案例,PHP使用JWT创建Token的实例详解
  2. IPSec ***基于ASA的配置(思科)
  3. 掌握这些!让Python不再从入门到放弃,初学者容易忽略的一些细节
  4. 兰州2021高考一诊成绩查询,2021兰州中考"一诊"成绩分析结果查询
  5. Raider对F#支持的技术细节
  6. PHP怎样表示几时几分,PHP将时间戳转换为刚刚、N分钟前、今天几点几分、昨天几点几分......
  7. 小鹏汽车2021财年总收入209.9亿元 同比增长259.1%
  8. yum安装 vs 源码编译安装
  9. oracle votedisk ocr,Oracle RAC 重建OCR和Votedisk
  10. Unity UGUI——UI基础,Canvas
  11. html5的web存储详解
  12. 圆的内接正n边形的周长
  13. 紫铜带、黄铜带、锡磷青铜带、白铜带的特性
  14. 物联网蜜罐地理分布情况
  15. Flink——运行的组件有哪些?分别有什么作用?
  16. 裸函数 __declspec(naked),C语言是怎么变成汇编的,用裸函数加汇编实现一个最简单的加法函数
  17. 人工智能时代将如何改变社会?
  18. 计算机软件的安装步骤及注意事项,组态软件介绍以及安装注意事项
  19. UOS利用系统安装光盘做本地apt源安装软件包
  20. [转载]-应用监控、弹出猎豹推荐弹窗

热门文章

  1. 利用JS制作抖音同款3D照片墙(three.js)
  2. html判定会员,会员详情查询.html
  3. 机器学习数据的预处理
  4. ArkID 一账通:企业级开源IDaaS/IAM平台系统
  5. HTML5期末大作业:影评网站的设计--豆瓣以及IMDb等影评网站
  6. 电脑配置单5(自用勿删)
  7. iOS - 微信分享无法显示好友列表
  8. 51JOB:根据HR处理简历的一般流程,简历投递后会有如下几种状态出现
  9. 计算机msvcr110.dll,msvcr110.dll
  10. nasm预处理器(1)