ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

smart license简单使用感想

2020-12-15 17:32:52  阅读:786  来源: 互联网

标签:String license 感想 jar 校验 file new smart



中途换换脑子,顺便为以后做储备。看了下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的,防君子不防小人。

标签:String,license,感想,jar,校验,file,new,smart
来源: https://blog.csdn.net/zwrlj527/article/details/111225821

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有