SpringBoot添加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)入
并配置好攔截器LicenseCheckInterceptor就可以使用了。配置方法在InterceptorConfig類中,可以參考。
這里需要注意的是使用license需要兩個(gè)文件:license.lic,publicCerts.keystore
演示項(xiàng)目配置的路徑是絕對路徑,一般我們會配置相對路徑,把兩個(gè)文件放到項(xiàng)目下,配置位置在LicenseCheckListener類中
修改如下部分改為相對路徑讀取就可以了
這里就不演示如何修改了,因?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)文章!
相關(guān)文章:
1. vue實(shí)現(xiàn)web在線聊天功能2. 完美解決vue 中多個(gè)echarts圖表自適應(yīng)的問題3. JavaScript實(shí)現(xiàn)頁面動態(tài)驗(yàn)證碼的實(shí)現(xiàn)示例4. 解決Android Studio 格式化 Format代碼快捷鍵問題5. JavaEE SpringMyBatis是什么? 它和Hibernate的區(qū)別及如何配置MyBatis6. Java使用Tesseract-Ocr識別數(shù)字7. Python使用urlretrieve實(shí)現(xiàn)直接遠(yuǎn)程下載圖片的示例代碼8. 在Chrome DevTools中調(diào)試JavaScript的實(shí)現(xiàn)9. Springboot 全局日期格式化處理的實(shí)現(xiàn)10. SpringBoot+TestNG單元測試的實(shí)現(xiàn)
