SpringBoot添加License的多種方式
工具已經(jīng)封裝好,小伙伴們可以直接下載使用:https://gitee.com/lm970585581/spring-boot2-license
下載后打開(kāi)cloud-license-serve項(xiàng)目直接啟動(dòng)即可。
然后調(diào)用項(xiàng)目的獲取信息接口:http://localhost:9081/license/getServerInfos?osName=windows
會(huì)得到類(lèi)似如下結(jié)果,分別代表ip地址、mac地址、cpu序號(hào)、主板序號(hào)。
{ '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ū)庫(kù):
假如我們?cè)O(shè)置公鑰庫(kù)密碼為:public_password1234,私鑰庫(kù)密碼為: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í)行完成之后,會(huì)在當(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)其許可信息。
最后我們?cè)偕蒷icense,調(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': '這是證書(shū)描述信息', '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' }}
如果請(qǐng)求成功,那么最后會(huì)在 licensePath 參數(shù)設(shè)置的路徑生成一個(gè) license.lic 的文件,這個(gè)文件就是給客戶部署代碼的服務(wù)器許可文件。
使用License如果小伙伴們按照上文的步驟一步一步的跟著實(shí)現(xiàn),我們已經(jīng)獲得了license.lic,接下來(lái)就是把license使用到我們自己的項(xiàng)目中了。
cloud-license-client就是引入項(xiàng)目的一個(gè)例子,打開(kāi)可以直接使用。
引入自己的項(xiàng)目只需將以下文件導(dǎo)入
并配置好攔截器LicenseCheckInterceptor就可以使用了。配置方法在InterceptorConfig類(lèi)中,可以參考。
這里需要注意的是使用license需要兩個(gè)文件:license.lic,publicCerts.keystore
演示項(xiàng)目配置的路徑是絕對(duì)路徑,一般我們會(huì)配置相對(duì)路徑,把兩個(gè)文件放到項(xiàng)目下,配置位置在LicenseCheckListener類(lèi)中
修改如下部分改為相對(duì)路徑讀取就可以了
這里就不演示如何修改了,因?yàn)樾薷钠饋?lái)很容易。
還需要注意一點(diǎn):
對(duì)于LicenseCheckModel,LicenseCreatorParam兩個(gè)類(lèi),引入到自己的客戶端后一定要保證包名與生成license時(shí)的包名一致,不然會(huì)導(dǎo)致序列化失敗的問(wèn)題。
直接集成的方案引入Maven依賴<dependency> <groupId>org.smartboot.license</groupId> <artifactId>license-client</artifactId> <version>1.0.3</version></dependency>載入License。如若License已過(guò)期,則會(huì)觸發(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并以此配置啟動(dòng)軟件。還原license 進(jìn)入bin目錄執(zhí)行以下命令,例如:./license_revert.sh source.txt。 執(zhí)行成功后會(huì)在當(dāng)前目錄下生成License文件license_revert.txt。
簡(jiǎn)單方便,幾行代碼放在啟動(dòng)方法里校驗(yàn),也可以加注在攔截器里。
一個(gè)簡(jiǎn)單方便的授權(quán)方式,只需以上幾步就可集成到boot項(xiàng)目中去啦!
說(shuō)了這么多,在演示下代碼吧生成機(jī)器碼
我們首先要做的就是對(duì)軟件部署的環(huán)境的唯一性進(jìn)行限制,這里使用的是macadderss,當(dāng)然你也可以換成cpu序列編號(hào),并無(wú)太大影響,先上代碼
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)證書(shū)主要包含三個(gè)要素,機(jī)器碼,是否永久有效標(biāo)識(shí),證書(shū)時(shí)效,我們會(huì)將這些數(shù)據(jù)寫(xiě)入文本中并進(jìn)行加密處理,看下生成證書(shū)的代碼
public static void getLicense(String isNoTimeLimit, String licenseLimit, String machineCode, String licensePath, String priavateKeyPath) throws Exception{String[] liccontent = {'LICENSEID=yanpeng19940119@gmail.com','LICENSENAME=YBLOG使用證書(shū)',MessageFormat.format('LICENSETYPE={0}',isNoTimeLimit),MessageFormat.format('EXPIREDAY={0}',licenseLimit), //日期采用yyyy-MM-dd日期格式MessageFormat.format('MACHINECODE={0}',machineCode),''}; //將lic內(nèi)容進(jìn)行混合簽名并寫(xiě)入內(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);//將寫(xiě)入的內(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,我們會(huì)在系統(tǒng)中注冊(cè)一個(gè)攔截器,未通過(guò)系統(tǒng)授權(quán)認(rèn)證會(huì)自動(dòng)跳轉(zhuǎn)到lic文件上傳界面,springboot接收文件與常規(guī)java有一些不同,使用的MultipartFile對(duì)象,會(huì)獲取到上傳文件的數(shù)組,進(jìn)行操作。
我們就可以通過(guò)系統(tǒng)內(nèi)置的公鑰對(duì)lic文件的機(jī)器碼,授權(quán)時(shí)間進(jìn)行驗(yàn)證,確定是否能正常訪問(wèn)系統(tǒng)。
總結(jié)好了,到這里本文的分享就結(jié)束了,本文分享的其實(shí)是License的使用說(shuō)明,并沒(méi)有帶大家閱讀源碼去看原理,感興趣的小伙伴可以自行閱讀一下項(xiàng)目源碼,也很容易看懂哦。
以上就是SpringBoot生成License的多種實(shí)現(xiàn)方式的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot生成License的資料請(qǐng)關(guān)注好吧啦網(wǎng)其它相關(guān)文章!
相關(guān)文章:
1. 解決Python 進(jìn)程池Pool中一些坑2. Python如何讀寫(xiě)CSV文件3. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究4. 三個(gè)不常見(jiàn)的 HTML5 實(shí)用新特性簡(jiǎn)介5. 無(wú)線標(biāo)記語(yǔ)言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁(yè)6. ajax請(qǐng)求添加自定義header參數(shù)代碼7. php測(cè)試程序運(yùn)行速度和頁(yè)面執(zhí)行速度的代碼8. Python獲取抖音關(guān)注列表封號(hào)賬號(hào)的實(shí)現(xiàn)代碼9. python利用os模塊編寫(xiě)文件復(fù)制功能——copy()函數(shù)用法10. Python使用jupyter notebook查看ipynb文件過(guò)程解析
