Springboot 添加License 以及生成證書和證書驗(yàn)證

1. 先準(zhǔn)備生成cer證書及私鑰,公鑰

## (1). 生成私匙庫(kù)

# validity:私鑰的有效期多少天365?

# alias:私鑰別稱privateKey

# keystore: 指定私鑰庫(kù)文件的名稱(生成在當(dāng)前目錄)privateKeys.keystore

# storepass:指定私鑰庫(kù)的密碼(獲取keystore信息所需的密碼)public_password

# keypass:指定別名條目的密碼(私鑰的密碼)private_password

keytool -genkeypair -keysize 1024 -validity 365 -alias "privateKey" -keystore "privateKeys.keystore" -storepass "public_password" -keypass "private_password" -dname "CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN"

此時(shí)會(huì)在cd的目錄中生成?privateKeys.keystore 文件

## (2). 把私匙庫(kù)內(nèi)的公匙導(dǎo)出到一個(gè)文件當(dāng)中

# alias:私鑰別稱privateKey

# keystore:指定私鑰庫(kù)的名稱(在當(dāng)前目錄查找)privateKeys.keystore

# storepass: 指定私鑰庫(kù)的密碼public_password

# file:證書名稱certfile.cer

keytool -exportcert -alias "privateKey" -keystore "privateKeys.keystore" -storepass "public_password" -file "certfile.cer"

此時(shí)會(huì)在cd的目錄中生成?certfile.cer?文件

## (3). 再把這個(gè)證書文件導(dǎo)入到公匙庫(kù)

# alias:公鑰別稱publicCert

# file:證書名稱certfile.cer

# keystore:公鑰文件名稱publicCerts.keystore

# storepass:指定私鑰庫(kù)的密碼public_password

keytool -import -alias "publicCert" -file "certfile.cer" -keystore "publicCerts.keystore" -storepass "public_password"

此時(shí)在命令窗口輸入 y 回車霜第,然后會(huì)在cd的目錄中生成?publicCerts.keystore?文件,注意此時(shí)應(yīng)該一共生成了三個(gè)文件分別是?privateKeys.keystore户辞,?certfile.cer泌类,publicCerts.keystore

2. 在項(xiàng)目中添加maven依賴(truelicense):

<dependency>

? ? <groupId>de.schlichtherle.truelicense</groupId>

? ? <artifactId>truelicense-core</artifactId>

? ? <version>1.33</version>

? ? <scope>provided</scope>

</dependency>

3. 開始編寫生成接口和驗(yàn)證接口目錄結(jié)構(gòu)如下圖:


(1) CustomKeyStoreParam.java

package com.ruoyi.license.create;

import de.schlichtherle.license.AbstractKeyStoreParam;

import java.io.*;

/**

* 自定義KeyStoreParam,用于將公私鑰存儲(chǔ)文件存放到其他磁盤位置而不是項(xiàng)目中

*/

public class CustomKeyStoreParam extends AbstractKeyStoreParam {

? ? /**

? ? * 公鑰/私鑰在磁盤上的存儲(chǔ)路徑

? ? */

? ? private String storePath;

? ? private String alias;

? ? private String storePwd;

? ? private 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;

? ? }

? ? /**

? ? * 復(fù)寫de.schlichtherle.license.AbstractKeyStoreParam的getStream()方法

? ? * <br/>

? ? * 用于將公私鑰存儲(chǔ)文件存放到其他磁盤位置而不是項(xiàng)目中

? ? */

? ? @Override

? ? public InputStream getStream() throws IOException {

? ? ? ? final InputStream in = new FileInputStream(new File(storePath));

? ? ? ? if (null == in) {

? ? ? ? ? ? throw new FileNotFoundException(storePath);

? ? ? ? }

? ? ? ? return in;

? ? }

}

(2) LicenseCreator.java

package com.ruoyi.license.create;

import com.ruoyi.license.verify.CustomLicenseManager;

import de.schlichtherle.license.*;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import javax.security.auth.x500.X500Principal;

import java.io.File;

import java.text.MessageFormat;

import java.util.prefs.Preferences;

/**

* 證書生成器

*/

public class LicenseCreator {

? ? private static Logger logger = LoggerFactory.getLogger(LicenseCreator.class);

? ? 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證書

? ? */

? ? public boolean generateLicense() {

? ? ? ? try {

? ? ? ? ? ? LicenseManager licenseManager = new CustomLicenseManager(initLicenseParam());

? ? ? ? ? ? LicenseContent licenseContent = initLicenseContent();

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

? ? ? ? ? ? return true;

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? logger.error(MessageFormat.format("證書生成失斉乜巍:{0}", param), e);

? ? ? ? ? ? return false;

? ? ? ? }

? ? }

? ? /**

? ? * 初始化證書生成參數(shù)

? ? */

? ? private LicenseParam initLicenseParam() {

? ? ? ? Preferences preferences = Preferences.userNodeForPackage(LicenseCreator.class);

? ? ? ? //設(shè)置對(duì)證書內(nèi)容加密的秘鑰

? ? ? ? CipherParam cipherParam = new DefaultCipherParam(param.getStorePass());

? ? ? ? KeyStoreParam privateStoreParam = new CustomKeyStoreParam(LicenseCreator.class

? ? ? ? ? ? ? ? , param.getPrivateKeysStorePath()

? ? ? ? ? ? ? ? , param.getPrivateAlias()

? ? ? ? ? ? ? ? , param.getStorePass()

? ? ? ? ? ? ? ? , param.getKeyPass());

? ? ? ? LicenseParam licenseParam = new DefaultLicenseParam(param.getSubject()

? ? ? ? ? ? ? ? , preferences

? ? ? ? ? ? ? ? , privateStoreParam

? ? ? ? ? ? ? ? , cipherParam);

? ? ? ? return licenseParam;

? ? }

? ? /**

? ? * 設(shè)置證書生成正文信息

? ? */

? ? 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.getExpiryTime());

? ? ? ? licenseContent.setConsumerType(param.getConsumerType());

? ? ? ? licenseContent.setConsumerAmount(param.getConsumerAmount());

? ? ? ? licenseContent.setInfo(param.getDescription());

? ? ? ? //擴(kuò)展校驗(yàn)服務(wù)器硬件信息

? ? ? ? licenseContent.setExtra(param.getLicenseCheckModel());

? ? ? ? return licenseContent;

? ? }

}

(3) LicenseCreatorController.java

package com.ruoyi.license.create;

import com.ruoyi.common.utils.StringUtils;

import com.ruoyi.license.model.AbstractServerInfos;

import com.ruoyi.license.model.LicenseCheckModel;

import com.ruoyi.license.model.LinuxServerInfos;

import com.ruoyi.license.model.WindowsServerInfos;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.http.MediaType;

import org.springframework.web.bind.annotation.RequestBody;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

import java.util.Map;

/**

* 證書生成路由

*/

@RestController

@RequestMapping("/license")

public class LicenseCreatorController {

? ? /**

? ? * 證書生成路徑

? ? */

? ? @Value("${license.createLicensePath}")

? ? private String createLicensePath;

? ? @Value("${license.privateKeysStorePath}")

? ? private String privateKeysStorePath;

? ? /**

? ? * 獲取服務(wù)器硬件信息

? ? */

? ? @RequestMapping(value = "/getServerInfos", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})

? ? public LicenseCheckModel getServerInfos(@RequestParam(value = "osName", required = false) String osName) {

? ? ? ? //操作系統(tǒng)類型

? ? ? ? if (StringUtils.isBlank(osName)) {

? ? ? ? ? ? osName = System.getProperty("os.name");

? ? ? ? }

? ? ? ? osName = osName.toLowerCase();

? ? ? ? AbstractServerInfos abstractServerInfos = null;

? ? ? ? //根據(jù)不同操作系統(tǒng)類型選擇不同的數(shù)據(jù)獲取方法

? ? ? ? if (osName.startsWith("windows")) {

? ? ? ? ? ? abstractServerInfos = new WindowsServerInfos();

? ? ? ? } else if (osName.startsWith("linux")) {

? ? ? ? ? ? abstractServerInfos = new LinuxServerInfos();

? ? ? ? } else {//其他服務(wù)器類型

? ? ? ? ? ? abstractServerInfos = new LinuxServerInfos();

? ? ? ? }

? ? ? ? return abstractServerInfos.getServerInfos();

? ? }

? ? /**

? ? * 生成證書

? ? */

? ? @RequestMapping(value = "/generateLicense", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})

? ? public Map<String, Object> generateLicense(@RequestBody(required = true) LicenseCreatorParam param) {

? ? ? ? Map<String, Object> resultMap = new HashMap<>(2);

? ? ? ? if (StringUtils.isBlank(param.getLicensePath())) {

? ? ? ? ? ? param.setLicensePath(createLicensePath);

? ? ? ? ? ? param.setPrivateKeysStorePath(privateKeysStorePath);

? ? ? ? }

? ? ? ? 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;

? ? }

}

(4) LicenseCreatorParam.java

package com.ruoyi.license.create;

import com.fasterxml.jackson.annotation.JsonFormat;

import com.ruoyi.license.model.LicenseCheckModel;

import lombok.Data;

import java.io.Serializable;

import java.util.Date;

/**

* 證書生成參數(shù)

*/

@Data

public class LicenseCreatorParam implements Serializable {

? ? private static final long serialVersionUID = -7793154252684580872L;

? ? /**

? ? * 證書subject

? ? */

? ? private String subject;

? ? /**

? ? * 密鑰別稱

? ? */

? ? private String privateAlias;

? ? /**

? ? * 密鑰密碼(需要妥善保管书蚪,不能讓使用者知道)

? ? */

? ? private String keyPass;

? ? /**

? ? * 訪問秘鑰庫(kù)的密碼

? ? */

? ? private String storePass;

? ? /**

? ? * 證書生成路徑

? ? */

? ? private String licensePath;

? ? /**

? ? * 密鑰庫(kù)存儲(chǔ)路徑

? ? */

? ? private String privateKeysStorePath;

? ? /**

? ? * 證書生效時(shí)間

? ? */

? ? @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")

? ? private Date issuedTime = new Date();

? ? /**

? ? * 證書失效時(shí)間

? ? */

? ? @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")

? ? private Date expiryTime;

? ? /**

? ? * 用戶類型

? ? */

? ? private String consumerType = "user";

? ? /**

? ? * 用戶數(shù)量

? ? */

? ? private Integer consumerAmount = 1;

? ? /**

? ? * 描述信息

? ? */

? ? private String description = "";

? ? /**

? ? * 額外的服務(wù)器硬件校驗(yàn)信息

? ? */

? ? private LicenseCheckModel licenseCheckModel;

}

(5) ApiConfig.java

package com.ruoyi.license.interceptor;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

/**

* 接口攔截器

*/

@Configuration

public class ApiConfig extends WebMvcConfigurationSupport {

? ? @Autowired

? ? LicenseCheckInterceptor licenseCheckInterceptor;

? ? @Override

? ? protected void addInterceptors(InterceptorRegistry registry) {

? ? ? ? registry.addInterceptor(this.licenseCheckInterceptor).addPathPatterns("/**");

? ? ? ? super.addInterceptors(registry);

? ? }

}

(6) LicenseCheckInterceptor.java

package com.ruoyi.license.interceptor;

import com.ruoyi.common.exception.LicenseException;

import com.ruoyi.license.verify.LicenseVerify;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* 攔截器校驗(yàn)證書是否合法

*/

@Slf4j

@SuppressWarnings("ALL")

@Component

public class LicenseCheckInterceptor extends HandlerInterceptorAdapter {

? ? @Override

? ? public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

? ? ? ? LicenseVerify licenseVerify = new LicenseVerify();

? ? ? ? log.info("開始驗(yàn)證證書是否有效");

? ? ? ? // 校驗(yàn)證書是否有效

? ? ? ? boolean verifyResult = licenseVerify.verify();

? ? ? ? // verifyResult = false;

? ? ? ? if (verifyResult) {

? ? ? ? ? ? return true;

? ? ? ? } else {

? ? ? ? ? ? log.warn("您的證書無(wú)效喇澡,請(qǐng)核查服務(wù)器是否取得授權(quán)或重新申請(qǐng)證書!");

? ? ? ? ? ? throw new LicenseException("您的證書無(wú)效殊校,請(qǐng)核查服務(wù)器是否取得授權(quán)或重新申請(qǐng)證書晴玖!");

? ? ? ? }

? ? }

}

(7) AbstractServerInfos.java

package com.ruoyi.license.model;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.net.InetAddress;

import java.net.NetworkInterface;

import java.net.SocketException;

import java.util.ArrayList;

import java.util.Enumeration;

import java.util.List;

/**

* 用于獲取客戶服務(wù)器的基本信息,如:IP为流、Mac地址残家、CPU序列號(hào)袱衷、主板序列號(hào)等

*/

public abstract class AbstractServerInfos {

? ? private static Logger logger = LoggerFactory.getLogger(AbstractServerInfos.class);

? ? /**

? ? * 組裝需要額外校驗(yàn)的License參數(shù)

? ? */

? ? public LicenseCheckModel getServerInfos() {

? ? ? ? LicenseCheckModel result = new LicenseCheckModel();

? ? ? ? try {

? ? ? ? ? ? result.setIpAddress(this.getIpAddress());

? ? ? ? ? ? result.setMacAddress(this.getMacAddress());

? ? ? ? ? ? result.setCpuSerial(this.getCPUSerial());

? ? ? ? ? ? result.setMainBoardSerial(this.getMainBoardSerial());

? ? ? ? } catch (Exception e) {

? ? ? ? ? ? logger.error("獲取服務(wù)器硬件信息失敗", e);

? ? ? ? }

? ? ? ? return result;

? ? }

? ? /**

? ? * 獲取IP地址

? ? *

? ? * @return java.util.List<java.lang.String>

? ? */

? ? protected abstract List<String> getIpAddress() throws Exception;

? ? /**

? ? * 獲取Mac地址

? ? *

? ? * @return java.util.List<java.lang.String>

? ? */

? ? protected abstract List<String> getMacAddress() throws Exception;

? ? /**

? ? * 獲取CPU序列號(hào)

? ? */

? ? protected abstract String getCPUSerial() throws Exception;

? ? /**

? ? * 獲取主板序列號(hào)

? ? */

? ? protected abstract String getMainBoardSerial() throws Exception;

? ? /**

? ? * 獲取當(dāng)前服務(wù)器所有符合條件的InetAddress

? ? *

? ? * @return java.util.List<java.net.InetAddress>

? ? * @author zifangsky

? ? * @date 2018/4/23 17:38

? ? * @since 1.0.0

? ? */

? ? protected List<InetAddress> getLocalAllInetAddress() throws Exception {

? ? ? ? List<InetAddress> result = new ArrayList<>(4);

? ? ? ? // 遍歷所有的網(wǎng)絡(luò)接口

? ? ? ? for (Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); networkInterfaces.hasMoreElements(); ) {

? ? ? ? ? ? NetworkInterface iface = (NetworkInterface) networkInterfaces.nextElement();

? ? ? ? ? ? // 在所有的接口下再遍歷IP

? ? ? ? ? ? for (Enumeration inetAddresses = iface.getInetAddresses(); inetAddresses.hasMoreElements(); ) {

? ? ? ? ? ? ? ? InetAddress inetAddr = (InetAddress) inetAddresses.nextElement();

? ? ? ? ? ? ? ? //排除LoopbackAddress噩峦、SiteLocalAddress币他、LinkLocalAddress、MulticastAddress類型的IP地址

? ? ? ? ? ? ? ? if (!inetAddr.isLoopbackAddress() /*&& !inetAddr.isSiteLocalAddress()*/

? ? ? ? ? ? ? ? ? ? ? ? && !inetAddr.isLinkLocalAddress() && !inetAddr.isMulticastAddress()) {

? ? ? ? ? ? ? ? ? ? result.add(inetAddr);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return result;

? ? }

? ? /**

? ? * 獲取某個(gè)網(wǎng)絡(luò)接口的Mac地址

? ? */

? ? protected String getMacByInetAddress(InetAddress inetAddr) {

? ? ? ? try {

? ? ? ? ? ? byte[] mac = NetworkInterface.getByInetAddress(inetAddr).getHardwareAddress();

? ? ? ? ? ? StringBuffer stringBuffer = new StringBuffer();

? ? ? ? ? ? for (int i = 0; i < mac.length; i++) {

? ? ? ? ? ? ? ? if (i != 0) {

? ? ? ? ? ? ? ? ? ? stringBuffer.append("-");

? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? //將十六進(jìn)制byte轉(zhuǎn)化為字符串

? ? ? ? ? ? ? ? String temp = Integer.toHexString(mac[i] & 0xff);

? ? ? ? ? ? ? ? if (temp.length() == 1) {

? ? ? ? ? ? ? ? ? ? stringBuffer.append("0" + temp);

? ? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? ? ? ? stringBuffer.append(temp);

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? return stringBuffer.toString().toUpperCase();

? ? ? ? } catch (SocketException e) {

? ? ? ? ? ? e.printStackTrace();

? ? ? ? }

? ? ? ? return null;

? ? }

}

(8) LicenseBaseModel.java

package com.ruoyi.license.model;

import lombok.Data;

import java.io.Serializable;

import java.util.Date;

/**

* 可被允許的服務(wù)器硬件信息的實(shí)體類

*/

@Data

public class LicenseBaseModel implements Serializable {

? ? private static final long serialVersionUID = 8600137500316662317L;

? ? private String subject;

? ? private Date issued;

? ? private Date notBefore;

? ? private Date notAfter;

}

(9) LicenseCheckModel.java

package com.ruoyi.license.model;

import lombok.Data;

import java.io.Serializable;

import java.util.List;

/**

* 可被允許的服務(wù)器硬件信息的實(shí)體類

*/

@Data

public class LicenseCheckModel implements Serializable {

? ? private static final long serialVersionUID = 8600137500316662317L;

? ? /**

? ? * 可被允許的IP地址

? ? */

? ? private List<String> ipAddress;

? ? /**

? ? * 可被允許的MAC地址

? ? */

? ? private List<String> macAddress;

? ? /**

? ? * 可被允許的CPU序列號(hào)

? ? */

? ? private String cpuSerial;

? ? /**

? ? * 可被允許的主板序列號(hào)

? ? */

? ? private String mainBoardSerial;

? ? @Override

? ? public String toString() {

? ? ? ? return "LicenseCheckModel{" +

? ? ? ? ? ? ? ? "ipAddress=" + ipAddress +

? ? ? ? ? ? ? ? ", macAddress=" + macAddress +

? ? ? ? ? ? ? ? ", cpuSerial='" + cpuSerial + '\'' +

? ? ? ? ? ? ? ? ", mainBoardSerial='" + mainBoardSerial + '\'' +

? ? ? ? ? ? ? ? '}';

? ? }

}

(10) LinuxServerInfos.java

package com.ruoyi.license.model;

import com.ruoyi.common.utils.StringUtils;

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.InetAddress;

import java.util.List;

import java.util.stream.Collectors;

/**

* 用于獲取客戶Linux服務(wù)器的基本信息

*/

public class LinuxServerInfos extends AbstractServerInfos {

? ? @Override

? ? protected List<String> getIpAddress() throws Exception {

? ? ? ? List<String> result = null;

? ? ? ? //獲取所有網(wǎng)絡(luò)接口

? ? ? ? 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;

? ? }

? ? @Override

? ? protected List<String> getMacAddress() throws Exception {

? ? ? ? List<String> result = null;

? ? ? ? //1. 獲取所有網(wǎng)絡(luò)接口

? ? ? ? List<InetAddress> inetAddresses = getLocalAllInetAddress();

? ? ? ? if (inetAddresses != null && inetAddresses.size() > 0) {

? ? ? ? ? ? //2. 獲取所有網(wǎng)絡(luò)接口的Mac地址

? ? ? ? ? ? result = inetAddresses.stream().map(this::getMacByInetAddress).distinct().collect(Collectors.toList());

? ? ? ? }

? ? ? ? return result;

? ? }

? ? @Override

? ? protected String getCPUSerial() throws Exception {

? ? ? ? //序列號(hào)

? ? ? ? String serialNumber = "";

? ? ? ? //使用dmidecode命令獲取CPU序列號(hào)

? ? ? ? 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 {

? ? ? ? //序列號(hào)

? ? ? ? String serialNumber = "";

? ? ? ? //使用dmidecode命令獲取主板序列號(hào)

? ? ? ? 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;

? ? }

}

(11) WindowsServerInfos.java

package com.ruoyi.license.model;

import java.net.InetAddress;

import java.util.List;

import java.util.Scanner;

import java.util.stream.Collectors;

/**

* 用于獲取客戶Windows服務(wù)器的基本信息

*/

public class WindowsServerInfos extends AbstractServerInfos {

? ? @Override

? ? protected List<String> getIpAddress() throws Exception {

? ? ? ? List<String> result = null;

? ? ? ? //獲取所有網(wǎng)絡(luò)接口

? ? ? ? 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;

? ? }

? ? @Override

? ? protected List<String> getMacAddress() throws Exception {

? ? ? ? List<String> result = null;

? ? ? ? //1. 獲取所有網(wǎng)絡(luò)接口

? ? ? ? List<InetAddress> inetAddresses = getLocalAllInetAddress();

? ? ? ? if (inetAddresses != null && inetAddresses.size() > 0) {

? ? ? ? ? ? //2. 獲取所有網(wǎng)絡(luò)接口的Mac地址

? ? ? ? ? ? result = inetAddresses.stream().map(this::getMacByInetAddress).distinct().collect(Collectors.toList());

? ? ? ? }

? ? ? ? return result;

? ? }

? ? @Override

? ? protected String getCPUSerial() throws Exception {

? ? ? ? //序列號(hào)

? ? ? ? String serialNumber = "";

? ? ? ? //使用WMIC獲取CPU序列號(hào)

? ? ? ? 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 {

? ? ? ? //序列號(hào)

? ? ? ? String serialNumber = "";

? ? ? ? //使用WMIC獲取主板序列號(hào)

? ? ? ? Process process = Runtime.getRuntime().exec("wmic baseboard get serialnumber");

? ? ? ? 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;

? ? }

}

(12) CustomLicenseManager.java

package com.ruoyi.license.verify;

import com.ruoyi.common.exception.LicenseException;

import com.ruoyi.common.utils.StringUtils;

import com.ruoyi.common.utils.bean.BeanUtils;

import com.ruoyi.license.model.*;

import de.schlichtherle.license.*;

import de.schlichtherle.xml.GenericCertificate;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.beans.XMLDecoder;

import java.io.BufferedInputStream;

import java.io.ByteArrayInputStream;

import java.io.UnsupportedEncodingException;

import java.util.Date;

import java.util.List;

/**

* 自定義LicenseManager莲祸,用于增加額外的服務(wù)器硬件信息校驗(yàn)

*/

public class CustomLicenseManager extends LicenseManager {

? ? private static Logger logger = LoggerFactory.getLogger(CustomLicenseManager.class);

? ? // XML編碼

? ? private static final String XML_CHARSET = "UTF-8";

? ? // 默認(rèn)BUFSIZE

? ? private static final int DEFAULT_BUFSIZE = 8 * 1024;

? ? public CustomLicenseManager() {

? ? }

? ? public CustomLicenseManager(LicenseParam param) {

? ? ? ? super(param);

? ? }

? ? /**

? ? * 復(fù)寫create方法

? ? */

? ? @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);

? ? }

? ? /**

? ? * 復(fù)寫install方法蹂安,其中validate方法調(diào)用本類中的validate方法,校驗(yàn)IP地址锐帜、Mac地址等其他信息

? ? */

? ? @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;

? ? }

? ? /**

? ? * 復(fù)寫verify方法田盈,調(diào)用本類中的validate方法,校驗(yàn)IP地址缴阎、Mac地址等其他信息

? ? */

? ? @Override

? ? protected synchronized LicenseContent verify(final LicenseNotary notary)

? ? ? ? ? ? throws Exception {

? ? ? ? GenericCertificate certificate = getCertificate();

? ? ? ? // Load license key from preferences,

? ? ? ? final byte[] key = getLicenseKey();

? ? ? ? if (null == key) {

? ? ? ? ? ? throw new NoLicenseInstalledException(getLicenseParam().getSubject());

? ? ? ? }

? ? ? ? certificate = getPrivacyGuard().key2cert(key);

? ? ? ? notary.verify(certificate);

? ? ? ? final LicenseContent content = (LicenseContent) this.load(certificate.getEncoded());

? ? ? ? this.validate(content);

? ? ? ? setCertificate(certificate);

? ? ? ? return content;

? ? }

? ? /**

? ? * 校驗(yàn)生成證書的參數(shù)信息

? ? */

? ? protected synchronized void validateCreate(final LicenseContent content)

? ? ? ? ? ? throws LicenseContentException {

? ? ? ? final LicenseParam param = getLicenseParam();

? ? ? ? final Date now = new Date();

? ? ? ? final Date notBefore = content.getNotBefore();

? ? ? ? final Date notAfter = content.getNotAfter();

? ? ? ? logger.debug("證書生效時(shí)間:" + notBefore + "允瞧,證書失效時(shí)間:" + notAfter);

? ? ? ? if (null != notAfter && now.after(notAfter)) {

? ? ? ? ? ? throw new LicenseContentException("證書失效時(shí)間不能早于當(dāng)前時(shí)間");

? ? ? ? }

? ? ? ? if (null != notBefore && null != notAfter && notAfter.before(notBefore)) {

? ? ? ? ? ? throw new LicenseContentException("證書生效時(shí)間不能晚于證書失效時(shí)間");

? ? ? ? }

? ? ? ? final String consumerType = content.getConsumerType();

? ? ? ? if (null == consumerType) {

? ? ? ? ? ? throw new LicenseContentException("用戶類型不能為空");

? ? ? ? }

? ? }

? ? /**

? ? * 復(fù)寫validate方法,增加IP地址、Mac地址等其他信息校驗(yàn)

? ? */

? ? @Override

? ? protected synchronized void validate(final LicenseContent content)

? ? ? ? ? ? throws LicenseContentException, LicenseException {

? ? ? ? //1. 首先調(diào)用父類的validate方法

? ? ? ? super.validate(content);

? ? ? ? //2. 然后校驗(yàn)自定義的License參數(shù)

? ? ? ? // License中可被允許的參數(shù)信息

? ? ? ? LicenseBaseModel licenseBaseModel = new LicenseBaseModel();

? ? ? ? BeanUtils.copyBeanProp(licenseBaseModel, content);

? ? ? ? // System.out.println("基本信息=====>" + licenseBaseModel);

? ? ? ? LicenseCheckModel expectedCheckModel = (LicenseCheckModel) content.getExtra();

? ? ? ? // System.out.println("證書額外驗(yàn)證信息=====>" + expectedCheckModel);

? ? ? ? // 當(dāng)前服務(wù)器真實(shí)的參數(shù)信息

? ? ? ? LicenseCheckModel serverCheckModel = getServerInfos();

? ? ? ? // System.out.println("當(dāng)前服務(wù)器信息=====>" + serverCheckModel);

? ? ? ? if (expectedCheckModel != null && serverCheckModel != null) {

? ? ? ? ? ? //校驗(yàn)IP地址

? ? ? ? ? ? if (!checkIpAddress(expectedCheckModel.getIpAddress(), serverCheckModel.getIpAddress())) {

? ? ? ? ? ? ? ? throw new LicenseException("當(dāng)前服務(wù)器的IP沒在授權(quán)范圍內(nèi)");

? ? ? ? ? ? }

? ? ? ? ? ? //校驗(yàn)Mac地址

? ? ? ? ? ? if (!checkIpAddress(expectedCheckModel.getMacAddress(), serverCheckModel.getMacAddress())) {

? ? ? ? ? ? ? ? throw new LicenseException("當(dāng)前服務(wù)器的Mac地址沒在授權(quán)范圍內(nèi)");

? ? ? ? ? ? }

//? ? ? ? ? ? //校驗(yàn)主板序列號(hào)

//? ? ? ? ? ? if (!checkSerial(expectedCheckModel.getMainBoardSerial(), serverCheckModel.getMainBoardSerial())) {

//? ? ? ? ? ? ? ? throw new LicenseContentException("當(dāng)前服務(wù)器的主板序列號(hào)沒在授權(quán)范圍內(nèi)");

//? ? ? ? ? ? }

//

//? ? ? ? ? ? //校驗(yàn)CPU序列號(hào)

//? ? ? ? ? ? if (!checkSerial(expectedCheckModel.getCpuSerial(), serverCheckModel.getCpuSerial())) {

//? ? ? ? ? ? ? ? throw new LicenseContentException("當(dāng)前服務(wù)器的CPU序列號(hào)沒在授權(quán)范圍內(nèi)");

//? ? ? ? ? ? }

? ? ? ? } else {

? ? ? ? ? ? // throw new LicenseException("不能獲取服務(wù)器硬件信息");

? ? ? ? }

? ? }

? ? /**

? ? * 重寫XMLDecoder解析XML

? ? */

? ? 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_BUFSIZE), 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) {

? ? ? ? ? ? ? ? logger.error("XMLDecoder解析XML失敗", e);

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return null;

? ? }

? ? /**

? ? * 獲取當(dāng)前服務(wù)器需要額外校驗(yàn)的License參數(shù)

? ? */

? ? private LicenseCheckModel getServerInfos() {

? ? ? ? //操作系統(tǒng)類型

? ? ? ? String osName = System.getProperty("os.name").toLowerCase();

? ? ? ? AbstractServerInfos abstractServerInfos = null;

? ? ? ? //根據(jù)不同操作系統(tǒng)類型選擇不同的數(shù)據(jù)獲取方法

? ? ? ? if (osName.startsWith("windows")) {

? ? ? ? ? ? abstractServerInfos = new WindowsServerInfos();

? ? ? ? } else if (osName.startsWith("linux")) {

? ? ? ? ? ? abstractServerInfos = new LinuxServerInfos();

? ? ? ? } else {//其他服務(wù)器類型

? ? ? ? ? ? abstractServerInfos = new LinuxServerInfos();

? ? ? ? }

? ? ? ? return abstractServerInfos.getServerInfos();

? ? }

? ? /**

? ? * 校驗(yàn)當(dāng)前服務(wù)器的IP/Mac地址是否在可被允許的IP范圍內(nèi)

? ? * 如果存在IP在可被允許的IP/Mac地址范圍內(nèi)述暂,則返回true

? ? */

? ? private boolean checkIpAddress(List<String> expectedList, List<String> serverList) {

? ? ? ? if (expectedList != null && expectedList.size() > 0) {

? ? ? ? ? ? if (serverList != null && serverList.size() > 0) {

? ? ? ? ? ? ? ? for (String expected : expectedList) {

? ? ? ? ? ? ? ? ? ? if (serverList.contains(expected.trim())) {

? ? ? ? ? ? ? ? ? ? ? ? return true;

? ? ? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? return false;

? ? ? ? } else {

? ? ? ? ? ? return true;

? ? ? ? }

? ? }

? ? /**

? ? * 校驗(yàn)當(dāng)前服務(wù)器硬件(主板痹升、CPU等)序列號(hào)是否在可允許范圍內(nèi)

? ? */

? ? private boolean checkSerial(String expectedSerial, String serverSerial) {

? ? ? ? if (StringUtils.isNotBlank(expectedSerial)) {

? ? ? ? ? ? if (StringUtils.isNotBlank(serverSerial)) {

? ? ? ? ? ? ? ? if (expectedSerial.equals(serverSerial)) {

? ? ? ? ? ? ? ? ? ? return true;

? ? ? ? ? ? ? ? }

? ? ? ? ? ? }

? ? ? ? ? ? return false;

? ? ? ? } else {

? ? ? ? ? ? return true;

? ? ? ? }

? ? }

}

(13) LicenseCheckListener.java

package com.ruoyi.license.verify;

import com.ruoyi.common.utils.StringUtils;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

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;

/**

* 在項(xiàng)目啟動(dòng)時(shí)安裝證書

*/

@Component

public class LicenseCheckListener implements ApplicationListener<ContextRefreshedEvent> {

? ? private static Logger logger = LoggerFactory.getLogger(LicenseCheckListener.class);

? ? /**

? ? * 證書subject

? ? */

? ? @Value("${license.subject}")

? ? private String subject;

? ? /**

? ? * 公鑰別稱

? ? */

? ? @Value("${license.publicAlias}")

? ? private String publicAlias;

? ? /**

? ? * 訪問公鑰庫(kù)的密碼

? ? */

? ? @Value("${license.storePass}")

? ? private String storePass;

? ? /**

? ? * 證書生成路徑

? ? */

? ? @Value("${license.licensePath}")

? ? private String licensePath;

? ? /**

? ? * 密鑰庫(kù)存儲(chǔ)路徑

? ? */

? ? @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)) {

? ? ? ? ? ? ? ? logger.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);

? ? ? ? ? ? ? ? logger.info("++++++++ 證書安裝結(jié)束 ++++++++");

? ? ? ? ? ? }

? ? ? ? }

? ? }

}

(14) LicenseManagerHolder.java

package com.ruoyi.license.verify;

import de.schlichtherle.license.LicenseManager;

import de.schlichtherle.license.LicenseParam;

/**

* de.schlichtherle.license.LicenseManager的單例

*/

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;

? ? }

}

(15) LicenseVerify.java

package com.ruoyi.license.verify;

import com.ruoyi.license.create.CustomKeyStoreParam;

import de.schlichtherle.license.*;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.io.File;

import java.text.DateFormat;

import java.text.MessageFormat;

import java.text.SimpleDateFormat;

import java.util.prefs.Preferences;

/**

* License校驗(yàn)類

*/

public class LicenseVerify {

? ? private static Logger logger = LoggerFactory.getLogger(LicenseVerify.class);

? ? /**

? ? * 安裝License證書

? ? */

? ? public synchronized LicenseContent install(LicenseVerifyParam param){

? ? ? ? LicenseContent result = null;

? ? ? ? DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

? ? ? ? //1. 安裝證書

? ? ? ? try{

? ? ? ? ? ? LicenseManager licenseManager = LicenseManagerHolder.getInstance(initLicenseParam(param));

? ? ? ? ? ? licenseManager.uninstall();

? ? ? ? ? ? result = licenseManager.install(new File(param.getLicensePath()));

? ? ? ? ? ? logger.info(MessageFormat.format("證書安裝成功,證書有效期:{0} - {1}",format.format(result.getNotBefore()),format.format(result.getNotAfter())));

? ? ? ? }catch (Exception e){

? ? ? ? ? ? logger.error("證書安裝失斆车洹视卢!",e);

? ? ? ? }

? ? ? ? return result;

? ? }

? ? /**

? ? * 校驗(yàn)License證書

? ? */

? ? public boolean verify() {

? ? ? ? LicenseManager licenseManager = LicenseManagerHolder.getInstance(null);

? ? ? ? DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

? ? ? ? //2. 校驗(yàn)證書

? ? ? ? try {

? ? ? ? ? ? LicenseContent licenseContent = licenseManager.verify();

? ? ? ? ? ? logger.info("subject=======>" + licenseContent.getSubject());

? ? ? ? ? ? logger.info(MessageFormat.format("證書校驗(yàn)通過,證書有效期:{0} - {1}", format.format(licenseContent.getNotBefore()),format.format(licenseContent.getNotAfter())));

? ? ? ? ? ? return true;

? ? ? ? }catch (Exception e){

? ? ? ? ? ? logger.error("證書校驗(yàn)失斃韧铡!",e);

? ? ? ? ? ? return false;

? ? ? ? }

? ? }

? ? /**

? ? * 初始化證書生成參數(shù)

? ? */

? ? 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);

? ? }

}

(16) LicenseVerifyController.java

package com.ruoyi.license.verify;

import com.ruoyi.common.annotation.Log;

import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.common.enums.BusinessType;

import org.springframework.web.bind.annotation.GetMapping;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**

* 驗(yàn)證接口

*/

@RestController

@RequestMapping("/license")

public class LicenseVerifyController {

? ? /**

? ? * 驗(yàn)證信息

? ? */

? ? @GetMapping("/verify")

? ? @Log(title = "證書驗(yàn)證", businessType = BusinessType.OTHER)

? ? public AjaxResult licenseVerify() {

? ? ? ? return AjaxResult.success("驗(yàn)證成功");

? ? }

}

(17) LicenseVerifyParam.java

package com.ruoyi.license.verify;

import lombok.AllArgsConstructor;

import lombok.Data;

import lombok.NoArgsConstructor;

/**

* License校驗(yàn)類需要的參數(shù)

*/

@Data

@NoArgsConstructor

@AllArgsConstructor

public class LicenseVerifyParam {

? ? /**

? ? * 證書subject

? ? */

? ? private String subject;

? ? /**

? ? * 公鑰別稱

? ? */

? ? private String publicAlias;

? ? /**

? ? * 訪問公鑰庫(kù)的密碼

? ? */

? ? private String storePass;

? ? /**

? ? * 證書生成路徑

? ? */

? ? private String licensePath;

? ? /**

? ? * 密鑰庫(kù)存儲(chǔ)路徑

? ? */

? ? private String publicKeysStorePath;

}

(18) 相關(guān)配置

license:

? privateKeysStorePath: D:/license/new/privateKeys.keystore

? createLicensePath: D:/license/new/license.lic

? subject: xxxx

? publicAlias: publicCert

? storePass: public_password

? licensePath: D:/license/license.lic

? publicKeysStorePath: D:/license/publicCerts.keystore

4. 證書測(cè)試

(1) 生成證書,將第一步生成的privateKeys.keystore文件放在對(duì)應(yīng)目錄(privateKeysStorePath)下 (先吧攔截器注釋掉惋砂,否則有可能訪問不到)

請(qǐng)求/license/generateLicense?參數(shù)為json妒挎,content-type 為 application/json? 例如:

{

? "subject" : "xxxx",

? "privateAlias" : "privateKey",

? "keyPass": "private_password",

? "storePass" : "public_password",

? "expiryTime": "2024-04-10 14:15:00",

? "licenseCheckModel" : {

? ? ? "ipAddress": ["000.000.0.000"],

? ? ? "macAddress": ["XX-XX-XX-XX-XX-XX"],

? ? ? "cpuSerial": "",

? ? ? "mainBoardSerial": ""

? ?}

}

若成功則會(huì)在配置的目錄(createLicensePath)下生成license.lic文件,不成功一般是沒有將對(duì)應(yīng)的privateKeys.keystore放置或者是參數(shù)與第一步設(shè)置的參數(shù)不同步或者參數(shù)不合理導(dǎo)致

(2) 驗(yàn)證證書西饵,將新生成的license.lic文件放到對(duì)應(yīng)的目錄(licensePath)下?(攔截器取消注釋,然后重啟)

請(qǐng)求/license/verify?查看響應(yīng)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末酝掩,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子眷柔,更是在濱河造成了極大的恐慌期虾,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,544評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件驯嘱,死亡現(xiàn)場(chǎng)離奇詭異镶苞,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)鞠评,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,430評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門茂蚓,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人剃幌,你說我怎么就攤上這事聋涨。” “怎么了负乡?”我有些...
    開封第一講書人閱讀 162,764評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵牍白,是天一觀的道長(zhǎng)。 經(jīng)常有香客問我抖棘,道長(zhǎng)茂腥,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,193評(píng)論 1 292
  • 正文 為了忘掉前任钉答,我火速辦了婚禮础芍,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘数尿。我一直安慰自己仑性,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,216評(píng)論 6 388
  • 文/花漫 我一把揭開白布右蹦。 她就那樣靜靜地躺著诊杆,像睡著了一般歼捐。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上晨汹,一...
    開封第一講書人閱讀 51,182評(píng)論 1 299
  • 那天豹储,我揣著相機(jī)與錄音,去河邊找鬼淘这。 笑死剥扣,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的铝穷。 我是一名探鬼主播钠怯,決...
    沈念sama閱讀 40,063評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼曙聂!你這毒婦竟也來(lái)了晦炊?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 38,917評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤宁脊,失蹤者是張志新(化名)和其女友劉穎断国,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體榆苞,經(jīng)...
    沈念sama閱讀 45,329評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡稳衬,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,543評(píng)論 2 332
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了语稠。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片宋彼。...
    茶點(diǎn)故事閱讀 39,722評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖仙畦,靈堂內(nèi)的尸體忽然破棺而出输涕,到底是詐尸還是另有隱情,我是刑警寧澤慨畸,帶...
    沈念sama閱讀 35,425評(píng)論 5 343
  • 正文 年R本政府宣布莱坎,位于F島的核電站,受9級(jí)特大地震影響寸士,放射性物質(zhì)發(fā)生泄漏檐什。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,019評(píng)論 3 326
  • 文/蒙蒙 一弱卡、第九天 我趴在偏房一處隱蔽的房頂上張望乃正。 院中可真熱鬧,春花似錦婶博、人聲如沸瓮具。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,671評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)名党。三九已至叹阔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間传睹,已是汗流浹背耳幢。 一陣腳步聲響...
    開封第一講書人閱讀 32,825評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留欧啤,地道東北人睛藻。 一個(gè)月前我還...
    沈念sama閱讀 47,729評(píng)論 2 368
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像邢隧,于是被迫代替她去往敵國(guó)和親修档。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,614評(píng)論 2 353

推薦閱讀更多精彩內(nèi)容