当前位置: 首页 > news >正文

spring boot添加License(软件许可)

文章目录

  • 前言
  • 1. 生成钥匙库
  • 2. 生成证书
  • 3. 生成公匙库
  • 4.业务代码
    • 1. 引入依赖
    • 2. 关键代码
    • 3. 配置文件
  • 5、改成线上地址,这样不用每次打包,发送license.lic文件给客户,重启项目就行
    • 5.1、工具类
    • 5.2 修改部分:
  • 总结


前言

工作需要给软件加上许可

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

1. 生成钥匙库

# 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. 生成证书


# 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. 生成公匙库


# 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. 引入依赖

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

2. 关键代码

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

/*** 自定义KeyStoreParam*/
public class CustomKeyStoreParam extends AbstractKeyStoreParam {// ...省略代码/*** 重写getStream()方法* @return* @throws IOException*/@Overridepublic 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鉴权,也就没有可能请求成功其他接口。

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

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

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

3. 配置文件

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

#License相关配置
license:#License如果是本地地址,则改成本地地址,我这里改成线上地址了licenseFile: http://192.168.110.188:9000/iot/2024/05/21/license.licsubject: MyLicenseDemopublicAlias: publicCertstorePass: zhiutech@123# 公钥地址publicKeysStoreFile: C:/Users/Administrator/Desktop/license/publicCerts.keystoregenerateLicensePath: C:/Users/Administrator/Desktop/license/license.lic

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

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

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

5.1、工具类

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转FileItemFileItemFactory 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转MultipartFileMultipartFile 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文件中,调整证书安装的方法,调用工具类,获取线上文件转换地址

/*** 安装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,感谢源码提供方

http://www.lryc.cn/news/355746.html

相关文章:

  • LangChain打造一个AI客服
  • 【前端三剑客之JS】详解JS
  • 重庆耶非凡科技有限公司有选品师项目培训吗?
  • 格式转化——Labelme标注好的json文件批量转为png(标签)文件(物体为红色,背景为黑色)和jpg原图
  • 力扣刷题--2535. 数组元素和与数字和的绝对差【简单】
  • 2024年【危险化学品经营单位安全管理人员】考试报名及危险化学品经营单位安全管理人员找解析
  • IntelliJ IDEA集成Baidu Comate,商城系统支付交易功能开发实战
  • 20212313 2023-2024-2 《移动平台开发与实践》第5次作业
  • Python图形界面(GUI)Tkinter笔记(十二):用【Entry()】实现单行文本输入(3)
  • 前端渲染页面的原理
  • 【一竞技DOTA2】RAMZES666替补参加裂变联赛
  • 1109 擅长C(测试点0,1,2,3)
  • 北京新高度画室:端午假期免费吃,住,学!
  • 电脑重要文件如何加密保护?教你两种方法
  • 新零售收银解决方案:传统门店超市的数字化-亿发
  • 独家揭秘!Amazon、lazada、Shopee测评自养号,新手也能秒变高手!
  • 企企通入选第一新声《2024年中国CIO数字化产品选型白皮书》供应链数字产品可信名录
  • Linux中 “权限设置修改”
  • 9.1 Go语言入门(环境篇)
  • 简单的网页分享按钮代码
  • ld链接文件
  • React-事件绑定
  • 使用“tcpdump”查看原始数据包
  • Python字符串处理全面教程
  • 基于微信小程序+ JAVA后端实现的【微信小程序跑腿平台】设计与实现 (内附设计LW + PPT+ 源码+ 演示视频 下载)
  • 使用 VALUES 子句构建数据集
  • for循环绑定id,更新html页面的文字内容
  • claude3国内API接口对接
  • Java:IO
  • ubuntu安全加固