True-License的示範用例

來源:互聯網
上載者:User

首先要用KeyTool工具來產生私匙庫:

keytool -genkey -alias privatekey -keystore privateKeys.store
然後把私匙庫內的公匙匯出到一個檔案當中:

keytool -export -alias privatekey -file certfile.cer -keystore privateKeys.store
然後再把這個認證檔案匯入到公匙庫:

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


import de.schlichtherle.license.LicenseManager;import de.schlichtherle.license.LicenseParam;/** * LicenseManager容器類 * @author 宋三思 */public class LicenseManagerHolder {private static LicenseManager licenseManager; public static synchronized LicenseManager getLicenseManager(LicenseParam licenseParams) {    if (licenseManager == null) {    licenseManager = new LicenseManager(licenseParams);    }    return licenseManager;    }}

import java.io.File;import java.util.Date;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;public class TestTrueLicense {public final static String PRIVATEALIAS = "privatekey";public final static String PUBLICALIAS = "publiccert";public final static String KEYPWD = "111aaa";public final static String STOREPWD = "111aaa";public final static String SUBJECT = "trueLicense測試";//為了方便直接用的API裡的例子//X500Princal是一個認證檔案的固有格式,詳見APIpublic final static X500Principal DEFAULTHOLDERANDISSUER = new X500Principal("CN=Duke、OU=JavaSoft、O=Sun Microsystems、C=US");    public static void main(String[] args) throws Exception {    /**************認證發行者端執行******************/    LicenseManager licenseManager =     LicenseManagerHolder.getLicenseManager(initLicenseParams0());    String storePath = TestTrueLicense.class.getResource(".").getFile() + File.separator + "測試.lic";    licenseManager.store((createLicenseContent()), new File(storePath));    System.out.println("伺服器端產生認證成功!");    /**************認證使用者端執行******************/    //由於用的是一個licenseManager所以重新設定驗證時需要的LicenseParam    licenseManager.setLicenseParam(initLicenseParams1());    //安裝認證    licenseManager.install(new File(storePath));    System.out.println("用戶端安裝認證成功!");    //驗證認證    licenseManager.verify();    System.out.println("用戶端驗證認證成功!");    }         //返回產生認證時需要的參數    private static LicenseParam initLicenseParams0() {    Preferences preference = Preferences.userNodeForPackage(TestTrueLicense.class);        //設定對認證內容加密的對稱密碼    CipherParam cipherParam = new DefaultCipherParam("111aaa");        //參數1,2從哪個Class.getResource()獲得密鑰庫    //參數3密鑰庫的別名    //參數4密鑰庫儲存密碼    //參數5密鑰庫密碼        KeyStoreParam privateStoreParam  =             new DefaultKeyStoreParam(TestTrueLicense.class, "privateKeys.store", PRIVATEALIAS, STOREPWD, KEYPWD);        LicenseParam licenseParams =        new DefaultLicenseParam(SUBJECT, preference, privateStoreParam, cipherParam);        return licenseParams;    }        //返回驗證認證需要的參數    private static LicenseParam initLicenseParams1() {    Preferences preference = Preferences.userNodeForPackage(TestTrueLicense.class);        CipherParam cipherParam = new DefaultCipherParam("111aaa");                KeyStoreParam privateStoreParam  =             new DefaultKeyStoreParam(TestTrueLicense.class, "publicCerts.store", PUBLICALIAS, STOREPWD, null);        LicenseParam licenseParams =        new DefaultLicenseParam(SUBJECT, preference, privateStoreParam, cipherParam);        return licenseParams;    }    //一般可以從外部表格單拿到認證的內容    //這裡為了示範,把認證內容寫死了    public final static LicenseContent createLicenseContent() {    LicenseContent content = null;    content = new LicenseContent();    content.setSubject(SUBJECT);    content.setHolder(DEFAULTHOLDERANDISSUER);    content.setIssuer(DEFAULTHOLDERANDISSUER);    content.setIssued(new Date());    content.setNotBefore(new Date());    content.setNotAfter(new Date(new Date().getTime() + 1000));    content.setConsumerType("user");    content.setConsumerAmount(1);    content.setInfo("隨便不為空白的描述性資訊");    //擴充    content.setExtra(new Object());    return content;    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.