# LicenseDemo **Repository Path**: Instinctb/LicenseDemo ## Basic Information - **Project Name**: LicenseDemo - **Description**: 改造-在基于Spring的项目中使用 TrueLicense 生成和验证License(服务器许可)的示例代码- - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 221 - **Created**: 2023-10-23 - **Last Updated**: 2023-10-25 ## Categories & Tags **Categories**: Uncategorized **Tags**: 懒人必备 ## README # LicenseDemo #### 项目介绍 在基于Spring的项目中使用 `TrueLicense `生成和验证`License证书`(服务器许可)的示例代码 #### 技术依赖: * `Spring Boot`:项目基础架构 * `TrueLicense `:基于`Java`实现的生成和验证服务器许可的简单框架 #### 环境依赖: * `JDK8+` #### 两个子项目说明: #### - `ServerDemo`:用于**开发者**给客户生成`License证书`的示例代码 - `ClientDemo`:**模拟需要给客户部署的业务项目** #### ServerDemo项目: #### 对外发布了两个RESTful接口: (1)获取服务器硬件信息 : 请求地址:`http://127.0.0.1:7000/license/getServerInfos` ![获取服务器硬件信息](https://www.zifangsky.cn/wp-content/uploads/2018/07/20180710140711.png) (2)生成证书 : 请求地址:`http://127.0.0.1:7000/license/generateLicense` 请求时需要在Header中添加一个 **Content-Type** ,其值为:**application/json;charset=UTF-8**。请求参数如下: ```json { "subject": "license_demo", "privateAlias": "privateKey", "keyPass": "private_password1234", "storePass": "public_password1234", "privateKeysStorePath": "E:\\privateKeys.keystore", "issuedTime": "2023-10-24 15:00:12", "expiryTime": "2024-12-31 00:00:00", "consumerType": "User", "consumerAmount": 1, "description": "这是证书描述信息", "licenseCheckModel": { "macAddress": [ "00-50-56-C0-00-01", "50-7B-9D-F9-18-41" ] } } ``` ![生成证书](https://www.zifangsky.cn/wp-content/uploads/2018/07/20180710141528.png) #### ClientDemo项目: #### 项目启动时安装证书,通过`cn/zifangsky/license/LicenseCheckListener.java`类实现。用户登录时校验证书的可用性,通过`cn/zifangsky/license/LicenseCheckInterceptor.java`类实现。 #### 通过Jdk(建议jdk1.8)自带的keytool工具生成证书: #### #生成命令 keytool -genkeypair -keysize 1024 -validity 365 -alias "privateKey" -keystore "privateKeys.keystore" -storepass "water_password1234" -keypass "water_link1234" -dname "CN=bo, OU=water, O=car, L=BJ, ST=BJ, C=CN" keysize 密钥长度 validity 私钥的有效期(单位:天) alias 私钥别称 keystore 指定私钥库文件的名称 (生成在当前目录) storepass 指定私钥库的密码 (keystore 文件存储密码) keypass 指定别名条目的密码 (私钥加解密密码) dname 证书个人信息 CN 为你的姓名 OU 为你的组织单位名称 O 为你的组织名称 L 为你所在的城市名称 ST 为你所在的省份名称 C 为你的国家名称 1、首先要用KeyTool工具来生成密钥库:(-alias别名 –validity 3650表示10年有效) keytool -genkey -alias privatekeys -keysize 1024 -keystore privateKeys.store -validity 3650 2、然后将密钥库中名称为‘privatekeys’的证书条目导出到证书文件certfile.cer中: keytool -export -alias privatekeys -file certfile.cer -keystore privateKeys.store 3、然后再把这个证书文件的信息导入到公钥库中别名为publiccert的证书条目中: keytool -import -alias publiccert -file certfile.cer -keystore publicCerts.store 最后生成的文件privateKeys.store和publicCerts.store拷贝出来备用。