久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot添加License的多種方式

瀏覽:4日期:2023-03-04 15:45:17
目錄第一種方案生成License使用License直接集成的方案引入Maven依賴載入License。如若License已過期,則會觸發(fā)異常。說了這么多,在演示下代碼吧總結(jié)第一種方案生成License

工具已經(jīng)封裝好,小伙伴們可以直接下載使用:https://gitee.com/lm970585581/spring-boot2-license

下載后打開cloud-license-serve項(xiàng)目直接啟動即可。

然后調(diào)用項(xiàng)目的獲取信息接口:http://localhost:9081/license/getServerInfos?osName=windows

會得到類似如下結(jié)果,分別代表ip地址、mac地址、cpu序號、主板序號。

{ 'ipAddress': ['192.168.80.1','192.168.220.1' ], 'macAddress': ['01-51-56-C0-00-01','00-52-56-C0-00-08','BC-54-2D-DF-69-FC' ], 'cpuSerial': 'BFECFBFF000806EC', 'mainBoardSerial': 'L1HF16301D5'}

使用JDK自帶的 keytool 工具生成公私鑰證書庫:

假如我們設(shè)置公鑰庫密碼為:public_password1234,私鑰庫密碼為:private_password1234,則生成命令如下:

#生成命令keytool -genkeypair -keysize 1024 -validity 3650 -alias 'privateKey' -keystore 'privateKeys.keystore' -storepass 'public_password1234' -keypass 'private_password1234' -dname 'CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN' #導(dǎo)出命令keytool -exportcert -alias 'privateKey' -keystore 'privateKeys.keystore' -storepass 'public_password1234' -file 'certfile.cer' #導(dǎo)入命令keytool -import -alias 'publicCert' -file 'certfile.cer' -keystore 'publicCerts.keystore' -storepass 'public_password1234'

上述命令執(zhí)行完成之后,會在當(dāng)前路徑下生成三個(gè)文件,分別是:privateKeys.keystore、publicCerts.keystore、certfile.cer。其中文件certfile.cer不再需要可以刪除,文件privateKeys.keystore用于當(dāng)前的 ServerDemo 項(xiàng)目給客戶生成license文件,而文件publicCerts.keystore則隨應(yīng)用代碼部署到客戶服務(wù)器,用戶解密license文件并校驗(yàn)其許可信息。

最后我們再生成license,調(diào)用接口地址為:http://localhost:9081/license/generateLicense

調(diào)用的參數(shù)是一個(gè)json參數(shù),格式如下:

{ 'subject': 'license_demo', 'privateAlias': 'privateKey', 'keyPass': 'private_password1234', 'storePass': 'public_password1234', 'licensePath': 'C:/Users/zifangsky/Desktop/license_demo/license.lic', 'privateKeysStorePath': 'C:/Users/zifangsky/Desktop/license_demo/privateKeys.keystore', 'issuedTime': '2018-07-10 00:00:01', 'expiryTime': '2019-12-31 23:59:59', 'consumerType': 'User', 'consumerAmount': 1, 'description': '這是證書描述信息', 'licenseCheckModel': {'ipAddress': ['192.168.245.1', '10.0.5.22'],'macAddress': ['00-50-56-C0-00-01', '50-7B-9D-F9-18-41'],'cpuSerial': 'BFEBFBFF000406E3','mainBoardSerial': 'L1HF65E00X9' }}

如果請求成功,那么最后會在 licensePath 參數(shù)設(shè)置的路徑生成一個(gè) license.lic 的文件,這個(gè)文件就是給客戶部署代碼的服務(wù)器許可文件。

使用License

如果小伙伴們按照上文的步驟一步一步的跟著實(shí)現(xiàn),我們已經(jīng)獲得了license.lic,接下來就是把license使用到我們自己的項(xiàng)目中了。

cloud-license-client就是引入項(xiàng)目的一個(gè)例子,打開可以直接使用。

引入自己的項(xiàng)目只需將以下文件導(dǎo)入

SpringBoot添加License的多種方式

并配置好攔截器LicenseCheckInterceptor就可以使用了。配置方法在InterceptorConfig類中,可以參考。

這里需要注意的是使用license需要兩個(gè)文件:license.lic,publicCerts.keystore

演示項(xiàng)目配置的路徑是絕對路徑,一般我們會配置相對路徑,把兩個(gè)文件放到項(xiàng)目下,配置位置在LicenseCheckListener類中

修改如下部分改為相對路徑讀取就可以了

SpringBoot添加License的多種方式

這里就不演示如何修改了,因?yàn)樾薷钠饋砗苋菀住?/p>

還需要注意一點(diǎn):

對于LicenseCheckModel,LicenseCreatorParam兩個(gè)類,引入到自己的客戶端后一定要保證包名與生成license時(shí)的包名一致,不然會導(dǎo)致序列化失敗的問題。

直接集成的方案引入Maven依賴

<dependency> <groupId>org.smartboot.license</groupId> <artifactId>license-client</artifactId> <version>1.0.3</version></dependency>載入License。如若License已過期,則會觸發(fā)異常。

public class LicenseTest { public static void main(String[] args) throws Exception { File file=new File('license.txt'); License license = new License(); LicenseEntity licenseEntity=license.loadLicense(file); System.out.println(new String(licenseEntity.getData())); }}獲取licenseEntity并以此配置啟動軟件。還原license 進(jìn)入bin目錄執(zhí)行以下命令,例如:./license_revert.sh source.txt。 執(zhí)行成功后會在當(dāng)前目錄下生成License文件license_revert.txt。

簡單方便,幾行代碼放在啟動方法里校驗(yàn),也可以加注在攔截器里。

一個(gè)簡單方便的授權(quán)方式,只需以上幾步就可集成到boot項(xiàng)目中去啦!

說了這么多,在演示下代碼吧

生成機(jī)器碼

我們首先要做的就是對軟件部署的環(huán)境的唯一性進(jìn)行限制,這里使用的是macadderss,當(dāng)然你也可以換成cpu序列編號,并無太大影響,先上代碼

private static String getMac() {try { Enumeration<NetworkInterface> el = NetworkInterface .getNetworkInterfaces(); while (el.hasMoreElements()) {byte[] mac = el.nextElement().getHardwareAddress();if (mac == null) continue;String hexstr = bytesToHexString(mac);return getSplitString(hexstr, '-', 2).toUpperCase(); }} catch (Exception exception) { exception.printStackTrace();}return null; } public static String getMachineCode() throws Exception{Set<String> result = new HashSet<>();String mac = getMac();result.add(mac);Properties props = System.getProperties();String javaVersion = props.getProperty('java.version');result.add(javaVersion);String javaVMVersion = props.getProperty('java.vm.version');result.add(javaVMVersion);String osVersion = props.getProperty('os.version');result.add(osVersion);String code = Encrpt.GetMD5Code(result.toString());return getSplitString(code, '-', 4); }

這里進(jìn)行的操作是取出機(jī)器碼,與java版本,jvm,操作系統(tǒng)參數(shù)進(jìn)行混合,并進(jìn)行MD5操作

進(jìn)行l(wèi)ic文件的生成

授權(quán)證書主要包含三個(gè)要素,機(jī)器碼,是否永久有效標(biāo)識,證書時(shí)效,我們會將這些數(shù)據(jù)寫入文本中并進(jìn)行加密處理,看下生成證書的代碼

public static void getLicense(String isNoTimeLimit, String licenseLimit, String machineCode, String licensePath, String priavateKeyPath) throws Exception{String[] liccontent = {'LICENSEID=yanpeng19940119@gmail.com','LICENSENAME=YBLOG使用證書',MessageFormat.format('LICENSETYPE={0}',isNoTimeLimit),MessageFormat.format('EXPIREDAY={0}',licenseLimit), //日期采用yyyy-MM-dd日期格式MessageFormat.format('MACHINECODE={0}',machineCode),''}; //將lic內(nèi)容進(jìn)行混合簽名并寫入內(nèi)容StringBuilder sign = new StringBuilder();for(String item:liccontent){ sign.append(item+'yblog');}liccontent[5] = MessageFormat.format('LICENSESIGN={0}',Encrpt.GetMD5Code(sign.toString()));FileUtil.createFileAndWriteLines(licensePath,liccontent);//將寫入的內(nèi)容整體加密替換String filecontent =FileUtil.readFileToString(licensePath);String encrptfilecontent = Encrpt.EncriptWRSA_Pri(filecontent,priavateKeyPath);File file = new File(licensePath);file.delete();FileUtil.createFile(licensePath,encrptfilecontent);

最后在驗(yàn)證lic,我們會在系統(tǒng)中注冊一個(gè)攔截器,未通過系統(tǒng)授權(quán)認(rèn)證會自動跳轉(zhuǎn)到lic文件上傳界面,springboot接收文件與常規(guī)java有一些不同,使用的MultipartFile對象,會獲取到上傳文件的數(shù)組,進(jìn)行操作。

我們就可以通過系統(tǒng)內(nèi)置的公鑰對lic文件的機(jī)器碼,授權(quán)時(shí)間進(jìn)行驗(yàn)證,確定是否能正常訪問系統(tǒng)。

總結(jié)

好了,到這里本文的分享就結(jié)束了,本文分享的其實(shí)是License的使用說明,并沒有帶大家閱讀源碼去看原理,感興趣的小伙伴可以自行閱讀一下項(xiàng)目源碼,也很容易看懂哦。

以上就是SpringBoot生成License的多種實(shí)現(xiàn)方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot生成License的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 通江县| 内江市| 平江县| 苍溪县| 德州市| 南川市| 葵青区| 林甸县| 湄潭县| 修文县| 湟中县| 万源市| 芒康县| 灵川县| 南安市| 鹰潭市| 建昌县| 南投市| 合山市| 宝应县| 泰来县| 白水县| 永定县| 临洮县| 襄城县| 墨竹工卡县| 晋城| 大邑县| 新平| 庆元县| 宜宾市| 徐闻县| 晋州市| 绍兴县| 冷水江市| 云龙县| 迁安市| 晋宁县| 璧山县| 十堰市| 金溪县|