当前位置: 服务支持 >  技术文档 >  Spring Cloud集成License许可证授权模块实战

Spring Cloud集成License许可证授权模块实战

阅读数 86
点赞 16
copyright 著作权
article_banner


由于客户需要对公司销售的软件进行试用,公司考虑只允许客户在指定服务器上进去短期试用,所以在系统中集成了License授权模块。

一、设计思路

首先,我们需要获取要部署的服务器的基本信息,然后根据服务器信息生成对应的许可证,许可证采用DSA加密算法进行加密,再通过网关过滤器,对所有权限进行校验。

二、获取服务器基本信息

创建获取服务器基本信息模块,打成jar包,在要授权的机器上运行,生成 device.json 文件

Maven依赖


<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.18</version>
</dependency>

<dependency>
    <groupId>de.schlichtherle.truelicense</groupId>
    <artifactId>truelicense-core</artifactId>
    <version>1.33</version>
</dependency>

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
</dependency>

<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>

主类:

通过 DeviceUtil 获取对应的设备信息并生成 device.json 文件到 ./license 目录下,可自行定义目录地址。


public class LicenseDeviceApplication {

    public static void main(String[] args) {
        try {
            DeviceInfo deviceInfo = DeviceUtil.getDeviceInfo();
            String json = new Gson().toJson(deviceInfo);
            Files.createDirectories(Paths.get("license"));
            Files.writeString(Paths.get("license", "device.json"), json);
            System.out.println("success");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

DeviceInfo 类:


@Data
@Accessors(chain = true)
public class DeviceInfo {

    private String cpu;

    private String mainBoard;

    private List<String> ipList;

    private List<String> macList;

}

DeviceUtil 类:

DeviceUtil 根据不同的操作系统采用不同的方式获取服务器数据


public class DeviceUtil {
    private static final String WIN_PRE = "win";

    public static DeviceInfo getDeviceInfo() throws Exception {
        String os = System.getProperty("os.name").toLowerCase();
        Device device;
        if (os.startsWith(WIN_PRE)) {
            device = new WinDevice();
        } else {
            device = new LinuxDevice();
        }
        return device.getDeviceInfo();
    }
}

DeviceUtil 依赖的几个类:

获取设备信息的抽象类,将不同操作系统的通用信息获取方法抽象出来



public abstract class Device {


   public DeviceInfo getDeviceInfo() throws Exception {

       return new DeviceInfo()

               .setIpList(this.getIpAddress())

               .setMacList(this.getMacAddress())

               .setCpu(this.getCpuSerial())

               .setMainBoard(this.getMainBoardSerial());

   }


   List<String> getIpAddress() throws Exception {

       List<InetAddress> inetAddresses = getLocalAllInetAddress();

       return Optional.ofNullable(inetAddresses).orElse(new ArrayList<>(0))

               .stream()

               .map(e -> e.getHostAddress().toLowerCase())

               .distinct()

               .collect(Collectors.toList());

   }


   List<String> getMacAddress() throws Exception {

       List<InetAddress> inetAddresses = getLocalAllInetAddress();

       return Optional.ofNullable(inetAddresses).orElse(new ArrayList<>(0))

               .stream()

               .map(this::getMacByInetAddress)

               .distinct()

               .collect(Collectors.toList());

   }


   /**

    * 获取 CpuSerial

    *

    * @return CpuSerial

    * @throws Exception Exception

    */

   abstract String getCpuSerial() throws Exception;


   /**

    * 获取 MainBoardSerial

    *

    * @return MainBoardSerial

    * @throws Exception Exception

    */

   abstract String getMainBoardSerial() throws Exception;


   List<InetAddress> getLocalAllInetAddress() throws Exception {

       List<InetAddress> inetAddressList = new ArrayList<>();

       for (Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();

            networkInterfaces.hasMoreElements(); ) {

           NetworkInterface networkInterface = networkInterfaces.nextElement();

           for (Enumeration<InetAddress> inetAddresses = networkInterface.getInetAddresses();

                inetAddresses.hasMoreElements(); ) {

               InetAddress inetAddr = inetAddresses.nextElement();

               if (!inetAddr.isLoopbackAddress()

                       && !inetAddr.isLinkLocalAddress()

                       && !inetAddr.isMulticastAddress()) {

                   inetAddressList.add(inetAddr);

               }

           }

       }

       return inetAddressList;

   }


   String getMacByInetAddress(InetAddress inetAddr) {

       try {

           byte[] mac = NetworkInterface

相关文章
QR Code
微信扫一扫,欢迎咨询~

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 155-2731-8020
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

手机不正确

公司不为空