smart license简单使用感想


中途换换脑子,顺便为以后做储备。看了下license,看到个比较新的smart license工具包。就简单弄弄试试。
一、先说说出处

我关注了一些公众号,从推文里看到的。绝对还不错,其实主要是简单,不过用license来预防盗版等等其实就是一个矛与盾的游戏。
出处:https://www.oschina.net/p/smart-license

二、使用
我是先看的java引入,现在凡是不支持springboot、maven引包的,我基本也不想看。其实也是不想破坏目前的springboot框架。
引包:

<!-- smart-license支持 -->
 <dependency>
      <groupId>org.smartboot.license</groupId>
      <artifactId>license-client</artifactId>
      <version>1.0.3</version>
  </dependency>

大致使用分2种:
1、通过license授权文件读取是否异常(超期),超期会有异常
2、与souce.txt做还原比对校验
我这里使用的是第一种,抛砖引玉大家自己飞。
三、生成license.txt
我使用的是jdk11,安装文档上的操作,报不能创建虚拟机的错误。在.bat脚本上设置jdk1.8的路径也是这个错,然后我就放弃了,实际看源码,想使用java的方式生成。源码时用的Jd看的,看的tar.gz包的license-service.jar。截图大家看看关键地方:
在这里插入图片描述
看到这,再看看其他jar
在这里插入图片描述
是不是绝对其他几个jar都有点熟悉,没错,那就是一些常用的jar。是不是觉得妥妥的可以使用java生成了?
三、java方式生成license.txt
这里我先没有试使用maven导入license-server,所以采用的时把这个jar放到项目的lib下,再再pom.xml设置引入lib下的这个jar。研究完成了做代码整理的时候,试了下,居然server包也可以引入

<dependency>
    <groupId>org.smartboot.license</groupId>
    <artifactId>license-server</artifactId>
    <version>1.0.3</version>
    <exclusions>
        <exclusion>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
        </exclusion>
    </exclusions>
</dependency>

这里因为它内部也有log,所以排除,不然老提示那个引入多个log的警告。看着日志框打印红色的日志有强迫症。
关键代码,其实就是复制的jar里LicenseServer类的main方法,下面时我建的一个junit测试类,简单试了生成与读取

/**
 * @author zhengwen
 **/
@Slf4j
public class LicenseTest {
    @Test
    public void createLicense() throws Exception{
        LicenseServer license = new LicenseServer();
        String expire = "1h";
        //文件
        //String data = "E:\\Workspaces\\intellij idea\\NG20\\DSI\\target\\DSI.jar";
        //字符串
        String data = "zwtest";
        String enKey = "";

        char type = expire.charAt(expire.length() - 1);
        int value = Integer.valueOf(expire.substring(0, expire.length() - 1));
        Calendar calendar = Calendar.getInstance();
        switch (type) {
            case 'h':
            case 'H':
                calendar.add(Calendar.HOUR, value);
                break;
            case 'd':
            case 'D':
                calendar.add(Calendar.DAY_OF_YEAR, value);
                break;
            case 'y':
            case 'Y':
                calendar.add(Calendar.YEAR, value);
                break;
            default:
                throw new UnsupportedOperationException(expire);
        }

        File file = new File(data);
        byte[] bytes;
        if (file.isFile()) {
            log.info("sign for file:{}", file.getPath());
            bytes = IOUtils.toByteArray(new FileInputStream(file));
        } else {
            log.info("sign for string:{}", data);
            bytes = data.getBytes();
        }
        if (StringUtils.isNotBlank(enKey)){
            license.createLicense(bytes, calendar.getTime(), Base64.getDecoder().decode(enKey));
        } else {
            license.createLicense(bytes, calendar.getTime());
        }
        log.info("--sign end--");
    }

    //@Test
    public void readLicense(){
        String licensePath = "E:\\Workspaces\\intellij idea\\NG20\\DSI";
        File file = new File(licensePath + File.separator +"license.txt");
        License license = new License();

        LicenseEntity licenseEntity = license.loadLicense(file);
        System.out.println(new String(licenseEntity.getData()));
    }
}

四、我的简单使用
我这里是一个springboot项目,不想破坏太多代码,毕竟这个上面还没说用不用了,所以就封装了校验读取方法,在项目的启动类上加个先判断license校验。

校验工具类

/**
 * License简单工具类
 * @author zhengwen
 **/
@Slf4j
public class LicenseUtil {
    /**
     * license文件校验
     * @return 是否过期
     */
    public static boolean checkSoftLicense() {
        boolean licensePass = true;
        String path = System.getProperty("user.dir");
        File file = new File(path,"license.txt");

        License license = new License();
        try {
            LicenseEntity licenseEntity = license.loadLicense(file);
            log.debug("-------license加密文件选的内容:{}",new String(licenseEntity.getData()));
            //System.out.println(new String(licenseEntity.getData()));
        }catch (LicenseException e){
            log.error("---license授权文件异常---");
            licensePass = false;
        }
        return licensePass;
    }
}

启动类增加

public static void main(String[] args) {
    //可能会遇到user.dir路径读取的问题,先这样简单处理,还有种方式是可以使用路径配置,再config里配置类里校验,那样要修改没这方便,反正是先研究使用而已
    boolean licensePass = LicenseUtil.checkSoftLicense();
    if (licensePass){
        SpringApplication.run(DsiApplication.class, args);
        System.out.println("--数据服务接口启动成功--");
    }else {
        System.out.println("--数据服务接口license校验不通过--");
    }
}

到此就ok了,先校验是否过期,没过期项目就继续启动。
五、说说问题
1、生成license.txt慢,之前我是想用项目的jar生成,生成倒是没问题,就是慢。在一个就是生成的license文件比我的jar文件还大,这显然不行。
2、建议把校验方式也写道client里,让用户无感的,配置个license.txt路径,调用下方法,传入路径、key等返回布尔值就行(当然这个我们也可以自己做)。
就这2点吧,用这个给重要文件或者部署服务器的mac、ip等生成一个就license就行,校验逻辑,我们可以自行校验(项目读取mac、ip等等进行比对校验,双保险)
这个嘛,其实也没有什么特别的,毕竟现在这种方式只要知道生成逻辑就可以破。现在很多都是采用的授权服务器传参校验,动态授权、生成授权文件的。但是有些项目是没有外网的,所以这种方式也是ok的,防君子不防小人。

smart-license是一款用于安全加固的开源项目。主要服务于非开源产品、商业软件、具备试用功能的付费软件等,为软件提供授权制的使用方式。smart-license适用场景非开源产品、商业软件、收费软件。 限制产品的传播性,每个客户拥有专属 License。 同一款软件发行包根据 License 的不同提供不同的服务能力。 限定软件授权时效smart-license特色开源,代码完全公开,License生成原理是透明的。 易用,提供二进制包,直接基于命令行生成 License。 安全,生成License 在一定程度上具备防篡改能力,破解难度大。 安全加固,采用非对称加密方式对 License源数据 进行预处理,防止伪造Licensesmart-license使用方式生成License 1、下载smart-license.tar.gz包,解压 2、进入bin目录执行以下命令,例如:./license.sh 1d HelloWorld。 1d:表示授权效期1天,即一天后该License便过期。支持的效期格式包括: h,1h:1小时; 2h:2小时 d,1d:1天; 10d:10天 y,1y:1年; 2y:2年 HelloWorld:表示待加密的license内容。 实际场景下可以通过license授权不同的产品功能和有效期,例如:./license.sh 1y features_1:on;features_2:off; 如果待授权的license内容为文件,可以采用同样的命令,例如:./license.sh 1y config.properties 3、执行成功后,会在当前目录下生成 License:license.txt以及 License源文件:source.txt。 注意:license.txt是提供给客户的授权文件;而source.txt是由软件提供方持有,其中包含加密私钥,需要妥善保使用License 1、引入Maven依赖 org.smartboot.license license-client 1.0.0-SNAPSHOT 2、载入License。如若License已过期,则会触发异常。 public class LicenseTest { public static void main(String[] args) throws Exception { File file=new File("license.txt"); License license = new License(); LicenseConfig licenseConfig=license.loadLicense(file); System.out.println(licenseData.getOriginal()); } } 3、获取licenseData并以此配置启动软件。smart-license截图
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

肥仔哥哥1930

来一波支持,吃不了亏上不了当

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

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

打赏作者

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

抵扣说明:

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

余额充值