受朋友所托,需要给产品加上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 类

package cn.melina.license;
import de.schlichtherle.license.LicenseManager;
import de.schlichtherle.license.LicenseParam; /**
* LicenseManagerHolder
* @author melina
*/
public class LicenseManagerHolder { private static LicenseManager licenseManager; public static synchronized LicenseManager getLicenseManager(LicenseParam licenseParams) {
if (licenseManager == null) {
licenseManager = new LicenseManager(licenseParams);
}
return licenseManager;
}
}

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

package cn.melina.license;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Properties;
import java.util.prefs.Preferences;
import javax.security.auth.x500.X500Principal;
import de.schlichtherle.license.CipherParam;
import de.schlichtherle.license.DefaultCipherParam;
import de.schlichtherle.license.DefaultKeyStoreParam;
import de.schlichtherle.license.DefaultLicenseParam;
import de.schlichtherle.license.KeyStoreParam;
import de.schlichtherle.license.LicenseContent;
import de.schlichtherle.license.LicenseParam;
import de.schlichtherle.license.LicenseManager; /**
* CreateLicense
* @author melina
*/
public class CreateLicense {
//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 = new X500Principal(
"CN=Duke、OU=JavaSoft、O=Sun Microsystems、C=US"); public void setParam(String propertiesPath) {
// 获取参数
Properties prop = new Properties();
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 boolean create() {
try {
/************** 证书发布者端执行 ******************/
LicenseManager licenseManager = LicenseManagerHolder
.getLicenseManager(initLicenseParams0());
licenseManager.store((createLicenseContent()), new File(licPath));
} catch (Exception e) {
e.printStackTrace();
System.out.println("客户端证书生成失败!");
return false;
}
System.out.println("服务器端生成证书成功!");
return true;
} // 返回生成证书时需要的参数
private static LicenseParam initLicenseParams0() {
Preferences preference = Preferences
.userNodeForPackage(CreateLicense.class);
// 设置对证书内容加密的对称密码
CipherParam cipherParam = new DefaultCipherParam(STOREPWD);
// 参数1,2从哪个Class.getResource()获得密钥库;参数3密钥库的别名;参数4密钥库存储密码;参数5密钥库密码
KeyStoreParam privateStoreParam = new DefaultKeyStoreParam(
CreateLicense.class, priPath, PRIVATEALIAS, STOREPWD, KEYPWD);
LicenseParam licenseParams = new DefaultLicenseParam(SUBJECT,
preference, privateStoreParam, cipherParam);
return licenseParams;
} // 从外部表单拿到证书的内容
public final static LicenseContent createLicenseContent() {
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
LicenseContent content = null;
content = new LicenseContent();
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(new Object());
return content;
}
}

3、  测试程序 licenseCreateTest.java

package cn.melina.license;
import cn.melina.license.CreateLicense;
public class licenseCreateTest {
public static void main(String[] args){
CreateLicense cLicense = new CreateLicense();
//获取参数
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

package cn.melina.license;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.prefs.Preferences; import de.schlichtherle.license.CipherParam;
import de.schlichtherle.license.DefaultCipherParam;
import de.schlichtherle.license.DefaultKeyStoreParam;
import de.schlichtherle.license.DefaultLicenseParam;
import de.schlichtherle.license.KeyStoreParam;
import de.schlichtherle.license.LicenseParam;
import de.schlichtherle.license.LicenseManager; /**
* VerifyLicense
* @author melina
*/
public class VerifyLicense {
//common param
private static String PUBLICALIAS = "";
private static String STOREPWD = "";
private static String SUBJECT = "";
private static String licPath = "";
private static String pubPath = ""; public void setParam(String propertiesPath) {
// 获取参数
Properties prop = new Properties();
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 boolean verify() {
/************** 证书使用者端执行 ******************/ LicenseManager licenseManager = LicenseManagerHolder
.getLicenseManager(initLicenseParams());
// 安装证书
try {
licenseManager.install(new File(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 static LicenseParam initLicenseParams() {
Preferences preference = Preferences
.userNodeForPackage(VerifyLicense.class);
CipherParam cipherParam = new DefaultCipherParam(STOREPWD); KeyStoreParam privateStoreParam = new DefaultKeyStoreParam(
VerifyLicense.class, pubPath, PUBLICALIAS, STOREPWD, null);
LicenseParam licenseParams = new DefaultLicenseParam(SUBJECT,
preference, privateStoreParam, cipherParam);
return licenseParams;
}
}

3、  验证测试程序 licenseVerifyTest.java

package cn.melina.license;

public class licenseVerifyTest {
public static void main(String[] args){
VerifyLicense vLicense = new VerifyLicense();
//获取参数
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


作者:朝雨忆轻尘
出处:https://www.cnblogs.com/xifengxiaoma/
版权所有,欢迎转载,转载请注明原文作者及出处。

基于TrueLicense实现产品License验证功能的更多相关文章

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

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

  2. 基于权限安全框架Shiro的登录验证功能实现

    目前在企业级项目里做权限安全方面喜欢使用Apache开源的Shiro框架或者Spring框架的子框架Spring Security. Apache Shiro是一个强大且易用的Java安全框架,执行身 ...

  3. EBS OAF开发中的Java 实体对象(Entity Object)验证功能补充

    EBS OAF开发中的Java 实体对象(Entity Object)验证功能补充 (版权声明,本人原创或者翻译的文章如需转载,如转载用于个人学习,请注明出处:否则请与本人联系,违者必究) EO理论上 ...

  4. 微信小程序产品定位及功能介绍

    产品定位及功能介绍 微信小程序是一种全新的连接用户与服务的方式,它可以在微信内被便捷地获取和传播,同时具有出色的使用体验. 小程序注册 注册小程序帐号 在微信公众平台官网首页(mp.weixin.qq ...

  5. javamail实现邮箱验证功能

    javamail是基于SMTP协议和POP3协议的邮件发送与接收系统,在用户注册与登陆的过程中,常需要用到邮箱验证功能,下面是基于javamail的一个简单实现 用户注册后通过servlet得到邮箱地 ...

  6. 基于django的自定义简单session功能

    基于django的自定义简单session功能 简单思路: 1.建立自定义session数据库 2.登入时将用户名和密码存入session库 3.将自定义的随机session_id写入cookie中 ...

  7. (译) 在AngularJS中使用的表单验证功能【转】

    验证功能是AngularJS里面最酷炫的功能之一,它可以让你写出一个具有良好用户体验的Web应用. 在AngularJS中,有许多用于验证的指令.我们将先学习几个最流行的内置指令,然后再创建一个自定义 ...

  8. (译) 在AngularJS中使用的表单验证功能

    验证功能是AngularJS里面最酷炫的功能之一,它可以让你写出一个具有良好用户体验的Web应用. 在AngularJS中,有许多用于验证的指令.我们将先学习几个最流行的内置指令,然后再创建一个自定义 ...

  9. B2C电子商务系统研发——产品媒体常见功能点

    产品媒体常见功能点 电商研发系列——产品媒体常见功能点 支持图片.视频和文档等媒体类型 产品图片对清晰度要求比极高,但又不能太大,所以图片一般是jpg格式. 视频一般是flv流媒体格式,如果是嵌入产品 ...

随机推荐

  1. springmvc 开涛 注解式控制器

    版本 定义处理器类 处理器映射适配器 备注 支持的注解 2.5前 controller       2.5 注解 DefaultAnnotationHandlerMapping AnnotationM ...

  2. java web前端调试手段

    1.console.log() 2. jQuery.ajax({                     url:"/task1/com/guodiantong/servlet/JsonTo ...

  3. Discoverer Table

    http://www.cnblogs.com/fandychen/p/3182826.html EUL4_BAS Table gives list of Business Areas EUL4_OBJ ...

  4. Java计算手机九宫格锁屏图案连接9个点的方案总数

    (一)问题 九宫格图案解锁连接9个点共有多少种方案? (二)初步思考 可以把问题抽象为求满足一定条件的1-9的排列数(类似于“八皇后问题”),例如123456789和987654321都是合法的(按照 ...

  5. Microsoft.Web.Administration操作IIS7时的权限设置

    在用Microsoft.Web.Administration操作IIS7时,你可能会遇到如下权限错误: 文件名: redirection.config错误: 由于权限不足而无法读取配置文件 如下图: ...

  6. 为什么不能用Abort退出线程

    在使用线程时,如果线程还未结束直接退出线程很有可能会导致数据丢失. class threadAbort { static void Main(string[] args) { WriteMessage ...

  7. 二,PHP会话机制---session的基本使用

    1,思考:登录网站后,在每个网页都能拿到用户信息 (1) 使用超链接传递用户名,这样太繁琐了,不建议使用 . (2) 使用数据库,每打开一个页面都查询一次用户信息表,这样网页加载速度变慢,用户体验变差 ...

  8. firebug中html显示为灰色的原因总结

    1.被设置了display:none. 2.长.宽都为0.

  9. Java并发工具类之同步屏障CyclicBarrier

    CyclicBarrier的字面意思是可以循环使用的Barrier,它要做的事情是让一个线程到达一个Barrier的时候被阻塞,直到最后一个线程到达Barrier,屏障才会放开,所有被Barrier拦 ...

  10. OS之进程管理---孤儿进程和僵尸进程

    僵尸进程 当一个进程终止时,操作系统会释放其资源,不过它位于进程表中的条目还是在的,直到它的父进程调用wait():这是因为进程表中包含了进程的退出状态.当进程已经终止,但是其父进尚未调用wait() ...