Java tutorial
/* ================================================================== * Created [2016-06-22] by Jon.King * ================================================================== * TSS * ================================================================== * mailTo:boubei@163.com * Copyright (c) boubei.com, 2015-2018 * ================================================================== */ package com.boubei.tss.modules.license; import java.io.File; import java.security.KeyFactory; import java.security.Signature; import java.security.spec.X509EncodedKeySpec; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.boubei.tss.util.EasyUtils; import com.boubei.tss.util.FileHelper; /** * <pre> * 1??????license * 2?licenses?? * 3????licenses * * 4?licenses?JAVA Security API????? * ?/??licenses?? * * 5?license????? * ?java??? * </pre> */ public final class LicenseManager { private final Log log = LogFactory.getLog(getClass()); private static LicenseManager instance = new LicenseManager(); private LicenseManager() { } public static synchronized LicenseManager getInstance() { return instance; } private List<License> licenses; public void loadLicenses() { if (!EasyUtils.isNullOrEmpty(licenses)) return; licenses = new ArrayList<License>(); String files[] = new File(LicenseFactory.LICENSE_DIR).list(); for (int i = 0; i < files.length; i++) { String filename = files[i]; File file = new File(LicenseFactory.LICENSE_DIR, filename); if (file.isDirectory() || !filename.endsWith(".license")) { continue; } try { License license = License.fromConfigFile(filename); Date expiresDate = license.expiry; if (expiresDate != null && expiresDate.before(new Date())) { log.error("license \"" + file.getName() + "\" ?."); continue; } if (!validate(license)) { log.error("license \"" + file.getName() + "\" ??."); continue; } licenses.add(license); } catch (Exception e) { } } } /** * ????license?? * @param product * @param version * @return */ public boolean validateLicense(String product, String version) { License license = getLicense(product, version); return license != null; } /** * ?license * @param product * @param version * @return */ public String getLicenseType(String product, String version) { License license = getLicense(product, version); if (license == null) { return null; } return license.type; } public License getLicense(String product, String version) { loadLicenses(); double needsVersion = EasyUtils.obj2Double(version); for (License license : licenses) { double hasVersion = EasyUtils.obj2Double(license.version); if (license.product.equals(product) && hasVersion >= needsVersion) { return license; } } return null; } /** * <pre> * ?license???????? * </pre> * @param license * @return * @throws Exception */ boolean validate(License license) throws Exception { File keyFile = new File(LicenseFactory.PUBLIC_KEY_FILE); String publicKey = FileHelper.readFile(keyFile).trim(); X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(EasyUtils.decodeHex(publicKey)); KeyFactory keyFactory = KeyFactory.getInstance(LicenseFactory.KEY_ALGORITHM); java.security.PublicKey pubKey = keyFactory.generatePublic(pubKeySpec); Signature sig = Signature.getInstance(LicenseFactory.KEY_ALGORITHM); sig.initVerify(pubKey); sig.update(license.getFingerprint()); return sig.verify(EasyUtils.decodeHex(license.signature)); } }