True-License的演示用例

keytool生成密钥库的方法就不说了。网上很多,也可详见 <java核心技术第二卷>

简单的trueLicense用例,仅供参考.

true-license的源码,已经添加了中文注释,喜欢的朋友可以拿去看看。

truelicense源码



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是一个证书文件的固有格式,详见API
	public 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;
    }
}


你可能感兴趣的:(exception,Date,String,null,Class,import)