【工作记录】springboot应用实现license认证

news2024/11/28 5:29:33

前言

License授权是一种常见的商业模式,一般用于在客户端部署项目后进行使用人员或功能限制,也常用于软件的试用场景。

主要实现思路就是在服务端生成密钥对及证书,在客户端启动或访问过程中进行验证。

本文实现的是通过IP地址、MAC地址、CPU序列号、主板序列号(或BIOS序列号)等信息生成license证书,同时可以设置生效及失效时间,控制项目到期后不可用。

作为记录的同时也希望能帮助到需要的朋友。

实现流程

  1. 服务端生成密钥对: 私钥自己保存用于第二步的证书生成,公钥暴露给客户端
  2. 根据配置生成证书: 使用私钥对服务器配置信息、生效起止时间等数据进行加密后生成证书
  3. 客户端应用中安装证书
  4. 客户端收到请求时利用公钥验证证书有效性

1. 生成密钥对

新建文件夹,并在文件夹下打开cmd命令行,执行如下命令:

# 生成密钥对
keytool -genkey -keysize 1024 -keyalg DSA -validity 3650 -alias "privateKey" -keystore "privateKeys.keystore" -storepass "xxxxxx" -keypass "xxxxxxx" -dname "CN=cnhqd, OU=cnhqd, O=cnhqd, L=XA, ST=SX, C=CN"

keytool -exportcert -alias "privateKey" -keystore "privateKeys.keystore" -storepass "xxxxxx" -file "certfile.cer"
keytool -import -alias "publicCert" -file "certfile.cer" -keystore "publicCerts.keystore" -storepass "xxxxxx"

参数说明:

keysize 密钥长度
keyalg  加密方式 
validity 私钥的有效期(单位:天)
alias 私钥别称
keystore 指定私钥库文件的名称 (生成在当前目录)
storepass 指定私钥库的密码 (keystore 文件存储密码)
keypass 指定别名条目的密码 (私钥加解密密码)
dname 证书个人信息
CN 为你的姓名
OU 为你的组织单位名称
O 为你的组织名称
L 为你所在的城市名称
ST 为你所在的省份名称
C 为你的国家名称 或 区号

上述命令执行完成后会在当前目录生成三个文件:

  • certfile.cer 认证证书文件,暂时无用
  • privateKeys.keystore 私钥文件,自己保存
  • publicKeys.keystore 公钥文件,需要放到客户端项目目录里

2. 生成客户端证书

这里我们新建了一个springboot + maven的项目,通过swagger生成证书。

整体结构如下:

服务端整体结构

使用的软件版本:

软件名称版本
springboot2.7.13
jdk1.8
maven3.5
swagger2.9.2
knife4j2.0.2
truelicense1.33

关键类及功能说明:

1、AbstractServerInfos: 用于获取服务端信息,包含IP地址、mac地址、cpu序列号、主板或BIOS序列号等信息,具体实现分windows和linux

2、LicenseCreator: 生成证书的主类

3、AdditionInfo: 需要额外校验的信息实体类,属性可删减可扩展

2.1 新建springboot项目, 并引入依赖

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.13</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zjtx.tech.license</groupId>
    <artifactId>license-server</artifactId>
    <version>1.0.0</version>
    <name>license-server</name>
    <description>license-server</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>
        <!--引入swagger2-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!--开启knife4j-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>
        <!--生成license-->
        <dependency>
            <groupId>de.schlichtherle.truelicense</groupId>
            <artifactId>truelicense-core</artifactId>
            <version>1.33</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <finalName>license-service</finalName>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <testFailureIgnore>true</testFailureIgnore>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.tomcat.maven</groupId>
                <artifactId>tomcat7-maven-plugin</artifactId>
                <version>2.2</version>
                <configuration>
                    <port>8090</port>
                    <path>/</path>
                    <uriEncoding>UTF-8</uriEncoding>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

2.2 新建LicenseServerApplication类

package com.zjtx.tech.license.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LicenseServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(LicenseServerApplication.class, args);
    }

}

2.3 新建config包,添加swagger配置类

SwaggerConfig.java

package com.zjtx.tech.license.server.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
@EnableKnife4j
public class SwaggerConfig implements InitializingBean {

    @Value("${swagger.host:{null}}")
    private String host;

    @Value("${swagger.title:{null}}")
    private String title;

    @Value("${swagger.version:{null}}")
    private String version;

    @Value("${swagger.description:{null}}")
    private String description;

    @Value("${swagger.license:{null}}")
    private String license;

    @Value("${swagger.licenseUrl:{null}}")
    private String licenseUrl;

    @Value("${swagger.contact.name:{null}}")
    private String contactName;

    @Value("${swagger.contact.email:{null}}")
    private String contactEmail;

    @Value("${swagger.contact.url:{null}}")
    private String contactUrl;

    @Value("${swagger.package:{null}}")
    private String packagePath;

    //配置swagger的Docket的bean实例
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(packagePath))
                .build().host(host);
    }

    private ApiInfo apiInfo() {

        //作者信息
        Contact contact = new Contact(contactName, contactUrl, contactEmail);
        return new ApiInfo(title,description,version,
                contactUrl,contact,license,licenseUrl,new ArrayList<>());
    }

    @Override
    public void afterPropertiesSet() {
        System.out.println("SwaggerConfig==================afterPropertiesSet");
    }
}

2.4 新建entity包,添加需要用到的实体类

LicenseCreatorParam.java

package com.zjtx.tech.license.server.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;

@Data
@ApiModel("生成证书实体类")
public class LicenseCreatorParam implements Serializable {

    private static final long serialVersionUID = 2832129012982731724L;

    @ApiModelProperty("证书subject")
    private String subject;

    @ApiModelProperty("密钥别称")
    private String privateAlias;

    @ApiModelProperty("密钥密码")
    private String keyPass;

    @ApiModelProperty("访问秘钥库的密码")
    private String storePass;

    @ApiModelProperty("证书生成路径")
    private String licensePath;

    @ApiModelProperty("密钥库存储路径")
    private String privateKeysStorePath;

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty("证书生效时间")
    private Date issuedTime = new Date();

    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @ApiModelProperty("证书失效时间")
    private Date expireTime;

    @ApiModelProperty("用户类型")
    private String consumerType = "user";

    @ApiModelProperty("用户数量")
    private Integer consumerAmount = 1;

    @ApiModelProperty("描述信息")
    private String description = "";

    @ApiModelProperty("额外的服务器硬件校验信息")
    private AdditionInfo additionInfo;

} 	

AdditionInfo.java

package com.zjtx.tech.license.server.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.List;

@Data
@ApiModel("服务器硬件信息实体类")
public class AdditionInfo implements Serializable {

    private static final long serialVersionUID = -2314678441082223148L;

    @ApiModelProperty("可被允许的IP地址")
    private List<String> ipAddress;

    @ApiModelProperty("可被允许的MAC地址")
    private List<String> macAddress;

    @ApiModelProperty("可被允许的CPU序列号")
    private String cpuSerial;

    @ApiModelProperty("可被允许的主板序列号")
    private String mainBoardSerial;

}

2.5 添加serverinfo包 存放获取服务器信息相关的类

AbstractServerInfos.java

package com.zjtx.tech.license.server.serverinfo;

import com.zjtx.tech.license.server.entity.AdditionInfo;
import lombok.extern.slf4j.Slf4j;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
public abstract class AbstractServerInfos {

    public AdditionInfo getServerInfos() {
        AdditionInfo result = new AdditionInfo();

        try {
            result.setIpAddress(this.getIpAddress());
            result.setMacAddress(this.getMacAddress());
            result.setCpuSerial(this.getCPUSerial());
            result.setMainBoardSerial(this.getMainBoardSerial());
        } catch (Exception e) {
            log.error("获取服务器硬件信息失败", e);
        }

        return result;
    }

    protected List<String> getIpAddress() throws Exception{
        List<String> result = null;

        //获取所有网络接口
        List<InetAddress> inetAddresses = getLocalAllInetAddress();

        if(inetAddresses != null && inetAddresses.size() > 0){
            result = inetAddresses.stream().map(InetAddress::getHostAddress).distinct().map(String::toLowerCase).collect(Collectors.toList());
        }

        return result;
    }

    protected List<String> getMacAddress() throws Exception{
        List<String> result = null;

        //1. 获取所有网络接口
        List<InetAddress> inetAddresses = getLocalAllInetAddress();

        if(inetAddresses != null && inetAddresses.size() > 0){
            //2. 获取所有网络接口的Mac地址
            result = inetAddresses.stream().map(this::getMacByInetAddress).distinct().collect(Collectors.toList());
        }

        return result;
    }

    protected abstract String getCPUSerial() throws Exception;

    protected abstract String getMainBoardSerial() throws Exception;

    protected List<InetAddress> getLocalAllInetAddress() throws Exception {
        List<InetAddress> result = new ArrayList<>(4);

        // 遍历所有的网络接口
        for (Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces(); networkInterfaces.hasMoreElements(); ) {
            NetworkInterface iface = networkInterfaces.nextElement();
            // 在所有的接口下再遍历IP
            for (Enumeration<InetAddress> inetAddresses = iface.getInetAddresses(); inetAddresses.hasMoreElements(); ) {
                InetAddress inetAddr = inetAddresses.nextElement();

                //排除LoopbackAddress、SiteLocalAddress、LinkLocalAddress、MulticastAddress类型的IP地址
                if (!inetAddr.isLoopbackAddress() /*&& !inetAddr.isSiteLocalAddress()*/
                        && !inetAddr.isLinkLocalAddress() && !inetAddr.isMulticastAddress()) {
                    result.add(inetAddr);
                }
            }
        }
        return result;
    }

    protected String getMacByInetAddress(InetAddress inetAddr) {
        try {
            byte[] mac = NetworkInterface.getByInetAddress(inetAddr).getHardwareAddress();
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < mac.length; i++) {
                if (i != 0) {
                    sb.append("-");
                }
                //将十六进制byte转化为字符串
                String temp = Integer.toHexString(mac[i] & 0xff);
                if (temp.length() == 1) {
                    sb.append("0").append(temp);
                } else {
                    sb.append(temp);
                }
            }
            return sb.toString().toUpperCase();
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }
}

WindowsServerInfos.java

package com.zjtx.tech.license.server.serverinfo;

import org.apache.commons.lang3.StringUtils;

import java.util.Scanner;

public class WindowsServerInfos extends AbstractServerInfos {

        @Override
        protected String getCPUSerial() throws Exception {
            //序列号
            String serialNumber = "";
            //使用WMIC获取CPU序列号
            Process process = Runtime.getRuntime().exec("wmic cpu get processorid");
            process.getOutputStream().close();
            Scanner scanner = new Scanner(process.getInputStream());
            if(scanner.hasNext()){
                scanner.next();
            }
            if(scanner.hasNext()){
                serialNumber = scanner.next().trim();
            }
            scanner.close();
            return serialNumber;
        }

        @Override
        protected String getMainBoardSerial() throws Exception {
            String serialNumber = getSysInfo("wmic baseboard get serialnumber");
            if(StringUtils.isEmpty(serialNumber)) {
                //如果获取不到主板序列号就获取bios序列号
                serialNumber = getSysInfo("wmic bios get serialnumber");
            }
            return serialNumber;
        }

        /**
         * 通过命令行获取系统信息
         * @param command 查询信息的命令
         * @return 系统对应数据
         * @throws Exception 异常信息
         */
        private String getSysInfo(String command) throws Exception {
            String serialNumber = "";
            Process process = Runtime.getRuntime().exec(command);
            process.getOutputStream().close();
            Scanner scanner = new Scanner(process.getInputStream());
            if(scanner.hasNext()){
                scanner.next();
            }
            if(scanner.hasNext()){
                serialNumber = scanner.next().trim();
            }
            scanner.close();
            return serialNumber;
        }
}

LinuxServerInfos.java

package com.zjtx.tech.license.server.serverinfo;

import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class LinuxServerInfos extends AbstractServerInfos {

    @Override
    protected String getCPUSerial() throws Exception {
        //序列号
        String serialNumber = "";

        //使用dmidecode命令获取CPU序列号
        String[] shell = {"/bin/bash","-c","dmidecode -t processor | grep 'ID' | awk -F ':' '{print $2}' | head -n 1"};
        Process process = Runtime.getRuntime().exec(shell);
        process.getOutputStream().close();

        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line = reader.readLine().trim();
        if(StringUtils.isNotBlank(line)){
            serialNumber = line;
        }

        reader.close();
        return serialNumber;
    }

    @Override
    protected String getMainBoardSerial() throws Exception {
        //序列号
        String serialNumber = "";

        //使用dmidecode命令获取主板序列号
        String[] shell = {"/bin/bash","-c","dmidecode | grep 'Serial Number' | awk -F ':' '{print $2}' | head -n 1"};
        Process process = Runtime.getRuntime().exec(shell);
        process.getOutputStream().close();

        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));

        String line = reader.readLine().trim();
        if(StringUtils.isNotBlank(line)){
            serialNumber = line;
        }

        reader.close();
        return serialNumber;
    }
}

CustomKeyStoreParam.java

package com.zjtx.tech.license.server.serverinfo;

import de.schlichtherle.license.AbstractKeyStoreParam;

import java.io.*;
import java.nio.file.Files;

public class CustomKeyStoreParam extends AbstractKeyStoreParam {

    /**
     * 公钥/私钥在磁盘上的存储路径
     */
    private final String storePath;
    private final String alias;
    private final String storePwd;
    private final String keyPwd;

    public CustomKeyStoreParam(Class clazz, String resource, String alias, String storePwd, String keyPwd) {
        super(clazz, resource);
        this.storePath = resource;
        this.alias = alias;
        this.storePwd = storePwd;
        this.keyPwd = keyPwd;
    }

    @Override
    public String getAlias() {
        return alias;
    }

    @Override
    public String getStorePwd() {
        return storePwd;
    }

    @Override
    public String getKeyPwd() {
        return keyPwd;
    }

    @Override
    public InputStream getStream() throws IOException {
        return Files.newInputStream(new File(storePath).toPath());
    }
}

2.6 新建util包 存放生成证书的工具类

LicenseCreator.java

package com.zjtx.tech.license.server.util;

import com.zjtx.tech.license.server.entity.LicenseCreatorParam;
import com.zjtx.tech.license.server.serverinfo.CustomKeyStoreParam;
import de.schlichtherle.license.*;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import javax.security.auth.x500.X500Principal;
import java.io.File;
import java.text.MessageFormat;
import java.util.prefs.Preferences;

@Data
@Slf4j
public class LicenseCreator {

    private final static X500Principal DEFAULT_HOLDER_AND_ISSUER = new X500Principal("CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN");

    private LicenseCreatorParam param;

    public LicenseCreator(LicenseCreatorParam param) {
        this.param = param;
    }

    /**
     * 生成License证书
     *
     * @return boolean
     */
    public boolean generateLicense() {
        try {
            LicenseManager licenseManager = new LicenseManager(initLicenseParam());
            LicenseContent licenseContent = initLicenseContent();

            licenseManager.store(licenseContent, new File(param.getLicensePath()));

            return true;
        } catch (Exception e) {
            log.error(MessageFormat.format("证书生成失败:{0}", param), e);
            return false;
        }
    }

    /**
     * 初始化证书生成参数
     *
     * @return de.schlichtherle.license.LicenseParam
     */
    private LicenseParam initLicenseParam() {
        Preferences preferences = Preferences.userNodeForPackage(LicenseCreator.class);
        //设置对证书内容加密的秘钥
        CipherParam cipherParam = new DefaultCipherParam(param.getStorePass());
        KeyStoreParam privateStoreParam = new CustomKeyStoreParam(LicenseCreator.class,
                param.getPrivateKeysStorePath(), param.getPrivateAlias(),
                param.getStorePass(), param.getKeyPass());
        return new DefaultLicenseParam(param.getSubject(), preferences,
                privateStoreParam, cipherParam);
    }

    /**
     * 设置证书生成正文信息
     *
     * @return de.schlichtherle.license.LicenseContent
     */
    private LicenseContent initLicenseContent() {
        LicenseContent licenseContent = new LicenseContent();
        licenseContent.setHolder(DEFAULT_HOLDER_AND_ISSUER);
        licenseContent.setIssuer(DEFAULT_HOLDER_AND_ISSUER);

        licenseContent.setSubject(param.getSubject());
        licenseContent.setIssued(param.getIssuedTime());
        licenseContent.setNotBefore(param.getIssuedTime());
        licenseContent.setNotAfter(param.getExpireTime());
        licenseContent.setConsumerType(param.getConsumerType());
        licenseContent.setConsumerAmount(param.getConsumerAmount());
        licenseContent.setInfo(param.getDescription());

        //扩展校验服务器硬件信息
        licenseContent.setExtra(param.getAdditionInfo());

        return licenseContent;
    }
}

2.7 新建controller包 放置controller类

LicenseController.java

package com.zjtx.tech.license.server.controller;

import com.zjtx.tech.license.server.entity.AdditionInfo;
import com.zjtx.tech.license.server.entity.LicenseCreatorParam;
import com.zjtx.tech.license.server.util.LicenseCreator;
import com.zjtx.tech.license.server.serverinfo.AbstractServerInfos;
import com.zjtx.tech.license.server.serverinfo.LinuxServerInfos;
import com.zjtx.tech.license.server.serverinfo.WindowsServerInfos;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

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

@RestController
@Api(tags = "license证书")
@RequestMapping("/license")
public class LicenseController {

    /**
     * 证书生成路径
     */
    @Value("${license.licensePath}")
    private String licensePath;

    @ApiOperation("获取服务器硬件信息")
    @GetMapping("/getServerInfos/{osName}")
    public AdditionInfo getServerInfos(@PathVariable String osName) {
        //操作系统类型
        if (StringUtils.isBlank(osName)) {
            osName = System.getProperty("os.name");
        }
        osName = osName.toLowerCase();

        AbstractServerInfos abstractServerInfos;

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

        return abstractServerInfos.getServerInfos();
    }

    @ApiOperation("生成证书")
    @PostMapping("/generateLicense")
    public Map<String, Object> generateLicense(@RequestBody 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;
    }
}

2.8 配置文件

application.yml

server:
  port: 8090
  servlet:
    context-path: /api

license: 
  # 生成证书的目录 根据实际情况调整
  licensePath: D:/study/license/licensefile/license.lic

##swagger
swagger:
  package: com.zjtx.tech.license.server.controller
  host: 127.0.0.1:8090
  title: License-Server服务端
  version: v1.0
  description: API接口文档
  license: License
  licenseUrl: https://blog.zjtx.vip
  contact:
    name: 泽济天下
    url: https://blog.zjtx.vip
    email: xxxx@qq.com

logging:
  level:
    com.zjtx.tech.license: debug

#解决高版本SpringBoot整合Swagger 启动报错Failed to start bean ‘documentationPluginsBootstrapper‘ 问题
#参考https://blog.csdn.net/weixin_39792935/article/details/122215625
spring:
  mvc:
    path match:
      matching-strategy: ant_path_matcher

2.9 生成证书及测试

浏览器访问http://localhost:8090/api/doc.html

2.9.1 获取服务端信息

获取服务端硬件信息

2.9.2 生成证书

生成证书配置

截图中的参数配置:

{
	"consumerAmount": 1,
	"consumerType": "user",
	"description": "授权给xxx的证书,时长1年",
	"expiryTime": "2024-11-15 23:59:59",
	"issuedTime": "2023-11-15 00:00:00",
	"keyPass": "cnhqd@20231114",
	"additionInfo": {
      //需要把这部分内容替换成上一步获取到的服务器信息
      "ipAddress": [
        "172.16.60.104"
      ],
      "macAddress": [
        "6C-4B-90-C6-01-AB"
      ],
      "cpuSerial": "BFEBFBFF000906EA",
      "mainBoardSerial": "P509PEZ0"
    },
	"licensePath": "D:/study/license/licensefile/license.lic",
	"privateAlias": "privateKey",
	"privateKeysStorePath": "D:/study/license/licensefile/privateKeys.keystore",
	"storePass": "xxxxxx",
	"subject": "license_xxx"
}

参数配置完成后点击发送会在licensePath对应目录下生成lic后缀的文件,就是需要放在客户端项目目录中的文件。

参数配置说明:

生成证书参数配置

至此,公钥和客户端证书就生成完成了。

3.客户端项目及相关配置

说明: 客户端中使用的框架及版本与服务端一致,代码中部分类与服务端一致(见下图),就不再列出代码。

整体项目结构如下:

客户端项目结构

3.1 新建项目并引入依赖

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.13</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.zjtx.tech.license</groupId>
    <artifactId>license-client</artifactId>
    <version>1.0.0</version>
    <name>license-client</name>
    <description>license-client</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--引入swagger2-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!--开启knife4j-->
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>de.schlichtherle.truelicense</groupId>
            <artifactId>truelicense-core</artifactId>
            <version>1.33</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.7</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <!-- 引入缓存用于记录证书认证结果 --!>
        <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
            <version>2.9.3</version>
        </dependency>
    </dependencies>
</project>

3.2 新建LicenseClientApplication类

LicenseClientApplication.java

package com.zjtx.tech.license.client;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class LicenseClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(LicenseClientApplication.class, args);
    }
}

3.3 新建util包 用于存放安装和验证证书相关的类

CustomLicenseManager.java

package com.zjtx.tech.license.client.util;

import com.zjtx.tech.license.client.serverinfo.AbstractServerInfos;
import com.zjtx.tech.license.client.serverinfo.LinuxServerInfos;
import com.zjtx.tech.license.client.serverinfo.WindowsServerInfos;
import com.zjtx.tech.license.server.entity.AdditionInfo;
import de.schlichtherle.license.*;
import de.schlichtherle.xml.GenericCertificate;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

@Data
@EqualsAndHashCode(callSuper = true)
@Slf4j
public class CustomLicenseManager extends LicenseManager {

    //XML编码
    private static final String XML_CHARSET = "UTF-8";
    //默认buffer size
    private static final int DEFAULT_BUFFER_SIZE = 8 * 1024;

    public CustomLicenseManager(LicenseParam param) {
        super(param);
    }

    @Override
    protected synchronized byte[] create(LicenseContent content,LicenseNotary notary)
            throws Exception {
        initialize(content);
        this.validateCreate(content);
        final GenericCertificate certificate = notary.sign(content);
        return getPrivacyGuard().cert2key(certificate);
    }

    @Override
    protected synchronized LicenseContent install(final byte[] key, final LicenseNotary notary)
            throws Exception {
        final GenericCertificate certificate = getPrivacyGuard().key2cert(key);
        notary.verify(certificate);
        final LicenseContent content = (LicenseContent) this.load(certificate.getEncoded());
        this.validate(content);
        setLicenseKey(key);
        setCertificate(certificate);
        return content;
    }

    @Override
    protected synchronized LicenseContent verify(final LicenseNotary notary)
            throws Exception {
        // Load license key from preferences,
        final byte[] key = getLicenseKey();
        if (null == key) {
            throw new NoLicenseInstalledException(getLicenseParam().getSubject());
        }
        GenericCertificate certificate = getPrivacyGuard().key2cert(key);
        notary.verify(certificate);
        final LicenseContent content = (LicenseContent) this.load(certificate.getEncoded());
        this.validate(content);
        setCertificate(certificate);

        return content;
    }

    protected synchronized void validateCreate(final LicenseContent content)
            throws LicenseContentException {
        final Date now = new Date();
        final Date notBefore = content.getNotBefore();
        final Date notAfter = content.getNotAfter();
        if (null != notAfter && now.after(notAfter)) {
            throw new LicenseContentException("证书失效时间不能早于当前时间");
        }
        if (null != notBefore && null != notAfter && notAfter.before(notBefore)) {
            throw new LicenseContentException("证书生效时间不能晚于证书失效时间");
        }
        final String consumerType = content.getConsumerType();
        if (null == consumerType) {
            throw new LicenseContentException("用户类型不能为空");
        }
    }

    @Override
    protected synchronized void validate(final LicenseContent content)
            throws LicenseContentException {
        //1. 首先调用父类的validate方法
        super.validate(content);
        //2. 然后校验自定义的License参数
        //License中可被允许的参数信息
        AdditionInfo expectedCheckModel = (AdditionInfo) content.getExtra();
        //当前服务器真实的参数信息
        AdditionInfo serverCheckModel = getServerInfos();
        //有一个对象为空 直接抛异常
        if (expectedCheckModel == null || serverCheckModel == null) {
            throw new LicenseContentException("不能获取服务器硬件信息");
        }
        //校验IP地址
        if (ipAddressInvalid(expectedCheckModel.getIpAddress(), serverCheckModel.getIpAddress())) {
            throw new LicenseContentException("当前服务器的IP没在授权范围内");
        }
        //校验Mac地址
        if (ipAddressInvalid(expectedCheckModel.getMacAddress(), serverCheckModel.getMacAddress())) {
            throw new LicenseContentException("当前服务器的Mac地址没在授权范围内");
        }
        //校验主板序列号
        if (serialInvalid(expectedCheckModel.getMainBoardSerial(), serverCheckModel.getMainBoardSerial())) {
            throw new LicenseContentException("当前服务器的主板序列号没在授权范围内");
        }
        //校验CPU序列号
        if (serialInvalid(expectedCheckModel.getCpuSerial(), serverCheckModel.getCpuSerial())) {
            throw new LicenseContentException("当前服务器的CPU序列号没在授权范围内");
        }
    }

    private Object load(String encoded) {
        BufferedInputStream inputStream = null;
        XMLDecoder decoder = null;
        try {
            inputStream = new BufferedInputStream(new ByteArrayInputStream(encoded.getBytes(XML_CHARSET)));
            decoder = new XMLDecoder(new BufferedInputStream(inputStream, DEFAULT_BUFFER_SIZE), null, null);
            return decoder.readObject();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } finally {
            try {
                if (decoder != null) {
                    decoder.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                log.error("XMLDecoder解析XML失败", e);
            }
        }
        return null;
    }

    private AdditionInfo getServerInfos() {
        //操作系统类型
        String osName = System.getProperty("os.name").toLowerCase();
        AbstractServerInfos abstractServerInfos;

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

    private boolean ipAddressInvalid(List<String> expectedList, List<String> serverList) {
        if(CollectionUtils.isEmpty(expectedList) || CollectionUtils.isEmpty(serverList)) {
            return true;
        }
        for (String expected : expectedList) {
            if (serverList.contains(expected.trim())) {
                return false;
            }
        }
        return true;
    }

    private boolean serialInvalid(String expectedSerial, String serverSerial) {
        //为空或者不相等 就认为无效
        return StringUtils.isBlank(expectedSerial) || StringUtils.isBlank(serverSerial)
                || !expectedSerial.equals(serverSerial);
    }
}

LicenseVerify.java 证书安装与验证

package com.zjtx.tech.license.client.util;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.zjtx.tech.license.server.entity.LicenseVerifyParam;
import com.zjtx.tech.license.client.serverinfo.CustomKeyStoreParam;
import de.schlichtherle.license.*;
import lombok.extern.slf4j.Slf4j;

import java.text.DateFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.concurrent.TimeUnit;
import java.util.prefs.Preferences;

@Slf4j
public class LicenseVerify {

    private static final String CACHE_KEY_LICENSE_CHECK_RESULT = "license_check_result";

    private static final Cache<Object, Object> cache = Caffeine.newBuilder()
            //初始数量
            .initialCapacity(10)
            //最大条数
            .maximumSize(10)
            //PS:expireAfterWrite和expireAfterAccess同时存在时,以expireAfterWrite为准。
            //最后一次写操作后经过指定时间过期
            .expireAfterWrite(3, TimeUnit.MINUTES)
            //监听缓存被移除
            .removalListener((key, val, removalCause) -> { })
            //记录命中
            .recordStats()
            .build();

    /**
     * 安装License证书
     */
    public synchronized void install(LicenseVerifyParam param){
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //1. 安装证书
        try{
            LicenseManager licenseManager = LicenseManagerHolder.getInstance(initLicenseParam(param));
            licenseManager.uninstall();
            LicenseContent result = licenseManager.install(FileUtils.getFile(param.getLicensePath()));
            log.info(MessageFormat.format("证书安装成功,证书有效期:{0} - {1}",format.format(result.getNotBefore()),format.format(result.getNotAfter())));
        }catch (Exception e){
            log.error("证书安装失败!",e);
        }
    }

    /**
     * 校验License证书
     * @return boolean
     */
    public boolean verify(){
        Object cacheVal = cache.getIfPresent(CACHE_KEY_LICENSE_CHECK_RESULT);
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if( cacheVal != null) {
            log.info("读取到缓存数据........");
            LicenseContent licenseContent = (LicenseContent) cacheVal;
            log.info(MessageFormat.format("证书校验通过,证书有效期:{0} - {1}",format.format(licenseContent.getNotBefore()),format.format(licenseContent.getNotAfter())));
            return true;
        }
        LicenseManager licenseManager = LicenseManagerHolder.getInstance(null);
        //2. 校验证书
        try {
            LicenseContent licenseContent = licenseManager.verify();
            cache.put(CACHE_KEY_LICENSE_CHECK_RESULT, licenseContent);
            log.info(MessageFormat.format("证书校验通过,证书有效期:{0} - {1}",format.format(licenseContent.getNotBefore()),format.format(licenseContent.getNotAfter())));
            return true;
        }catch (Exception e){
            log.error("证书校验失败!",e);
            return false;
        }
    }

    /**
     * 初始化证书生成参数
     * @param param License校验类需要的参数
     * @return de.schlichtherle.license.LicenseParam
     */
    private LicenseParam initLicenseParam(LicenseVerifyParam param){
        Preferences preferences = Preferences.userNodeForPackage(LicenseVerify.class);
        CipherParam cipherParam = new DefaultCipherParam(param.getStorePass());
        KeyStoreParam publicStoreParam = new CustomKeyStoreParam(LicenseVerify.class,
                param.getPublicKeysStorePath(),param.getPublicAlias(),param.getStorePass(),null);
        return new DefaultLicenseParam(param.getSubject(),preferences,publicStoreParam,cipherParam);
    }
}

LicenseManagerHolder.java 一个单例实现类

package com.zjtx.tech.license.client.util;

import de.schlichtherle.license.LicenseManager;
import de.schlichtherle.license.LicenseParam;

public class LicenseManagerHolder {

    private static volatile LicenseManager LICENSE_MANAGER;

    public static LicenseManager getInstance(LicenseParam param){
        if(LICENSE_MANAGER == null){
            synchronized (LicenseManagerHolder.class){
                if(LICENSE_MANAGER == null){
                    LICENSE_MANAGER = new CustomLicenseManager(param);
                }
            }
        }
        return LICENSE_MANAGER;
    }
}

FileUtils.java 文件及文件流获取工具类

package com.zjtx.tech.license.client.util;

import org.apache.commons.lang3.StringUtils;
import org.springframework.core.io.ClassPathResource;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

public class FileUtils {

    private static final String CLASSPATH_RESOURCE_PREFIX = "classpath:";

    public static File getFile(String path) throws IOException {
        if(StringUtils.isEmpty(path)) {
            throw new FileNotFoundException("未找到文件路径");
        }
        if(path.startsWith(CLASSPATH_RESOURCE_PREFIX)) {
            return new ClassPathResource(path.replaceAll(CLASSPATH_RESOURCE_PREFIX, "")).getFile();
        }
        return new File(path);
    }

    public static InputStream getInputStream(String path) throws IOException {
        if(StringUtils.isEmpty(path)) {
            throw new FileNotFoundException("未找到文件路径");
        }
        if(path.startsWith(CLASSPATH_RESOURCE_PREFIX)) {
            return new ClassPathResource(path.replaceAll(CLASSPATH_RESOURCE_PREFIX, "")).getInputStream();
        }
        return Files.newInputStream(Paths.get(path));
    }

}

3.4 新建config包 用于存放拦截器等相关配置类

LicenseCheckListener.java 用于应用启动后安装证书

package com.zjtx.tech.license.client.config;

import com.zjtx.tech.license.client.util.LicenseVerify;
import com.zjtx.tech.license.server.entity.LicenseVerifyParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class LicenseCheckListener implements ApplicationListener<ContextRefreshedEvent> {

    /**
     * 证书subject
     */
    @Value("${license.subject}")
    private String subject;

    /**
     * 公钥别称
     */
    @Value("${license.publicAlias}")
    private String publicAlias;

    /**
     * 访问公钥库的密码
     */
    @Value("${license.storePass}")
    private String storePass;

    /**
     * 证书生成路径
     */
    @Value("${license.licensePath}")
    private String licensePath;

    /**
     * 密钥库存储路径
     */
    @Value("${license.publicKeysStorePath}")
    private String publicKeysStorePath;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        //root application context 没有parent
        ApplicationContext context = event.getApplicationContext().getParent();
        if (context == null) {
            if (StringUtils.isNotBlank(licensePath)) {
                log.info("++++++++ 开始安装证书 ++++++++");
                LicenseVerifyParam param = new LicenseVerifyParam();
                param.setSubject(subject);
                param.setPublicAlias(publicAlias);
                param.setStorePass(storePass);
                param.setLicensePath(licensePath);
                param.setPublicKeysStorePath(publicKeysStorePath);
                LicenseVerify licenseVerify = new LicenseVerify();
                //安装证书
                licenseVerify.install(param);
                log.info("++++++++ 证书安装结束 ++++++++");
            }
        }
    }
}

LicenseCheckInterceptor.java 证书校验拦截器 在controller之前执行

package com.zjtx.tech.license.client.config;

import com.alibaba.fastjson.JSON;
import com.zjtx.tech.license.client.util.LicenseVerify;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Component
public class LicenseCheckInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("进入拦截器,验证证书可使用性");

        LicenseVerify licenseVerify = new LicenseVerify();

        //校验证书是否有效
        boolean verifyResult = licenseVerify.verify();

        if (verifyResult) {
            log.info("验证成功,证书可用");
            return true;
        } else {
            log.info("验证失败,证书无效");
            response.setContentType("application/json;charset=utf-8");
            Map<String, String> result = new HashMap<>(1);
            result.put("result", "您的证书无效,请核查服务器是否取得授权或重新申请证书!");

            response.getWriter().write(JSON.toJSONString(result));

            return false;
        }
    }

}

InterceptorConfig.java 安全配置类, 不同版本可能写法不同,自行修改即可

package com.zjtx.tech.license.client.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Resource
    private LicenseCheckInterceptor licenseCheckInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        //添加要拦截的url
        registry.addInterceptor(licenseCheckInterceptor)
                // 拦截的路径
                .addPathPatterns("/**");
    }

}

3.5 新建controller包 用于存放测试用的controller

LoginController.java

package com.zjtx.tech.license.client.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
@Api(tags = "测试")
@RequestMapping("/text")
public class LoginController {

    @ApiOperation("登录")
    @GetMapping("/login")
    public Map<String, Object> test() {
        Map<String, Object> result = new HashMap<>(1);
        result.put("code", 200);
        result.put("mes", "登录成功");
        return result;

    }
}

3.6 配置文件

server:
  port: 8099
  servlet:
    context-path: /api

license:
  basePath: D:/study/license/licensefile
  subject: license_cnhqd
  publicAlias: publicCert
  storePass: cnhqd@20231114
  licensePath: classpath:license/license.lic
  publicKeysStorePath: classpath:license/publicCerts.keystore
  uploadPath: classpath:license/licensefile/

##swagger
swagger:
  package: com.zjtx.tech.license.client.controller
  host: 127.0.0.1:8090
  title: License-Client客户端
  version: v1.0
  description: API接口文档
  license: License
  licenseUrl: https://blog.zjtx.vip
  contact:
    name: 泽济天下
    url: https://blog.zjtx.vip
    email: xxxx@qq.com

logging:
  level:
    com.zjtx.tech.license: debug

#解决高版本SpringBoot整合Swagger 启动报错Failed to start bean ‘documentationPluginsBootstrapper‘ 问题
#参考https://blog.csdn.net/weixin_39792935/article/details/122215625
spring:
  mvc:
    path match:
      matching-strategy: ant_path_matcher

注意: 不要忘了把lic文件和publicKeys.keystore文件放到resources/license目录下,这个非常重要。

3.7 运行及访问测试

启动项目,可以看到如下日志输出即为安装完成并正常启动:

项目启动日志

浏览器访问http://localhost:8099/doc.html,可以看到如下日志输出:

证书校验日志
可以看到可以正常验证通过,同时在上述代码里加入了缓存用于保存证书认证结果,有效期设置的是5分钟。

如果证书过期,访问接口会提示证书无效或过期的异常, 可自行测试。

4. 问题记录与解决

  1. 高版本springboot与swagger2的兼容性问题

异常信息:

2023-11-17 08:42:31.487 ERROR 12164 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
 at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.28.jar:5.3.28]
 at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.28.jar:5.3.28]
 at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.28.jar:5.3.28]
 at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_181]
 at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.28.jar:5.3.28]
 at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.28.jar:5.3.28]
 at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:937) ~[spring-context-5.3.28.jar:5.3.28]
 at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.28.jar:5.3.28]
 at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147) ~[spring-boot-2.7.13.jar:2.7.13]
 at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731) [spring-boot-2.7.13.jar:2.7.13]
 at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408) [spring-boot-2.7.13.jar:2.7.13]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:307) [spring-boot-2.7.13.jar:2.7.13]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303) [spring-boot-2.7.13.jar:2.7.13]
 at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292) [spring-boot-2.7.13.jar:2.7.13]
 at com.zjtx.tech.license.client.LicenseClientApplication.main(LicenseClientApplication.java:10) [classes/:na]
Caused by: java.lang.NullPointerException: null
 at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:112) ~[springfox-spi-2.9.2.jar:null]
 at springfox.documentation.spi.service.contexts.Orderings$8.compare(Orderings.java:109) ~[springfox-spi-2.9.2.jar:null]
 at com.google.common.collect.ComparatorOrdering.compare(ComparatorOrdering.java:37) ~[guava-20.0.jar:na]
 at java.util.TimSort.countRunAndMakeAscending(TimSort.java:355) ~[na:1.8.0_181]
 at java.util.TimSort.sort(TimSort.java:220) ~[na:1.8.0_181]
 at java.util.Arrays.sort(Arrays.java:1438) ~[na:1.8.0_181]
 at com.google.common.collect.Ordering.sortedCopy(Ordering.java:855) ~[guava-20.0.jar:na]
 at springfox.documentation.spring.web.plugins.WebMvcRequestHandlerProvider.requestHandlers(WebMvcRequestHandlerProvider.java:57) ~[springfox-spring-web-2.9.2.jar:null]
 at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:138) ~[springfox-spring-web-2.9.2.jar:null]
 at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper$2.apply(DocumentationPluginsBootstrapper.java:135) ~[springfox-spring-web-2.9.2.jar:null]
 at com.google.common.collect.Iterators$7.transform(Iterators.java:750) ~[guava-20.0.jar:na]
 at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~[guava-20.0.jar:na]
 at com.google.common.collect.TransformedIterator.next(TransformedIterator.java:47) ~[guava-20.0.jar:na]
 at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:52) ~[guava-20.0.jar:na]
 at com.google.common.collect.MultitransformedIterator.hasNext(MultitransformedIterator.java:50) ~[guava-20.0.jar:na]
 at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:249) ~[guava-20.0.jar:na]
 at com.google.common.collect.ImmutableList.copyOf(ImmutableList.java:209) ~[guava-20.0.jar:na]
 at com.google.common.collect.FluentIterable.toList(FluentIterable.java:614) ~[guava-20.0.jar:na]
 at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.defaultContextBuilder(DocumentationPluginsBootstrapper.java:111) ~[springfox-spring-web-2.9.2.jar:null]
 at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.buildContext(DocumentationPluginsBootstrapper.java:96) ~[springfox-spring-web-2.9.2.jar:null]
 at springfox.documentation.spring.web.plugins.DocumentationPluginsBootstrapper.start(DocumentationPluginsBootstrapper.java:167) ~[springfox-spring-web-2.9.2.jar:null]
 at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:178) ~[spring-context-5.3.28.jar:5.3.28]
 ... 14 common frames omitted

解决方案: application.yml文件中添加如下配置即可:

#解决高版本SpringBoot整合Swagger 启动报错Failed to start bean ‘documentationPluginsBootstrapper‘ 问题
#参考https://blog.csdn.net/weixin_39792935/article/details/122215625
spring:
mvc:
 path match:
   matching-strategy: ant_path_matcher
  1. 证书安装失败问题
    异常信息:

    2023-11-17 08:44:25.551  INFO 13640 --- [           main] s.d.s.w.s.ApiListingReferenceScanner     : Scanning for api listing references
    2023-11-17 08:44:25.691  INFO 13640 --- [           main] c.z.t.l.c.config.LicenseCheckListener    : ++++++++ 开始安装证书 ++++++++
    java.lang.ClassNotFoundException: com/zjtx/tech/license/server/entity/AdditionInfo
    Continuing ...
    java.lang.NullPointerException: target should not be null
    Continuing ...
    java.lang.IllegalStateException: The outer element does not return value
    Continuing ...
    java.lang.IllegalStateException: The outer element does not return value
    Continuing ...
    java.lang.IllegalStateException: The outer element does not return value
    Continuing ...
    java.lang.IllegalStateException: The outer element does not return value
    Continuing ...
    2023-11-17 08:44:28.578 ERROR 13640 --- [           main] c.z.t.license.client.util.LicenseVerify  : 证书安装失败!
    

    解决方案: 把AdditionInfo.java 和 LicenseVerifyParam.java 放到和服务端一样名字的包下即可。

​ 原因分析: 证书安装的时候会做序列化和反序列化,并且是通过xml的格式进行的,xml中包含了要解析到的目标类全名,如果不一致,就解析不到数据导致的错误。目前知道的就这一种方案,如有其他更好的方案欢迎评论区留言交流。

总结

本文主要记录了springboot应用实现license授权的过程,主要包含服务端证书生成、客户端证书安装及校验的相关配置过程。

目前暂时没在项目中使用,不过作为技术储备还是有一定价值的。

希望能帮助到需要的朋友。

针对以上内容有任何疑问或者建议欢迎留言评论交流~~~

创作不易,欢迎一键三连~~~

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1218695.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Vue数据绑定

在我们Vue当中有两种数据绑定的方法 1.单向绑定 2.双向绑定 让我为大家介绍一下吧&#xff01; 1、单向绑定(v-bind) 数据只能从data流向页面 举个例子&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"…

vue-组件生命周期+网络请求

​&#x1f308;个人主页&#xff1a;前端青山 &#x1f525;系列专栏&#xff1a;Vue篇 &#x1f516;人终将被年少不可得之物困其一生 依旧青山,本期给大家带来vue篇专栏内容:vue-组件生命周期网络请求 目录 组件生命周期 1. Vue的生命周期 2. Vue 子组件和父组件执行顺序…

“具有分布式能源资源的多个智能家庭的能源管理的联邦强化学习”文章学习三——基于联邦深度学习的多智能家居能源管理

一、系统描述 我们考虑一个基于FRL的HEMS&#xff0c;它由单个GS和N个LHEMS组成&#xff0c;如图2所示。如图2-C所示&#xff0c;FRL训练过程包括两个步骤&#xff1a;步骤1&#xff09;使用本地数据对LHEMS&#xff08;即本地神经网络的权重ωn&#xff09;进行本地模型的训练…

9步打造个人ip

什么是个人IP&#xff1f; 就是一个人创造出来的属于自己的有个性有价值的&#xff0c;能让他人记住你&#xff0c;信任你&#xff0c;认可你的东西。 如何强化个人IP呢&#xff1f; 需要一些必要的条件如专业性、耐心、勤奋等等要知道&#xff0c;打造IP是一个见效慢的过程&am…

MATLAB中zticks函数用法

目录 语法 说明 示例 指定 z 轴刻度值和标签 指定非均匀 z 轴刻度值 以 2 为增量递增 z 轴刻度值 将 z 轴刻度值设置回默认值 指定特定坐标区的 z 轴刻度值 删除 z 轴刻度线 zticks函数的功能是设置或查询 z 轴刻度值。 语法 zticks(ticks) zt zticks zticks(auto)…

Postman接收列表、数组参数@RequestParam List<String> ids

示例如下: 接口定义如下: GetMapping(value "/queryNewMoviePath")public List<Map<String, Object>> queryNewMoviePath(RequestParam List<String> ids ) {return service.queryNewMoviePath(ids);}postman中测试如下&#xff1a; http://loc…

计算机毕业设计项目选题推荐(免费领源码)java+mysql二手校园交易平台67613

目录 摘要 1 绪论 1.1 研究背景 1.2国内外研究现状 1.3论文结构与章节安排 2 二手校园交易平台系统分析 2.1 可行性分析 2.2 系统流程分析 2.2.1 数据流程 3.3.2 业务流程 2.3 系统功能分析 2.3.1 功能性分析 2.3.2 非功能性分析 2.4 系统用例分析 2.5本章小结 …

TensorRT基础知识及应用【学习笔记(十)】

这篇博客为修改过后的转载&#xff0c;因为没有转载链接&#xff0c;所以选了原创 文章目录 一、准备知识1.1 环境配置A. CUDA DriverB. CUDAC. cuDNND. TensorRT 1.2 编程模型 二、构建阶段2.1 创建网络定义2.2 配置参数2.3 生成Engine2.4 保存为模型文件2.5 释放资源 三、运…

C/C+=内存管理

C/C内存管理以及动态内存的申请_c动态内存的申请与释放_Demo Test的博客-CSDN博客 问题是&#xff0c;这个0x0804 8000 到0xC 0000 0000之间&#xff0c;不止3GB&#xff0c;应该有47GB&#xff0c;该怎么解释呢&#xff1f;

Azure的AI使用-(语言检测、图像分析、图像文本识别)

1.语言检测 安装包&#xff1a; # 语言检测 %pip install azure-ai-textanalytics5.2.0 需要用到密钥和资源的终结点&#xff0c;所以去Azure上创建资源&#xff0c;我这个是创建好的了然后点击密钥和终结者去拿到key和终结点 两个密钥选择哪个都行 语言检测代码示例&#…

obsidian和bookmaster

1 手动安装插件 插件地址&#xff1a;https://forum-zh.obsidian.md/t/topic/12333 安装file服务器 地址&#xff1a;http://www.rejetto.com/hfs/ hfs.exe可以改个端口 改成8866&#xff0c;ip地址也可以改成 127.0.0.1 # 因为安装到本地 如果要创建账户的话&#xff0c;就…

【PB续命05】WinHttp.WinHttpRequest的介绍与使用

0 WinHttp.WinHttpRequest简介 winhttp.winhttprequest是Windows操作系统中的一个API函数&#xff0c;用于创建和发送HTTP请求。它可以用于从Web服务器获取数据&#xff0c;或将数据发送到Web服务器。该函数提供了许多选项&#xff0c;例如设置请求头、设置代理服务器、设置超…

软件安全学习课程实践3:软件漏洞利用实验

1 逆向分析 1.1.1 和 1.1.2 直接 F5 看 flag 就可以了,故略。 1.1.3 对输入用了算法变换,能看到flag,比如输入x,经过f处理成f(x)然后判断f(x)=y,现在要破解f的算法然后写个逆预算g(y)=x 这个代码看起来很抽象,因为 IDA 没有正确的恢复这里的变量结构。选中变量按“N”重…

2023年中国开式冷却塔应用现状及行业市场规模前景分析[图]

开式塔是目前应用最广、类型最多的一种冷却系统。循环水移走工艺介质或换热设备所散发的热量后成为热水&#xff0c;热水进入冷却塔后和空气直接接触&#xff0c;大部分热水得到冷却后&#xff0c;再循环使用。开式冷却塔又可以分为逆流式冷却塔和横流式冷却塔&#xff0c;按照…

Spring Cloud Hystrix:服务容错保护

&#x1f497;wei_shuo的个人主页 &#x1f4ab;wei_shuo的学习社区 &#x1f310;Hello World &#xff01; Spring Cloud Hystrix&#xff1a;服务容错保护 Spring Cloud Hystrix是Spring Cloud中的一个子项目&#xff0c;主要用于服务容错保护&#xff1b;分布式系统中&…

muduo源码剖析之TcpServer服务端

简介 TcpServer拥有Acceptor类&#xff0c;新连接到达时new TcpConnection后续客户端和TcpConnection类交互。TcpServer管理连接和启动线程池&#xff0c;用Acceptor接受连接。 服务端封装 - muduo的server端维护了多个tcpconnection 注意TcpServer本身不带Channel&#xff0…

员工电脑管理软件,企业电脑管理软件是什么

员工电脑管理软件&#xff0c;企业电脑管理软件是什么 企业电脑管理软件是指用于管理和监控企业员工工作电脑的软件。这些软件通常提供多种功能&#xff0c;旨在帮助企业管理员工电脑的使用、监控和维护&#xff0c;同时确保信息安全、提高生产力并确保合规性。推荐一款功能强…

【Attack】针对GNN-based假新闻检测器

Attacking Fake News Detectors via Manipulating News Social Engagement AbstractMotivationContributions FormulationMethodologyAttacker Capability&#xff08;针对挑战1&#xff09;Agent Configuration&#xff08;针对挑战3&#xff09; WWW’23, April 30-May 4, 20…

第14届蓝桥杯青少组python试题解析:23年5月省赛

选择题 T1. 执行以下代码&#xff0c;输出结果是&#xff08;&#xff09;。 lst "abc" print(lstlst)abcabc abc lstlst abcabc T2. 执行以下代码&#xff0c;输出的结果是&#xff08;&#xff09;。 age {16,18,17} print(type(sorted(age)))<class set&…

深度学习入门(第四天)——递归神经网络与词向量原理解读

一、RNN网络架构解读 常规神经网络并不能考虑时间序列的特征&#xff08;比如前天昨天今天或者带有前后关联的特征&#xff09;&#xff0c;现在每个特征都是独立考虑的&#xff0c;那么如果有这样的特征&#xff0c;网络应该怎么学呢 而递归递归网络hidden这里的转回箭头&…