<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    ivaneeo's blog

    自由的力量,自由的生活。

      BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
      669 Posts :: 0 Stories :: 64 Comments :: 0 Trackbacks

    使用truelicense實現用于JAVA工程license機制(包括license生成和驗,有需要的朋友可以參考下。


    開發的軟件產品在交付使用的時候,往往會授權一段時間的試用期,這個時候license就派上用場了。不同于在代碼中直接加上時間約束,需要重新授權的時候使用license可以避免修改源碼,改動部署,授權方直接生成一個新的license發送給使用方替換掉原來的license文件即可。下面將講述使用truelicense來實現license的生成和使用。Truelicense是一個開源的證書管理引擎,詳細介紹見https://truelicense.java.net/

    一、首先介紹下license授權機制的原理:

    1、 生成密鑰對,方法有很多。

    2、 授權者保留私鑰,使用私鑰對包含授權信息(如使用截止日期,MAC地址等)的license進行數字簽名。

    3、 公鑰給使用者(放在驗證的代碼中使用),用于驗證license是否符合使用條件。

    接下來是本例制作license的具體步驟:

    二、第一步:使用keytool生成密鑰對

    以下命令在dos命令行執行,注意當前執行目錄,最后生成的密鑰對即在該目錄下:

    1、首先要用KeyTool工具來生成私匙庫:(-alias別名 –validity 3650表示10年有效)

    keytool -genkey -alias privatekey -keystoreprivateKeys.store -validity 3650

    2、然后把私匙庫內的公匙導出到一個文件當中:

    keytool -export -alias privatekey -file certfile.cer -keystore privateKeys.store

    3、然后再把這個證書文件導入到公匙庫:

    keytool -import -alias publiccert -file certfile.cer -keystore publicCerts.store

    最后生成文件privateKeys.store、publicCerts.store拷貝出來備用。

    三、第二步:生成證書(該部分代碼由授權者獨立保管執行)

    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是一個證書文件的固有格式,詳見API 	private final static X500Principal DEFAULTHOLDERANDISSUER = new X500Principal( 			"CN=Duke、OU=JavaSoft、O=Sun Microsystems、C=US"); 	 	public void setParam(String propertiesPath) { 		// 獲取參數 		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 { 			/************** 證書發布者端執行 ******************/ 			LicenseManager licenseManager = LicenseManagerHolder 					.getLicenseManager(initLicenseParams0()); 			licenseManager.store((createLicenseContent()), new File(licPath));	 		} catch (Exception e) { 			e.printStackTrace(); 			System.out.println("客戶端證書生成失敗!"); 			return false; 		} 		System.out.println("服務器端生成證書成功!"); 		return true; 	}  	// 返回生成證書時需要的參數 	private static LicenseParam initLicenseParams0() { 		Preferences preference = Preferences 				.userNodeForPackage(CreateLicense.class); 		// 設置對證書內容加密的對稱密碼 		CipherParam cipherParam = new DefaultCipherParam(STOREPWD); 		// 參數1,2從哪個Class.getResource()獲得密鑰庫;參數3密鑰庫的別名;參數4密鑰庫存儲密碼;參數5密鑰庫密碼 		KeyStoreParam privateStoreParam = new DefaultKeyStoreParam( 				CreateLicense.class, priPath, PRIVATEALIAS, STOREPWD, KEYPWD); 		LicenseParam licenseParams = new DefaultLicenseParam(SUBJECT, 				preference, privateStoreParam, cipherParam); 		return licenseParams; 	}  	// 從外部表單拿到證書的內容 		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); 			// 擴展 			content.setExtra(new Object()); 			return content; 		} } 

    3、 測試程序licenseCreateTest.java:

    package cn.melina.license; import cn.melina.license.CreateLicense; public class licenseCreateTest { 	public static void main(String[] args){ 		CreateLicense cLicense = new CreateLicense(); 		//獲取參數 		cLicense.setParam("./param.properties"); 		//生成證書 		cLicense.create(); 	} } 

    4、 生成時使用到的param.properties文件如下:

    ##########common parameters########### #alias PRIVATEALIAS=privatekey #key(該密碼生成密鑰對的密碼,需要妥善保管,不能讓使用者知道) KEYPWD=bigdata123456 #STOREPWD(該密碼是在使用keytool生成密鑰對時設置的密鑰庫的訪問密碼) 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 

    根據properties文件可以看出,這里只簡單設置了使用時間的限制,當然可以自定義添加更多限制。該文件中表示授權者擁有私鑰,并且知道生成密鑰對的密碼。并且設置license的內容。

    四、第三步:驗證證書(使用證書)(該部分代碼結合需要授權的程序使用)

    1、 首先LicenseManagerHolder.java類,同上。

    2、 然后是主要驗證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) { 		// 獲取參數 		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() {		 		/************** 證書使用者端執行 ******************/  		LicenseManager licenseManager = LicenseManagerHolder 				.getLicenseManager(initLicenseParams()); 		// 安裝證書 		try { 			licenseManager.install(new File(licPath)); 			System.out.println("客戶端安裝證書成功!"); 		} catch (Exception e) { 			e.printStackTrace(); 			System.out.println("客戶端證書安裝失敗!"); 			return false; 		} 		// 驗證證書 		try { 			licenseManager.verify(); 			System.out.println("客戶端驗證證書成功!"); 		} catch (Exception e) { 			e.printStackTrace(); 			System.out.println("客戶端證書驗證失效!"); 			return false; 		} 		return true; 	}  	// 返回驗證證書需要的參數 	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、 測試程序licenseVerifyTest.java:

    package cn.melina.license;  public class licenseVerifyTest { 	public static void main(String[] args){ 		VerifyLicense vLicense = new VerifyLicense(); 		//獲取參數 		vLicense.setParam("./param.properties"); 		//驗證證書 		vLicense.verify(); 	} } 

    4、 驗證時使用到的Properties文件如下:

    ##########common parameters########### #alias PUBLICALIAS=publiccert #STOREPWD(該密碼是在使用keytool生成密鑰對時設置的密鑰庫的訪問密碼) STOREPWD=abc123456 #SUBJECT SUBJECT=bigdata #licPath licPath=bigdata.lic #pubPath pubPath=publicCerts.store 

    根據該驗證的properties可以看出,使用者只擁有公鑰,沒有私鑰,并且也只知道訪問密鑰庫的密碼,而不能知道生成密鑰對的密碼。

    五、說明:

    注意實際操作中,公鑰、私鑰、證書等文件的存放路徑。

    以上代碼需要用到truelicense的一些包,可以自行網上搜,也可以下載我的完整工程,里面附帶了所需的jar包。

    以上兩個完整工程提供下載:http://download.csdn.net/detail/luckymelina/7141131

    GOOD LUCK!小伙伴們加油!歡迎與我交流。

    posted on 2014-04-30 03:32 ivaneeo 閱讀(6582) 評論(0)  編輯  收藏 所屬分類: java魔力
    主站蜘蛛池模板: 免费A级毛片在线播放| 好吊色永久免费视频大全| 久久综合国产乱子伦精品免费 | 99久久久国产精品免费无卡顿| 亚洲AV无码专区国产乱码电影| 黄 色一级 成 人网站免费| 亚洲成A人片在线观看中文| 色屁屁在线观看视频免费| 日本人护士免费xxxx视频| 美女视频黄a视频全免费网站一区 美女视频黄a视频全免费网站色 | 亚洲人成影院在线高清| 5555在线播放免费播放| 亚洲春色另类小说| AA免费观看的1000部电影| 午夜在线a亚洲v天堂网2019| 成人免费a级毛片无码网站入口| 亚洲综合av一区二区三区不卡| 成人激情免费视频| 国产亚洲精品第一综合| 亚洲精品无码日韩国产不卡?V | 一级毛片全部免费播放| 亚洲精品福利在线观看| 成人午夜大片免费7777| 大桥未久亚洲无av码在线| 亚洲国产人成中文幕一级二级| 中国一级毛片视频免费看| 亚洲国产精品久久久久婷婷软件 | 亚洲欧美成人一区二区三区| 精品免费国产一区二区三区| 羞羞的视频在线免费观看| 亚洲成Av人片乱码色午夜| 最近免费中文字幕大全免费版视频 | 免费中文熟妇在线影片| 大桥未久亚洲无av码在线| 在线精品亚洲一区二区三区| 99久久免费看国产精品| 亚洲人成色777777精品| 中文字幕第一页亚洲| 国产曰批免费视频播放免费s| 亚洲成a人无码亚洲成av无码| 亚洲午夜久久久久久久久久|