1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
| package com.license;
import de.schlichtherle.license.*;
import javax.security.auth.x500.X500Principal; 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;
public class CreateLicense { public static void main(String[] args) { CreateLicense cLicense = new CreateLicense(); cLicense.setParam("/param.properties"); boolean b = cLicense.create(); }
private static String PRIVATE_ALIAS = ""; private static String KEY_PWD = ""; private static String STORE_PWD = ""; private static String SUB_JECT = ""; private static String LIC_PATH = ""; private static String PRI_PATH = ""; private static String ISSUED_TIME = ""; private static String NOT_BEFORE = ""; private static String NOT_AFTER = ""; private static String CONSUMER_TYPE = ""; private static int CONSUMER_AMOUNT = 0; private static String INFO = ""; private final static X500Principal DEFAULT_HOLDER_AND_ISSUER = new X500Principal("CN=maxzhao, OU=maxzhao, O=maxzhao_org, L=nanjing, ST=jiangsu, C=China");
public void setParam(String propertiesPath) { Properties prop = new Properties(); InputStream in = getClass().getResourceAsStream(propertiesPath); try { prop.load(in); } catch (IOException e) { e.printStackTrace(); } PRIVATE_ALIAS = prop.getProperty("privateAlias"); KEY_PWD = prop.getProperty("keyPwd"); STORE_PWD = prop.getProperty("storePwd"); SUB_JECT = prop.getProperty("subJect"); KEY_PWD = prop.getProperty("keyPwd"); LIC_PATH = prop.getProperty("licPath"); PRI_PATH = prop.getProperty("priPath"); ISSUED_TIME = prop.getProperty("issuedTime"); NOT_BEFORE = prop.getProperty("notBefore"); NOT_AFTER = prop.getProperty("notAfter"); CONSUMER_TYPE = prop.getProperty("consumerType"); CONSUMER_AMOUNT = Integer.valueOf(prop.getProperty("consumerAmount")); INFO = prop.getProperty("info"); }
public boolean create() { try { LicenseManager licenseManager = new LicenseManager(initLicenseParams()); licenseManager.store((createLicenseContent()), new File(LIC_PATH)); } catch (Exception e) { e.printStackTrace(); System.out.println("客户端证书生成失败!"); return false; } System.out.println("服务器端生成证书成功!"); return true; }
private static LicenseParam initLicenseParams() { Preferences preference = Preferences.userNodeForPackage(LicenseCreator.class); CipherParam cipherParam = new DefaultCipherParam(STORE_PWD); KeyStoreParam privateStoreParam = new DefaultKeyStoreParam(LicenseCreator.class, PRI_PATH, PRIVATE_ALIAS, STORE_PWD, KEY_PWD); return new DefaultLicenseParam(SUB_JECT, preference, privateStoreParam, cipherParam); }
public final static LicenseContent createLicenseContent() { DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); LicenseContent content = null; content = new LicenseContent(); content.setSubject(SUB_JECT); content.setHolder(DEFAULT_HOLDER_AND_ISSUER); content.setIssuer(DEFAULT_HOLDER_AND_ISSUER); try { content.setIssued(format.parse(ISSUED_TIME)); content.setNotBefore(format.parse(NOT_BEFORE)); content.setNotAfter(format.parse(NOT_AFTER)); } catch (ParseException e) { e.printStackTrace(); } content.setConsumerType(CONSUMER_TYPE); content.setConsumerAmount(CONSUMER_AMOUNT); content.setInfo(INFO); content.setExtra(new Object()); return content; } }
|