spring boot添加License(软件许可)

文章目录


前言

工作需要给软件加上许可

keytool 工具在你的java安装的bin目录中,命令行打开执行就行

1. 生成钥匙库

bash 复制代码
# validity:私钥的有效期多少天
# alias:私钥别称
# keyalg:指定加密算法,默认是DSA
# keystore: 指定私钥库文件的名称(生成在当前目录)
# storepass:指定私钥库的密码(获取keystore信息所需的密码) 
# keypass:指定别名条目的密码(私钥的密码) 

keytool -genkeypair -keysize 1024 -validity 3650 -alias "privateKey" -keystore "C:\Users\Administrator\Desktop\license\privateKey.keystore" -storepass "zhiutech@123" -keypass "zhiutech@123" -dname "CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN"

2. 生成证书

bash 复制代码
# alias:私钥别称
# keystore:指定私钥库的名称(在当前目录查找)
# storepass: 指定私钥库的密码
# file:证书名称

keytool -exportcert -alias "privateKey" -keystore "C:\Users\Administrator\Desktop\license\privateKey.keystore" -storepass "zhiutech@123" -file "C:\Users\Administrator\Desktop\license\certfile.cer"

3. 生成公匙库

bash 复制代码
# alias:公钥别称
# file:证书名称
# keystore:公钥文件名称
# storepass:指定私钥库的密码
keytool -import -alias "publicCert" -file "C:\Users\Administrator\Desktop\license\certfile.cer" -keystore "C:\Users\Administrator\Desktop\license\publicCerts.keystore" -storepass "zhiutech@123"

4.业务代码

源码借鉴了这个地址:

https://gitee.com/Zhiyun_Lee/ruo-yi-vue3-license

1. 引入依赖

bash 复制代码
<!-- License -->
<dependency>
    <groupId>de.schlichtherle.truelicense</groupId>
    <artifactId>truelicense-core</artifactId>
    <version>1.33</version>
</dependency>

2. 关键代码

CustomKeyStoreParam自定义参数,公私钥存放路径和其他信息。关键在于重写getStream方法,会因为本地开发环境原因会报错,需要根据注释使用不同方法获取存储路径上的文件信息。

java 复制代码
/**
 * 自定义KeyStoreParam
 */
public class CustomKeyStoreParam extends AbstractKeyStoreParam {
    // ...省略代码
    
    /**
     * 重写getStream()方法
     * @return
     * @throws IOException
     */
    @Override
    public InputStream getStream() throws IOException {
        // 本地开发环境,License生成
//        final InputStream in = new FileInputStream(new File(storePath));
​
        // 本地开发环境,License加载
        // 线上环境,直接用这个
        InputStream in = new ClassPathResource(storePath).getStream();
        if (null == in){
            throw new FileNotFoundException(storePath);
        }
        return in;
    }
}

ResourcesConfig通用配置文件,在自定义拦截规则里添加License检查拦截器。考虑到License检查需要花费时间,如果所有接口都设置拦截,那意味着整个项目平均请求慢了几百毫秒,所以这边我只拦截登录接口,因为没有登录就没有token鉴权,也就没有可能请求成功其他接口。

java 复制代码
/**
 * 通用配置
 * 
 * @author ruoyi
 */
@Configuration
public class ResourcesConfig implements WebMvcConfigurer
{
    @Autowired
    private RepeatSubmitInterceptor repeatSubmitInterceptor;
    @Autowired
    private LicenseCheckInterceptor licenseCheckInterceptor;
    
    // ...省略代码
    
    /**
     * 自定义拦截规则
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry)
    {
        registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
        registry.addInterceptor(licenseCheckInterceptor).addPathPatterns("/login");
    }
    
    // ...省略代码
}

SecurityConfig配置文件,添加/license/getInfo请求接口地址不过滤。

java 复制代码
/**
 * spring security配置
 * 
 * @author ruoyi
 */
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter
{
    // ...省略代码
    
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception
    {
        httpSecurity
            // 对于登录login 注册register 验证码captchaImage 允许匿名访问
            // 添加获取License硬件信息
            .antMatchers("/login", "/register", "/captchaImage", "/license/getInfo").permitAll();
    }
}

3. 配置文件

application.yml配置文件填写License相关配置

yaml 复制代码
#License相关配置
license:
  #License如果是本地地址,则改成本地地址,我这里改成线上地址了
  licenseFile: http://192.168.110.188:9000/iot/2024/05/21/license.lic
  subject: MyLicenseDemo
  publicAlias: publicCert
  storePass: zhiutech@123
  # 公钥地址
  publicKeysStoreFile: C:/Users/Administrator/Desktop/license/publicCerts.keystore
  generateLicensePath: C:/Users/Administrator/Desktop/license/license.lic

提示:如果只是打包到文件中,到这个地方就可以了,下面的内容是改成线上访问授权文件,不是本地访问了

5、改成线上地址,这样不用每次打包,发送license.lic文件给客户,重启项目就行

这里我对licenseFile做了一下改变,如果您不需要线上地址去校验license.lic,到这个地方就可以了

5.1、工具类

java 复制代码
package com.ruoyi.auth.license.utils;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * 操作word文档工具类
 *
 * @author wangyj
 * @date 2020-03-03
 *
 */
public class POIUtil {


	/**
	* 用一个docx文档作为模板,然后替换其中的内容,再写入目标文档中。
	* @throws Exception
	*/
	public static void templateWrite(String filePath,String outFilePath,Map<String, Object> params) throws Exception {
		InputStream is = new FileInputStream(filePath);
		XWPFDocument doc = new XWPFDocument(is);
		//替换段落里面的变量
		replaceInPara(doc, params);
		//替换表格里面的变量
		replaceInTable(doc, params);
		OutputStream os = new FileOutputStream(outFilePath);
		doc.write(os);
		close(os);
		close(is);
	}

	/**
	 * 用一个docx文档作为模板,然后替换其中的内容,再写入目标文档中。
	 * @throws Exception
	 */
	public static XWPFDocument templateWrite(InputStream is,Map<String, Object> params) throws Exception {
		XWPFDocument doc = new XWPFDocument(is);
		//替换段落里面的变量
		replaceInPara(doc, params);
		//替换表格里面的变量
		replaceInTable(doc, params);
		//保存文档
		close(is);
		return doc;
	}



	/**
	 * XWPFDocument 转 MultipartFile(CommonsMultipartFile)
	 *
	 * @param document 文档对象
	 * @param fileName 文件名
	 * @return
	 */
	public static MultipartFile xwpfDocumentToCommonsMultipartFile(XWPFDocument document, String fileName) {
		//XWPFDocument转FileItem
		FileItemFactory factory = new DiskFileItemFactory(16, null);
		FileItem fileItem = factory.createItem("textField", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", true, fileName+".docx");
		try {
			OutputStream os = fileItem.getOutputStream();
			document.write(os);
			os.close();
			//FileItem转MultipartFile
			MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
			return multipartFile;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


   /**
    * 替换段落里面的变量
    * @param doc 要替换的文档
    * @param params 参数
    */
	private static void replaceInPara(XWPFDocument doc, Map<String, Object> params) {
		Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
		XWPFParagraph para;
		while (iterator.hasNext()) {
			para = iterator.next();
			replaceInPara(para, params);
		}
	}

	/**
	 * 替换段落里面的变量
	 */
	public static List<String> getparas(InputStream is) throws IOException {
		XWPFDocument doc = new XWPFDocument(is);
		List<String> paramsList = new ArrayList<>();
		Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();
		XWPFParagraph para;
		while (iterator.hasNext()) {
			para = iterator.next();
			System.out.println(para.getParagraphText());
			Matcher matcher = matcher(para.getParagraphText());
			if (matcher.find()) {
				paramsList.add(matcher.group(1));
			}
		}
		return paramsList;
	}




   /**
	* 替换段落里面的变量
	*
	* @param para   要替换的段落
	* @param params 参数
	*/
	private static void replaceInPara(XWPFParagraph para, Map<String, Object> params) {
		List<XWPFRun> runs;
		Matcher matcher;
		String runText = "";
		int fontSize = 0;
		UnderlinePatterns underlinePatterns = null;
		if (matcher(para.getParagraphText()).find()) {
			runs = para.getRuns();
			if (runs.size() > 0) {
				int j = runs.size();
				for (int i = 0; i < j; i++) {
					XWPFRun run = runs.get(0);
					if (fontSize == 0) {
						fontSize = run.getFontSize();
					}
					if(underlinePatterns==null){
						underlinePatterns=run.getUnderline();
					}
					String i1 = run.toString();
					runText += i1;
					para.removeRun(0);
				}
			}
			matcher = matcher(runText);
			if (matcher.find()) {
				while ((matcher = matcher(runText)).find()) {
					runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));
				}
				//直接调用XWPFRun的setText()方法设置文本时,在底层会重新创建一个XWPFRun,把文本附加在当前文本后面,
				//所以我们不能直接设值,需要先删除当前run,然后再自己手动插入一个新的run。
				//para.insertNewRun(0).setText(runText);//新增的没有样式
				XWPFRun run = para.createRun();
				run.setText(runText,0);
				run.setFontSize(fontSize);
				run.setUnderline(underlinePatterns);
				run.setFontFamily("仿宋");//字体
				run.setFontSize(16);//字体大小
                //run.setBold(true); //加粗
                //run.setColor("FF0000");
                //默认:宋体(wps)/等线(office2016) 5号 两端对齐 单倍间距
                //run.setBold(false);//加粗
                //run.setCapitalized(false);//我也不知道这个属性做啥的
                //run.setCharacterSpacing(5);//这个属性报错
                //run.setColor("BED4F1");//设置颜色--十六进制
                //run.setDoubleStrikethrough(false);//双删除线
                //run.setEmbossed(false);//浮雕字体----效果和印记(悬浮阴影)类似
                //run.setFontFamily("宋体");//字体
                //run.setFontFamily("华文新魏", FontCharRange.cs);//字体,范围----效果不详
                //run.setFontSize(14);//字体大小
                //run.setImprinted(false);//印迹(悬浮阴影)---效果和浮雕类似
                //run.setItalic(false);//斜体(字体倾斜)
                //run.setKerning(1);//字距调整----这个好像没有效果
                //run.setShadow(true);//阴影---稍微有点效果(阴影不明显)
                //run.setSmallCaps(true);//小型股------效果不清楚
                //run.setStrike(true);//单删除线(废弃)
                //run.setStrikeThrough(false);//单删除线(新的替换Strike)
                //run.setSubscript(VerticalAlign.SUBSCRIPT);//下标(吧当前这个run变成下标)---枚举
                //run.setTextPosition(20);//设置两行之间的行间距
                //run.setUnderline(UnderlinePatterns.DASH_LONG);//各种类型的下划线(枚举)
                //run0.addBreak();//类似换行的操作(html的  br标签)
                //run0.addTab();//tab键
                //run0.addCarriageReturn();//回车键
                //注意:addTab()和addCarriageReturn() 对setText()的使用先后顺序有关:比如先执行addTab,再写Text这是对当前这个Text的Table,反之是对下一个run的Text的Tab效果


			}
		}

	}


   /**
	* 替换表格里面的变量
	* @param doc 要替换的文档
	* @param params 参数
	*/
	private static void replaceInTable(XWPFDocument doc, Map<String, Object> params) {
		Iterator<XWPFTable> iterator = doc.getTablesIterator();
		XWPFTable table;
		List<XWPFTableRow> rows;
		List<XWPFTableCell> cells;
		List<XWPFParagraph> paras;
		while (iterator.hasNext()) {
			table = iterator.next();
			rows = table.getRows();
			for (XWPFTableRow row : rows) {
				cells = row.getTableCells();
				for (XWPFTableCell cell : cells) {
					paras = cell.getParagraphs();
					for (XWPFParagraph para : paras) {
						replaceInPara(para, params);
					}
				}
			}
		}
	}

   /**
	* 正则匹配字符串
	* @param str
	* @return
	*/
	private static Matcher matcher(String str) {
		Pattern pattern = Pattern.compile("\\$\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);
		Matcher matcher = pattern.matcher(str);
		return matcher;
	}

   /**
	* 关闭输入流
	* @param is
	*/
	private static void close(InputStream is) {
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

   /**
	* 关闭输出流
	* @param os
	*/
	private static void close(OutputStream os) {
		if (os != null) {
			try {
				os.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}


	public static InputStream getImageStream(String url) {
		try {
			HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
			connection.setReadTimeout(5000);
			connection.setConnectTimeout(5000);
			connection.setRequestMethod("GET");
			if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
				InputStream inputStream = connection.getInputStream();
				return inputStream;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}


}

getImageStream这个方法是把线上的文件,转成流

5.2 修改部分:

LicenseVerify文件中,调整证书安装的方法,调用工具类,获取线上文件转换地址

java 复制代码
/**
     * 安装License证书
     */
    public synchronized LicenseContent install(LicenseVerifyParam param){
        LicenseContent result = null;
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try{
            LicenseManager licenseManager = LicenseManagerHolder.getInstance(initLicenseParam(param));
            licenseManager.uninstall();
            InputStream stream = POIUtil.getImageStream(param.getLicensePath());
            String tempFilePath = RuoYiConfig.getProfile() + "/license_temp.lic";
            File tempFile = new File(tempFilePath);
            File file = FileUtil.writeFromStream(stream, tempFile);
            result = licenseManager.install(file);
            logger.info(MessageFormat.format("证书安装成功,证书有效期:{0} - {1}",format.format(result.getNotBefore()),format.format(result.getNotAfter())));
        }catch (Exception e){
            logger.error("证书安装失败,请检查证书是否过期,证书放置位置不正确等因素!", e);
        }

        return result;
    }

总结

这样就做完了license,感谢源码提供方

相关推荐
Marst Code5 分钟前
(Django)初步使用
后端·python·django
代码之光_198011 分钟前
SpringBoot校园资料分享平台:设计与实现
java·spring boot·后端
编程老船长24 分钟前
第26章 Java操作Mongodb实现数据持久化
数据库·后端·mongodb
IT果果日记1 小时前
DataX+Crontab实现多任务顺序定时同步
后端
科技资讯早知道1 小时前
java计算机毕设课设—坦克大战游戏
java·开发语言·游戏·毕业设计·课程设计·毕设
苹果醋32 小时前
快速玩转 Mixtral 8x7B MOE大模型!阿里云机器学习 PAI 推出最佳实践
spring boot·nginx·毕业设计·layui·课程设计
程序员大金2 小时前
基于SpringBoot+Vue+MySQL的装修公司管理系统
vue.js·spring boot·mysql
小比卡丘2 小时前
C语言进阶版第17课—自定义类型:联合和枚举
android·java·c语言
xmh-sxh-13142 小时前
java 数据存储方式
java
liu_chunhai2 小时前
设计模式(3)builder
java·开发语言·设计模式