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

java生成license_使用truelicense实现用于JAVA工程license机制(包括license生成和验证)…开发的软件产品在交付使用的时候,往往会授权一段时间的试用期,这个时候license就派上用场了。不同于在代码中直接加上时间约束,需要重新授权的时候使用license可以避免修改源码,改动部署,授权方直接生成一个新的license发送给使用方替换掉原来的license文件即可。下面将讲述使用truelicense来实现license的生成和使用。Truelicense是一个开源的证书管理引擎,详细介…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

开发的软件产品在交付使用的时候,往往会授权一段时间的试用期,这个时候license就派上用场了。不同于在代码中直接加上时间约束,需要重新授权的时候使用license可以避免修改源码,改动部署,授权方直接生成一个新的license发送给使用方替换掉原来的license文件即可。下面将讲述使用truelicense来实现license的生成和使用。Truelicense是一个开源的证书管理引擎,详细介绍见https://truelicense.java.net/

一、首先介绍下license授权机制的原理:

1、 生成密钥对,方法有很多。

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

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

接下来是本例制作license的具体步骤:

二、第一步:使用keytool生成密钥对

以下命令在dos命令行执行,注意当前执行目录,最后生成的密钥对即在该目录下:

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

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

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

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

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

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

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

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

1、 首先LicenseManagerHolder.java类:

01.package cn.melina.license;

02.import de.schlichtherle.license.LicenseManager;

03.import de.schlichtherle.license.LicenseParam;

04.

05./**

06.* LicenseManager??????

07.* @author melina

08.*/

09.public class LicenseManagerHolder {

10.

11.private static LicenseManager licenseManager;

12.

13.public static synchronized LicenseManager getLicenseManager(LicenseParam licenseParams) {

14.if (licenseManager ==null) {

15.licenseManager =new LicenseManager(licenseParams);

16.}

17.return licenseManager;

18.}

19.}

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

001.package cn.melina.license;

002.

003.import java.io.File;

004.import java.io.IOException;

005.import java.io.InputStream;

006.import java.text.DateFormat;

007.import java.text.ParseException;

008.import java.text.SimpleDateFormat;

009.import java.util.Properties;

010.import java.util.prefs.Preferences;

011.import javax.security.auth.x500.X500Principal;

012.import de.schlichtherle.license.CipherParam;

013.import de.schlichtherle.license.DefaultCipherParam;

014.import de.schlichtherle.license.DefaultKeyStoreParam;

015.import de.schlichtherle.license.DefaultLicenseParam;

016.import de.schlichtherle.license.KeyStoreParam;

017.import de.schlichtherle.license.LicenseContent;

018.import de.schlichtherle.license.LicenseParam;

019.import de.schlichtherle.license.LicenseManager;

020.

021./**

022.* CreateLicense

023.* @author melina

024.*/

025.public class CreateLicense {

026.//common param

027.private static String PRIVATEALIAS =””;

028.private static String KEYPWD =””;

029.private static String STOREPWD =””;

030.private static String SUBJECT =””;

031.private static String licPath =””;

032.private static String priPath =””;

033.//license content

034.private static String issuedTime =””;

035.private static String notBefore =””;

036.private static String notAfter =””;

037.private static String consumerType =””;

038.private static int consumerAmount =0;

039.private static String info =””;

040.// 为了方便直接用的API里的例子

041.// X500Princal是一个证书文件的固有格式,详见API

042.private final static X500Principal DEFAULTHOLDERANDISSUER =new X500Principal(

043.”CN=Duke、OU=JavaSoft、O=Sun Microsystems、C=US”);

044.

045.public void setParam(String propertiesPath) {

046.// 获取参数

047.Properties prop =new Properties();

048.InputStream in = getClass().getResourceAsStream(propertiesPath);

049.try {

050.prop.load(in);

051.}catch (IOException e) {

052.// TODO Auto-generated catch block

053.e.printStackTrace();

054.}

055.PRIVATEALIAS = prop.getProperty(“PRIVATEALIAS”);

056.KEYPWD = prop.getProperty(“KEYPWD”);

057.STOREPWD = prop.getProperty(“STOREPWD”);

058.SUBJECT = prop.getProperty(“SUBJECT”);

059.KEYPWD = prop.getProperty(“KEYPWD”);

060.licPath = prop.getProperty(“licPath”);

061.priPath = prop.getProperty(“priPath”);

062.//license content

063.issuedTime = prop.getProperty(“issuedTime”);

064.notBefore = prop.getProperty(“notBefore”);

065.notAfter = prop.getProperty(“notAfter”);

066.consumerType = prop.getProperty(“consumerType”);

067.consumerAmount = Integer.valueOf(prop.getProperty(“consumerAmount”));

068.info = prop.getProperty(“info”);

069.

070.}

071.

072.public boolean create() {

073.try {

074./************** 证书发布者端执行 ******************/

075.LicenseManager licenseManager = LicenseManagerHolder

076..getLicenseManager(initLicenseParams0());

077.licenseManager.store((createLicenseContent()),new File(licPath));

078.}catch (Exception e) {

079.e.printStackTrace();

080.System.out.println(“客户端证书生成失败!”);

081.return false;

082.}

083.System.out.println(“服务器端生成证书成功!”);

084.return true;

085.}

086.

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

088.private static LicenseParam initLicenseParams0() {

089.Preferences preference = Preferences

090..userNodeForPackage(CreateLicense.class);

091.// 设置对证书内容加密的对称密码

092.CipherParam cipherParam =new DefaultCipherParam(STOREPWD);

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

094.KeyStoreParam privateStoreParam =new DefaultKeyStoreParam(

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

096.LicenseParam licenseParams =new DefaultLicenseParam(SUBJECT,

097.preference, privateStoreParam, cipherParam);

098.return licenseParams;

099.}

100.

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

102.public final static LicenseContent createLicenseContent() {

103.DateFormat format =new SimpleDateFormat(“yyyy-MM-dd”);

104.LicenseContent content =null;

105.content =new LicenseContent();

106.content.setSubject(SUBJECT);

107.content.setHolder(DEFAULTHOLDERANDISSUER);

108.content.setIssuer(DEFAULTHOLDERANDISSUER);

109.try {

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

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

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

113.}catch (ParseException e) {

114.// TODO Auto-generated catch block

115.e.printStackTrace();

116.}

117.content.setConsumerType(consumerType);

118.content.setConsumerAmount(consumerAmount);

119.content.setInfo(info);

120.// 扩展

121.content.setExtra(new Object());

122.return content;

123.}

124.}

3、 测试程序licenseCreateTest.java:

01.package cn.melina.license;

02.import cn.melina.license.CreateLicense;

03.public class licenseCreateTest {

04.public static void main(String[] args){

05.CreateLicense cLicense =new CreateLicense();

06.//获取参数

07.cLicense.setParam(“./param.properties”);

08.//生成证书

09.cLicense.create();

10.}

11.}

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

01.##########common parameters###########

02.#alias

03.PRIVATEALIAS=privatekey

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

05.KEYPWD=bigdata123456

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

07.STOREPWD=abc123456

08.#SUBJECT

09.SUBJECT=bigdata

10.#licPath

11.licPath=bigdata.lic

12.#priPath

13.priPath=privateKeys.store

14.##########license content###########

15.#issuedTime

16.issuedTime=2014-04-01

17.#notBeforeTime

18.notBefore=2014-04-01

19.#notAfterTime

20.notAfter=2014-05-01

21.#consumerType

22.consumerType=user

23.#ConsumerAmount

24.consumerAmount=1

25.#info

26.info=this is a license

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

四、第三步:验证证书(使用证书)(该部分代码结合需要授权的程序使用)

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

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

01.package cn.melina.license;

02.

03.import java.io.File;

04.import java.io.IOException;

05.import java.io.InputStream;

06.import java.util.Properties;

07.import java.util.prefs.Preferences;

08.

09.import de.schlichtherle.license.CipherParam;

10.import de.schlichtherle.license.DefaultCipherParam;

11.import de.schlichtherle.license.DefaultKeyStoreParam;

12.import de.schlichtherle.license.DefaultLicenseParam;

13.import de.schlichtherle.license.KeyStoreParam;

14.import de.schlichtherle.license.LicenseParam;

15.import de.schlichtherle.license.LicenseManager;

16.

17./**

18.* VerifyLicense

19.* @author melina

20.*/

21.public class VerifyLicense {

22.//common param

23.private static String PUBLICALIAS =””;

24.private static String STOREPWD =””;

25.private static String SUBJECT =””;

26.private static String licPath =””;

27.private static String pubPath =””;

28.

29.public void setParam(String propertiesPath) {

30.// 获取参数

31.Properties prop =new Properties();

32.InputStream in = getClass().getResourceAsStream(propertiesPath);

33.try {

34.prop.load(in);

35.}catch (IOException e) {

36.// TODO Auto-generated catch block

37.e.printStackTrace();

38.}

39.PUBLICALIAS = prop.getProperty(“PUBLICALIAS”);

40.STOREPWD = prop.getProperty(“STOREPWD”);

41.SUBJECT = prop.getProperty(“SUBJECT”);

42.licPath = prop.getProperty(“licPath”);

43.pubPath = prop.getProperty(“pubPath”);

44.}

45.

46.public boolean verify() {

47./************** 证书使用者端执行 ******************/

48.

49.LicenseManager licenseManager = LicenseManagerHolder

50..getLicenseManager(initLicenseParams());

51.// 安装证书

52.try {

53.licenseManager.install(new File(licPath));

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

55.}catch (Exception e) {

56.e.printStackTrace();

57.System.out.println(“客户端证书安装失败!”);

58.return false;

59.}

60.// 验证证书

61.try {

62.licenseManager.verify();

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

64.}catch (Exception e) {

65.e.printStackTrace();

66.System.out.println(“客户端证书验证失效!”);

67.return false;

68.}

69.return true;

70.}

71.

72.// 返回验证证书需要的参数

73.private static LicenseParam initLicenseParams() {

74.Preferences preference = Preferences

75..userNodeForPackage(VerifyLicense.class);

76.CipherParam cipherParam =new DefaultCipherParam(STOREPWD);

77.

78.KeyStoreParam privateStoreParam =new DefaultKeyStoreParam(

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

80.LicenseParam licenseParams =new DefaultLicenseParam(SUBJECT,

81.preference, privateStoreParam, cipherParam);

82.return licenseParams;

83.}

84.}

3、 测试程序licenseVerifyTest.java:

01.package cn.melina.license;

02.

03.public class licenseVerifyTest {

04.public static void main(String[] args){

05.VerifyLicense vLicense =new VerifyLicense();

06.//获取参数

07.vLicense.setParam(“./param.properties”);

08.//验证证书

09.vLicense.verify();

10.}

11.}

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

01.##########common parameters###########

02.#alias

03.PUBLICALIAS=publiccert

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

05.STOREPWD=abc123456

06.#SUBJECT

07.SUBJECT=bigdata

08.#licPath

09.licPath=bigdata.lic

10.#pubPath

11.pubPath=publicCerts.store

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

五、说明:

注意实际操作中,公钥、私钥、证书等文件的存放路径。

以上代码需要用到truelicense的一些包,可以自行网上搜,也可以下载我的完整工程,里面附带了所需的jar包。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/164391.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

  • 什么是工作流技术?

    什么是工作流技术?工作流(Workflow)就是工作流程的计算模型,即将工作流程中的工作如何前后组织在一起的逻辑和规则在计算机中以恰当的模型进行表示并对其实施计算。工作流要解决的主要问题是:为实现某个业务目标,在多个参

  • TCP端口检测、网络连接时延测试工具 tcping

    TCP端口检测、网络连接时延测试工具 tcping原文地址:https://zhangnq.com/3158.html在主流的linux系统中,通过yum或者apt也可安装tcping,不过通过源安装的tcping只能显示单次检测的结果,也没有具体的连接时延。现参考windows版tcping,用python写了一个linux环境下的类似工具。代码建立socket连接,测试端口连通性和网络连接时延。deftcp(ip,por…

  • PyCharm中如何设置切换Python Console终端的Python版本「建议收藏」

    PyCharm中如何设置切换Python Console终端的Python版本「建议收藏」在使用PyCharm的PythonConsole终端时,有时我们会需要切换终端运行的Python版本,比如下图中我的Console当前使用的版本是Python2,但是我现在想切换到Python3版本,如何做呢? 我的PyCharm版本是2018.1专业版:首先选择File——>Settings,然后进行如下操作:双击Build,Execution,Deploymen…

  • uml用例图详解_uml模型图

    uml用例图详解_uml模型图用例图的含义由参与者(Actor)、用例(UseCase)以及它们之间的关系构成的用于描述系统功能的动态视图称为用例图。其中用例和参与者之间的对应关系又叫做通讯关联(CommunicationAssociation)。用例图的作用用例图是需求分析中的产物,主要作用是描述参与者与和用例之间的关系,帮助开发人员可视化地了解系统的功能。借助于用例图,系统用户、系统分析人员、系统设计人员、领…

  • 线程锁EnterCriticalSection和LeaveCriticalSection的用法

    线程锁EnterCriticalSection和LeaveCriticalSection的用法线程锁的概念函数EnterCriticalSection和LeaveCriticalSection的用法注:使用结构CRITICAL_SECTION需加入头文件#include“afxmt.h”定义一个全局的锁CRITICAL_SECTION的实例  和一个静态全局变量CRITICAL_SECTIONcs;//可以理解为锁定一个资源staticintn_AddVal

  • $.ajax()方法详解学习

    在工作总是会有很多地方用到异步请求,有时候用快捷方法 get/post 或者getJson不能满足自己的需求,所以必须使用底层的ajax来实现异步请求,每次写完下次在用到的时候就记不清楚了,就在这里记录一下,方便自己以后使用和其他人学习! 主要是参考整理JQuery的文档和一些好博客内容!记录一下平常工作最常用的!

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号