第一种方案

生成license

工具已经封装好,小伙伴们可以直接下载使用:

下载后打开cloud-license-serve项目直接启动即可。

然后调用项目的获取信息接口:http://localhost:9081/license/getserverinfos?osname=windows

会得到类似如下结果,分别代表ip地址、mac地址、cpu序号、主板序号。

{
    "ipaddress": [
        "192.168.80.1",
        "192.168.220.1"
    ],
    "macaddress": [
        "01-51-56-c0-00-01",
        "00-52-56-c0-00-08",
        "bc-54-2d-df-69-fc"
    ],
    "cpuserial": "bfecfbff000806ec",
    "mainboardserial": "l1hf16301d5"
}

使用jdk自带的 keytool 工具生成公私钥证书库:

假如我们设置公钥库密码为:public_password1234,私钥库密码为:private_password1234,则生成命令如下:

#生成命令
keytool -genkeypair -keysize 1024 -validity 3650 -alias "privatekey" -keystore "privatekeys.keystore" -storepass "public_password1234" -keypass "private_password1234" -dname "cn=localhost, ou=localhost, o=localhost, l=sh, st=sh, c=cn"
 
#导出命令
keytool -exportcert -alias "privatekey" -keystore "privatekeys.keystore" -storepass "public_password1234" -file "certfile.cer"
 
#导入命令
keytool -import -alias "publiccert" -file "certfile.cer" -keystore "publiccerts.keystore" -storepass "public_password1234"

上述命令执行完成之后,会在当前路径下生成三个文件,分别是:privatekeys.keystore、publiccerts.keystore、certfile.cer。其中文件certfile.cer不再需要可以删除,文件privatekeys.keystore用于当前的 serverdemo 项目给客户生成license文件,而文件publiccerts.keystore则随应用代码部署到客户服务器,用户解密license文件并校验其许可信息。

最后我们再生成license,调用接口地址为:http://localhost:9081/license/generatelicense

调用的参数是一个json参数,格式如下:

{
    "subject": "license_demo",
    "privatealias": "privatekey",
    "keypass": "private_password1234",
    "storepass": "public_password1234",
    "licensepath": "c:/users/zifangsky/desktop/license_demo/license.lic",
    "privatekeysstorepath": "c:/users/zifangsky/desktop/license_demo/privatekeys.keystore",
    "issuedtime": "2018-07-10 00:00:01",
    "expirytime": "2019-12-31 23:59:59",
    "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"
    }
}

如果请求成功,那么最后会在 licensepath 参数设置的路径生成一个 license.lic 的文件,这个文件就是给客户部署代码的服务器许可文件。

使用license

如果小伙伴们按照上文的步骤一步一步的跟着实现,我们已经获得了license.lic,接下来就是把license使用到我们自己的项目中了。

cloud-license-client就是引入项目的一个例子,打开可以直接使用。

引入自己的项目只需将以下文件导入

并配置好拦截器licensecheckinterceptor就可以使用了。配置方法在interceptorconfig类中,可以参考。

这里需要注意的是使用license需要两个文件:license.lic,publiccerts.keystore

演示项目配置的路径是绝对路径,一般我们会配置相对路径,把两个文件放到项目下,配置位置在licensechecklistener类中

修改如下部分改为相对路径读取就可以了

这里就不演示如何修改了,因为修改起来很容易。

还需要注意一点:

对于licensecheckmodel,licensecreatorparam两个类,引入到自己的客户端后一定要保证包名与生成license时的包名一致,不然会导致序列化失败的问题。

直接集成的方案

引入maven依赖

<dependency>
  <groupid>org.smartboot.license</groupid>
  <artifactid>license-client</artifactid>
  <version>1.0.3</version>
</dependency>

载入license。如若license已过期,则会触发异常。

public class licensetest {
  public static void main(string[] args) throws exception {
      file file=new file("license.txt");
      license license = new license();
      licenseentity licenseentity=license.loadlicense(file);
      system.out.println(new string(licenseentity.getdata()));
  }
}

获取licenseentity并以此配置启动软件。
还原license

  • 进入bin目录执行以下命令,例如:./license_revert.sh source.txt。
  • 执行成功后会在当前目录下生成license文件license_revert.txt。

简单方便,几行代码放在启动方法里校验,也可以加注在拦截器里。

一个简单方便的授权方式,只需以上几步就可集成到boot项目中去啦!

说了这么多,在演示下代码吧

生成机器码

我们首先要做的就是对软件部署的环境的唯一性进行限制,这里使用的是macadderss,当然你也可以换成cpu序列编号,并无太大影响,先上代码

private static string getmac() {
        try {
            enumeration<networkinterface> el = networkinterface
                    .getnetworkinterfaces();
            while (el.hasmoreelements()) {
                byte[] mac = el.nextelement().gethardwareaddress();
                if (mac == null)
                    continue;
                string hexstr = bytestohexstring(mac);
                return getsplitstring(hexstr, "-", 2).touppercase();
            }
        } catch (exception exception) {
            exception.printstacktrace();
        }
        return null;
    } 
 
public static string getmachinecode() throws exception{
        set<string> result = new hashset<>();
        string mac = getmac();
        result.add(mac);
        properties props = system.getproperties();
        string javaversion = props.getproperty("java.version");
        result.add(javaversion);
        string javavmversion = props.getproperty("java.vm.version");
        result.add(javavmversion);
        string osversion = props.getproperty("os.version");
        result.add(osversion);
        string code = encrpt.getmd5code(result.tostring());
        return getsplitstring(code, "-", 4);
 
    }

这里进行的操作是取出机器码,与java版本,jvm,操作系统参数进行混合,并进行md5操作

进行lic文件的生成

授权证书主要包含三个要素,机器码,是否永久有效标识,证书时效,我们会将这些数据写入文本中并进行加密处理,看下生成证书的代码

public static void getlicense(string isnotimelimit, string licenselimit, string machinecode, string licensepath, string priavatekeypath) throws exception{
        string[] liccontent = {
                "licenseid=yanpeng19940119@gmail.com",
                "licensename=yblog使用证书",
                messageformat.format("licensetype={0}",isnotimelimit),
                messageformat.format("expireday={0}",licenselimit), //日期采用yyyy-mm-dd日期格式
                messageformat.format("machinecode={0}",machinecode),
                ""
        };
 
        //将lic内容进行混合签名并写入内容
        stringbuilder sign = new stringbuilder();
        for(string item:liccontent){
            sign.append(item+"yblog");
        }
        liccontent[5] = messageformat.format("licensesign={0}",encrpt.getmd5code(sign.tostring()));
        fileutil.createfileandwritelines(licensepath,liccontent);
        //将写入的内容整体加密替换
        string filecontent =fileutil.readfiletostring(licensepath);
        string encrptfilecontent = encrpt.encriptwrsa_pri(filecontent,priavatekeypath);
        file file = new file(licensepath);
        file.delete();
        fileutil.createfile(licensepath,encrptfilecontent);

最后在验证lic,我们会在系统中注册一个拦截器,未通过系统授权认证会自动跳转到lic文件上传界面,springboot接收文件与常规java有一些不同,使用的multipartfile对象,会获取到上传文件的数组,进行操作。

我们就可以通过系统内置的公钥对lic文件的机器码,授权时间进行验证,确定是否能正常访问系统。

总结

好了,到这里本文的分享就结束了,本文分享的其实是license的使用说明,并没有带大家阅读源码去看原理,感兴趣的小伙伴可以自行阅读一下项目源码,也很容易看懂哦。

以上就是springboot生成license的多种实现方式的详细内容,更多关于springboot生成license的资料请关注www.887551.com其它相关文章!