java TrueLicense实现 实现License授权许可和验证

简述

可用于项目交付项目部署到甲方以及包括代码防止泄露,经常会出现公司内部代码被已离职人员在下家公司使用,底层代码的封装增加license部分,杜绝这块的问题。定期更换license文件可进行续期,项目中采用truelicense version 1.33实现,SpringBoot version 2.0.3.RELEASE。
代码如下:
license-server
License-client
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

私钥、公钥、私钥证书生成

私钥

keytool -genkey -alias privatekey -keystore privateKeys.store -validity 3650

私钥证书

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

公钥

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

License 生成

拉去码云中的项目
访问接口生成License文件

package com.license.server.controller;

import com.license.core.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 用于生成证书文件,不能放在给客户部署的代码里
 *
 * @author zifangsky
 * @date 2018/4/26
 * @since 1.0.0
 */
@RestController
@RequestMapping("/license")
public class LicenseCreatorController {

    /**
     * 证书生成路径
     */
    private String licensePath = "C:/Users/zifangsky/Desktop/license_demo/license.lic";

    /**
     * 获取服务器硬件信息
     *
     * @param osName 操作系统类型,如果为空则自动判断
     * @return com.ccx.models.license.LicenseCheckModel
     * @author zifangsky
     * @since 1.0.0
     */
    @RequestMapping(value = "/getServerInfos", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public LicenseCheckModel getServerInfos(@RequestParam(value = "osName", required = false) String osName) {
        //操作系统类型
        if (StringUtils.isBlank(osName)) {
            osName = System.getProperty("os.name");
        }
        osName = osName.toLowerCase();

        AbstractServerInfos abstractServerInfos = null;

        //根据不同操作系统类型选择不同的数据获取方法
        if (osName.startsWith("windows")) {
            abstractServerInfos = new WindowsServerInfos();
        } else if (osName.startsWith("linux")) {
            abstractServerInfos = new LinuxServerInfos();
        } else {//其他服务器类型
            abstractServerInfos = new LinuxServerInfos();
        }

        return abstractServerInfos.getServerInfos();
    }

    /**
     * 生成证书
     *
     * @param param 生成证书需要的参数,如:{"subject":"ccx-models","privateAlias":"privateKey","keyPass":"5T7Zz5Y0dJFcqTxvzkH5LDGJJSGMzQ","storePass":"3538cef8e7","licensePath":"C:/Users/zifangsky/Desktop/license.lic","privateKeysStorePath":"C:/Users/zifangsky/Desktop/privateKeys.keystore","issuedTime":"2018-04-26 14:48:12","expiryTime":"2018-12-31 00:00:00","consumerType":"User","consumerAmount":1,"description":"这是证书描述信息","licenseCheckModel":{"ipAddress":["192.168.245.1","10.0.5.22"],"macAddress":["00-50-56-C0-00-01","50-7B-9D-F9-18-41"],"cpuSerial":"BFEBFBFF000406E3","mainBoardSerial":"L1HF65E00X9"}}
     * @return java.util.Map<java.lang.String                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               ,                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               java.lang.Object>
     * @author zifangsky
     * @since 1.0.0
     */
    @RequestMapping(value = "/generateLicense", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Map<String, Object> generateLicense(@RequestBody(required = true) LicenseCreatorParam param) {
        Map<String, Object> resultMap = new HashMap<>(2);

        if (StringUtils.isBlank(param.getLicensePath())) {
            param.setLicensePath(licensePath);
        }

        LicenseCreator licenseCreator = new LicenseCreator(param);
        boolean result = licenseCreator.generateLicense();

        if (result) {
            resultMap.put("result", "ok");
            resultMap.put("msg", param);
        } else {
            resultMap.put("result", "error");
            resultMap.put("msg", "证书文件生成失败!");
        }

        return resultMap;
    }
//
//    @RequestMapping(value = "/generateLicense", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
//    public Map<String, Object> generateLicense(@RequestBody Map<String, Object> params) {
//        return licenseCreatorService.generateLicense(params);
//    }

}

License 客户端部署

生成完毕后
License客户端代码拉去到本地,然后打包到本地依赖库。若install后依赖还是存在问题可以通过如下命令

mvn install:install-file -DgroupId=com.license.client   -DartifactId=license-client -Dversion=1.0.0-SNAPSHOT  -Dfile=D:\LicenseDemo\license-client\target\license-client-1.0.0-SNAPSHOT.jar   -Dpackaging=jar

将自己的项目依赖这个license-client,添加自定义拦截器
该拦截器在license完成,只需在自己的项目中配置添加即可
在这里插入图片描述
在这里插入图片描述
上图 在 WebMvcConfigurer 中注入 拦截器 并且 添加拦截器 添加拦截地址
注:拦截地址为登录校验地址 如果 登录校验地址与 登录页跳转地址相同 请将登录验证地址更名 否则 拦截器 在没有通过认证证书的情况下 会将登陆页面拦截
配置文件

添加 license 配置 公钥路径 以及 证书路径可更改

license:
  subject: license_demo
  publicAlias: publicCert
  storePass: public_password1234
  licensePath: E:/license.lic

配置启动类
在这里插入图片描述

启动类包扫描 默认扫描 application启动类 文件平级以及下级的文件夹与类 配置license 包扫描路径 com.licanse

  • 19
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
Truelicense 是一个 Java 应用程序许可证管理库,它提供了一种安全,灵活和易于使用的方法来保护您的 Java 应用程序。 在 Truelicense 中使用 RSA,可以实现数字签名和验证,从而确保许可证的完整性和真实性。下面是使用 Truelicense 实现 RSA 的基本步骤: 1. 生成 RSA 密钥对 使用 JDK 自带的 keytool 工具生成 RSA 密钥对,例如: ``` keytool -genkeypair -alias mykey -keyalg RSA -keysize 2048 -validity 365 -keystore mykeystore.jks ``` 2. 配置 TruelicenseTruelicense 的配置文件中,指定密钥库文件和密钥库密码,例如: ``` license.keyStoreType = JKS license.keyStorePath = /path/to/mykeystore.jks license.keyStorePassword = mykeystorepassword license.keyAlias = mykey license.keyPassword = mykeypassword ``` 3. 签名许可证 在许可证生成时,使用私钥对许可证进行数字签名,例如: ```java // 加载许可证模板 LicenseTemplate template = LicenseTemplate.newStandard() .subject("MyApp License") .issued(new Date()) .expires(new Date(System.currentTimeMillis() + 365 * 24 * 60 * 60 * 1000L)) .build(); // 生成许可License license = License.create(template, privateKey); // 输出许可证 System.out.println(license.toString()); ``` 4. 验证许可证 在应用程序启动时,使用公钥对许可证进行验证,例如: ```java // 加载许可License license = License.load(new ByteArrayInputStream(licenseData)); // 验证许可证 if (license.verify(publicKey)) { System.out.println("License is valid"); } else { System.out.println("License is invalid"); } ``` 以上是使用 Truelicense 实现 RSA 的基本步骤,您可以根据您的具体需求进行适当的调整。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一码归一码@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值