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; } }
最近下载更多
微信网友_6174112425660416 LV1
2022年10月15日
yufadashen LV3
2020年11月16日
huweinan03 LV1
2020年10月20日
ycr202008 LV1
2020年5月24日
123456781qaz LV2
2019年12月24日
Lee240603372 LV1
2019年10月25日
rongxinhui LV8
2019年8月31日
zzyyyyyy LV1
2019年7月8日
alang94 LV5
2019年5月22日
ftlgood LV1
2019年1月18日
最近浏览更多
kenhomeliu LV29
4月30日
80730176 LV7
2023年3月9日
青梅论酒 LV9
2023年2月14日
微信网友_6174112425660416 LV1
2022年10月13日
wwwbl123 LV2
2022年3月18日
陈箫goddess LV16
2021年12月28日
maixiaomai LV6
2021年3月26日
bfh365
2021年3月18日
暂无贡献等级
张洪燕
2021年3月17日
暂无贡献等级
xhmpmail LV17
2021年2月24日