src/main/java/com/zy/common/web/AuthController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/system/controller/LicenseCreatorController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/system/entity/license/CustomKeyStoreParam.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/system/entity/license/LicenseCheckListener.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/system/entity/license/LicenseVerify.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/zy/system/timer/LicenseTimer.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
src/main/java/com/zy/common/web/AuthController.java
@@ -16,6 +16,7 @@ import com.zy.system.entity.*; import com.zy.system.entity.license.LicenseVerify; import com.zy.system.service.*; import com.zy.system.timer.LicenseTimer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.transaction.annotation.Transactional; @@ -46,14 +47,14 @@ private PermissionService permissionService; @Autowired private RolePermissionService rolePermissionService; @Autowired private LicenseTimer licenseTimer; @RequestMapping("/login.action") @ManagerAuth(value = ManagerAuth.Auth.NONE, memo = "登录") public R loginAction(String username, String password){ //验证许可证是否有效 LicenseVerify licenseVerify = new LicenseVerify(); boolean verify = licenseVerify.verify(); if (!verify) {//许可证已失效 if (!licenseTimer.getSystemSupport()){ return R.parse(CodeRes.SYSTEM_20001); } if (username.equals("super") && password.equals(Cools.md5(superPwd))) { src/main/java/com/zy/system/controller/LicenseCreatorController.java
@@ -3,6 +3,7 @@ import com.core.common.Cools; import com.core.common.R; import com.zy.system.entity.license.*; import com.zy.system.timer.LicenseTimer; import de.schlichtherle.license.LicenseContent; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -29,6 +30,9 @@ @Autowired private LicenseCheckListener licenseCheckListener; @Autowired private LicenseTimer licenseTimer; /** * 获取服务器硬件信息 @@ -61,19 +65,7 @@ */ @RequestMapping(value = "/getLicenseDays") public R getLicenseDays() { LicenseVerify licenseVerify = new LicenseVerify(); LicenseContent verifyInfo = licenseVerify.getVerifyInfo(); if (verifyInfo == null) { return R.error(); } Date start = new Date(); Date end = verifyInfo.getNotAfter(); Long starTime = start.getTime(); Long endTime = end.getTime(); Long num = endTime - starTime;//时间戳相差的毫秒数 int day = (int) (num / 24 / 60 / 60 / 1000); return R.ok().add(day); return R.ok(licenseTimer.getLicenseDays()); } @RequestMapping(value = "/updateLicense") src/main/java/com/zy/system/entity/license/CustomKeyStoreParam.java
@@ -47,7 +47,7 @@ */ @Override public InputStream getStream() throws IOException { final InputStream in = new FileInputStream(new File(storePath)); final InputStream in = this.getClass().getClassLoader().getResourceAsStream(storePath); if (null == in) { throw new FileNotFoundException(storePath); } src/main/java/com/zy/system/entity/license/LicenseCheckListener.java
@@ -1,9 +1,11 @@ package com.zy.system.entity.license; import com.core.common.Cools; import com.zy.system.timer.LicenseTimer; import de.schlichtherle.license.LicenseContent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; @@ -11,6 +13,7 @@ import org.springframework.stereotype.Component; import java.io.File; import java.util.Date; /** * 在项目启动时安装证书 @@ -49,6 +52,9 @@ @Value("${license.publicKeysStorePath}") private String publicKeysStorePath; @Autowired private LicenseTimer licenseTimer; @Override public void onApplicationEvent(ContextRefreshedEvent event) { //root application context 没有parent @@ -64,18 +70,18 @@ logger.info("++++++++ 开始加载许可证 ++++++++"); try { String publicKeysStoreFileName = this.getClass().getClassLoader().getResource(publicKeysStorePath).getPath(); File publicKeysStoreFile = new File(publicKeysStoreFileName); String licensePathFileName = this.getClass().getClassLoader().getResource(licensePath).getPath(); File licensePathFile = new File(licensePathFileName); // String publicKeysStoreFileName = this.getClass().getClassLoader().getResource(publicKeysStorePath).getPath(); // File publicKeysStoreFile = new File(publicKeysStoreFileName); // // String licensePathFileName = this.getClass().getClassLoader().getResource(licensePath).getPath(); // File licensePathFile = new File(licensePathFileName); LicenseVerifyParam param = new LicenseVerifyParam(); param.setSubject(subject); param.setPublicAlias(publicAlias); param.setStorePass(storePass); param.setLicensePath(licensePathFile.getPath()); param.setPublicKeysStorePath(publicKeysStoreFile.getPath()); param.setLicensePath(licensePath); param.setPublicKeysStorePath(publicKeysStorePath); LicenseVerify licenseVerify = new LicenseVerify(); //安装证书 @@ -83,12 +89,25 @@ logger.info("++++++++ 许可证加载结束 ++++++++"); licenseTimer.setSystemSupport(install!=null); if (install != null) { Date start = new Date(); Date end = install.getNotAfter(); Long starTime = start.getTime(); Long endTime = end.getTime(); Long num = endTime - starTime;//时间戳相差的毫秒数 int day = (int) (num / 24 / 60 / 60 / 1000); licenseTimer.setLicenseDays(day); } return install != null; } catch (Exception e) { return false; } } licenseTimer.setSystemSupport(false); return false; } } src/main/java/com/zy/system/entity/license/LicenseVerify.java
@@ -3,8 +3,11 @@ import de.schlichtherle.license.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.apache.tomcat.util.http.fileupload.IOUtils; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.text.DateFormat; import java.text.MessageFormat; import java.text.SimpleDateFormat; @@ -28,6 +31,12 @@ LicenseManager licenseManager = LicenseManagerHolder.getInstance(initLicenseParam(param)); licenseManager.uninstall(); InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(param.getLicensePath()); File file = new File(param.getLicensePath()); try (FileOutputStream out = new FileOutputStream(file)) { IOUtils.copy(inputStream, out); } result = licenseManager.install(new File(param.getLicensePath())); logger.info(MessageFormat.format("许可证加载成功,许可证有效期:{0} - {1}",format.format(result.getNotBefore()),format.format(result.getNotAfter()))); }catch (Exception e){ src/main/java/com/zy/system/timer/LicenseTimer.java
New file @@ -0,0 +1,40 @@ package com.zy.system.timer; import com.zy.system.entity.license.LicenseVerify; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class LicenseTimer { private static boolean SYSTEM_SUPPORT = false;//系统激活状态,默认关闭 private static int LICENSE_DAYS = 0;//许可证天数 //每天晚上11点更新系统激活状态 @Scheduled(cron = "0 0 23 * * ? ") public void timer() { // System.out.println(SYSTEM_SUPPORT); //验证许可证是否有效 LicenseVerify licenseVerify = new LicenseVerify(); boolean verify = licenseVerify.verify(); setSystemSupport(verify);//更新系统激活状态 } public boolean getSystemSupport() { return SYSTEM_SUPPORT; } public void setSystemSupport(boolean systemSupport) { SYSTEM_SUPPORT = systemSupport; } public int getLicenseDays() { return LICENSE_DAYS; } public void setLicenseDays(int licenseDays) { LICENSE_DAYS = licenseDays; } }