使用truelicense實(shí)現(xiàn)用于JAVA工程license機(jī)制(包括license生成和驗(yàn),有需要的朋友可以參考下。
開發(fā)的軟件產(chǎn)品在交付使用的時(shí)候,往往會(huì)授權(quán)一段時(shí)間的試用期,這個(gè)時(shí)候license就派上用場(chǎng)了。不同于在代碼中直接加上時(shí)間約束,需要重新授權(quán)的時(shí)候使用license可以避免修改源碼,改動(dòng)部署,授權(quán)方直接生成一個(gè)新的license發(fā)送給使用方替換掉原來(lái)的license文件即可。下面將講述使用truelicense來(lái)實(shí)現(xiàn)license的生成和使用。Truelicense是一個(gè)開源的證書管理引擎,詳細(xì)介紹見(jiàn)https://truelicense.java.net/
一、首先介紹下license授權(quán)機(jī)制的原理:
1、 生成密鑰對(duì),方法有很多。
2、 授權(quán)者保留私鑰,使用私鑰對(duì)包含授權(quán)信息(如使用截止日期,MAC地址等)的license進(jìn)行數(shù)字簽名。
3、 公鑰給使用者(放在驗(yàn)證的代碼中使用),用于驗(yàn)證license是否符合使用條件。
接下來(lái)是本例制作license的具體步驟:
二、第一步:使用keytool生成密鑰對(duì)
以下命令在dos命令行執(zhí)行,注意當(dāng)前執(zhí)行目錄,最后生成的密鑰對(duì)即在該目錄下:
1、首先要用KeyTool工具來(lái)生成私匙庫(kù):(-alias別名 –validity 3650表示10年有效)
keytool -genkey -alias privatekey -keystoreprivateKeys.store -validity 3650
2、然后把私匙庫(kù)內(nèi)的公匙導(dǎo)出到一個(gè)文件當(dāng)中:
keytool -export -alias privatekey -file certfile.cer -keystore privateKeys.store
3、然后再把這個(gè)證書文件導(dǎo)入到公匙庫(kù):
keytool -import -alias publiccert -file certfile.cer -keystore publicCerts.store
最后生成文件privateKeys.store、publicCerts.store拷貝出來(lái)備用。
三、第二步:生成證書(該部分代碼由授權(quán)者獨(dú)立保管執(zhí)行)
1、 首先LicenseManagerHolder.java類:
package cn.melina.license; import de.schlichtherle.license.LicenseManager; import de.schlichtherle.license.LicenseParam; /** * LicenseManager?????? * @author melina */ public class LicenseManagerHolder { private static LicenseManager licenseManager; public static synchronized LicenseManager getLicenseManager(LicenseParam licenseParams) { if (licenseManager == null) { licenseManager = new LicenseManager(licenseParams); } return licenseManager; } }
2、 然后是主要生成license的代碼CreateLicense.java:
package cn.melina.license; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.text.DateFormat; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Properties; import java.util.prefs.Preferences; import javax.security.auth.x500.X500Principal; import de.schlichtherle.license.CipherParam; import de.schlichtherle.license.DefaultCipherParam; import de.schlichtherle.license.DefaultKeyStoreParam; import de.schlichtherle.license.DefaultLicenseParam; import de.schlichtherle.license.KeyStoreParam; import de.schlichtherle.license.LicenseContent; import de.schlichtherle.license.LicenseParam; import de.schlichtherle.license.LicenseManager; /** * CreateLicense * @author melina */ public class CreateLicense { //common param private static String PRIVATEALIAS = ""; private static String KEYPWD = ""; private static String STOREPWD = ""; private static String SUBJECT = ""; private static String licPath = ""; private static String priPath = ""; //license content private static String issuedTime = ""; private static String notBefore = ""; private static String notAfter = ""; private static String consumerType = ""; private static int consumerAmount = 0; private static String info = ""; // 為了方便直接用的API里的例子 // X500Princal是一個(gè)證書文件的固有格式,詳見(jiàn)API private final static X500Principal DEFAULTHOLDERANDISSUER = new X500Principal( "CN=Duke、OU=JavaSoft、O=Sun Microsystems、C=US"); public void setParam(String propertiesPath) { // 獲取參數(shù) Properties prop = new Properties(); InputStream in = getClass().getResourceAsStream(propertiesPath); try { prop.load(in); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } PRIVATEALIAS = prop.getProperty("PRIVATEALIAS"); KEYPWD = prop.getProperty("KEYPWD"); STOREPWD = prop.getProperty("STOREPWD"); SUBJECT = prop.getProperty("SUBJECT"); KEYPWD = prop.getProperty("KEYPWD"); licPath = prop.getProperty("licPath"); priPath = prop.getProperty("priPath"); //license content issuedTime = prop.getProperty("issuedTime"); notBefore = prop.getProperty("notBefore"); notAfter = prop.getProperty("notAfter"); consumerType = prop.getProperty("consumerType"); consumerAmount = Integer.valueOf(prop.getProperty("consumerAmount")); info = prop.getProperty("info"); } public boolean create() { try { /************** 證書發(fā)布者端執(zhí)行 ******************/ LicenseManager licenseManager = LicenseManagerHolder .getLicenseManager(initLicenseParams0()); licenseManager.store((createLicenseContent()), new File(licPath)); } catch (Exception e) { e.printStackTrace(); System.out.println("客戶端證書生成失敗!"); return false; } System.out.println("服務(wù)器端生成證書成功!"); return true; } // 返回生成證書時(shí)需要的參數(shù) private static LicenseParam initLicenseParams0() { Preferences preference = Preferences .userNodeForPackage(CreateLicense.class); // 設(shè)置對(duì)證書內(nèi)容加密的對(duì)稱密碼 CipherParam cipherParam = new DefaultCipherParam(STOREPWD); // 參數(shù)1,2從哪個(gè)Class.getResource()獲得密鑰庫(kù);參數(shù)3密鑰庫(kù)的別名;參數(shù)4密鑰庫(kù)存儲(chǔ)密碼;參數(shù)5密鑰庫(kù)密碼 KeyStoreParam privateStoreParam = new DefaultKeyStoreParam( CreateLicense.class, priPath, PRIVATEALIAS, STOREPWD, KEYPWD); LicenseParam licenseParams = new DefaultLicenseParam(SUBJECT, preference, privateStoreParam, cipherParam); return licenseParams; } // 從外部表單拿到證書的內(nèi)容 public final static LicenseContent createLicenseContent() { DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); LicenseContent content = null; content = new LicenseContent(); content.setSubject(SUBJECT); content.setHolder(DEFAULTHOLDERANDISSUER); content.setIssuer(DEFAULTHOLDERANDISSUER); try { content.setIssued(format.parse(issuedTime)); content.setNotBefore(format.parse(notBefore)); content.setNotAfter(format.parse(notAfter)); } catch (ParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } content.setConsumerType(consumerType); content.setConsumerAmount(consumerAmount); content.setInfo(info); // 擴(kuò)展 content.setExtra(new Object()); return content; } }
3、 測(cè)試程序licenseCreateTest.java:
package cn.melina.license; import cn.melina.license.CreateLicense; public class licenseCreateTest { public static void main(String[] args){ CreateLicense cLicense = new CreateLicense(); //獲取參數(shù) cLicense.setParam("./param.properties"); //生成證書 cLicense.create(); } }
4、 生成時(shí)使用到的param.properties文件如下:
##########common parameters########### #alias PRIVATEALIAS=privatekey #key(該密碼生成密鑰對(duì)的密碼,需要妥善保管,不能讓使用者知道) KEYPWD=bigdata123456 #STOREPWD(該密碼是在使用keytool生成密鑰對(duì)時(shí)設(shè)置的密鑰庫(kù)的訪問(wèn)密碼) STOREPWD=abc123456 #SUBJECT SUBJECT=bigdata #licPath licPath=bigdata.lic #priPath priPath=privateKeys.store ##########license content########### #issuedTime issuedTime=2014-04-01 #notBeforeTime notBefore=2014-04-01 #notAfterTime notAfter=2014-05-01 #consumerType consumerType=user #ConsumerAmount consumerAmount=1 #info info=this is a license
根據(jù)properties文件可以看出,這里只簡(jiǎn)單設(shè)置了使用時(shí)間的限制,當(dāng)然可以自定義添加更多限制。該文件中表示授權(quán)者擁有私鑰,并且知道生成密鑰對(duì)的密碼。并且設(shè)置license的內(nèi)容。
四、第三步:驗(yàn)證證書(使用證書)(該部分代碼結(jié)合需要授權(quán)的程序使用)
1、 首先LicenseManagerHolder.java類,同上。
2、 然后是主要驗(yàn)證license的代碼VerifyLicense.java:
package cn.melina.license; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.prefs.Preferences; import de.schlichtherle.license.CipherParam; import de.schlichtherle.license.DefaultCipherParam; import de.schlichtherle.license.DefaultKeyStoreParam; import de.schlichtherle.license.DefaultLicenseParam; import de.schlichtherle.license.KeyStoreParam; import de.schlichtherle.license.LicenseParam; import de.schlichtherle.license.LicenseManager; /** * VerifyLicense * @author melina */ public class VerifyLicense { //common param private static String PUBLICALIAS = ""; private static String STOREPWD = ""; private static String SUBJECT = ""; private static String licPath = ""; private static String pubPath = ""; public void setParam(String propertiesPath) { // 獲取參數(shù) Properties prop = new Properties(); InputStream in = getClass().getResourceAsStream(propertiesPath); try { prop.load(in); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } PUBLICALIAS = prop.getProperty("PUBLICALIAS"); STOREPWD = prop.getProperty("STOREPWD"); SUBJECT = prop.getProperty("SUBJECT"); licPath = prop.getProperty("licPath"); pubPath = prop.getProperty("pubPath"); } public boolean verify() { /************** 證書使用者端執(zhí)行 ******************/ LicenseManager licenseManager = LicenseManagerHolder .getLicenseManager(initLicenseParams()); // 安裝證書 try { licenseManager.install(new File(licPath)); System.out.println("客戶端安裝證書成功!"); } catch (Exception e) { e.printStackTrace(); System.out.println("客戶端證書安裝失敗!"); return false; } // 驗(yàn)證證書 try { licenseManager.verify(); System.out.println("客戶端驗(yàn)證證書成功!"); } catch (Exception e) { e.printStackTrace(); System.out.println("客戶端證書驗(yàn)證失效!"); return false; } return true; } // 返回驗(yàn)證證書需要的參數(shù) private static LicenseParam initLicenseParams() { Preferences preference = Preferences .userNodeForPackage(VerifyLicense.class); CipherParam cipherParam = new DefaultCipherParam(STOREPWD); KeyStoreParam privateStoreParam = new DefaultKeyStoreParam( VerifyLicense.class, pubPath, PUBLICALIAS, STOREPWD, null); LicenseParam licenseParams = new DefaultLicenseParam(SUBJECT, preference, privateStoreParam, cipherParam); return licenseParams; } }
3、 測(cè)試程序licenseVerifyTest.java:
package cn.melina.license; public class licenseVerifyTest { public static void main(String[] args){ VerifyLicense vLicense = new VerifyLicense(); //獲取參數(shù) vLicense.setParam("./param.properties"); //驗(yàn)證證書 vLicense.verify(); } }
4、 驗(yàn)證時(shí)使用到的Properties文件如下:
##########common parameters########### #alias PUBLICALIAS=publiccert #STOREPWD(該密碼是在使用keytool生成密鑰對(duì)時(shí)設(shè)置的密鑰庫(kù)的訪問(wèn)密碼) STOREPWD=abc123456 #SUBJECT SUBJECT=bigdata #licPath licPath=bigdata.lic #pubPath pubPath=publicCerts.store
根據(jù)該驗(yàn)證的properties可以看出,使用者只擁有公鑰,沒(méi)有私鑰,并且也只知道訪問(wèn)密鑰庫(kù)的密碼,而不能知道生成密鑰對(duì)的密碼。
五、說(shuō)明:
注意實(shí)際操作中,公鑰、私鑰、證書等文件的存放路徑。
以上代碼需要用到truelicense的一些包,可以自行網(wǎng)上搜,也可以下載我的完整工程,里面附帶了所需的jar包。
以上兩個(gè)完整工程提供下載:http://download.csdn.net/detail/luckymelina/7141131
GOOD LUCK!小伙伴們加油!歡迎與我交流。