自动化立体仓库 - WMS系统
野心家
2024-12-12 62cf138b7dd15ac062091a32ac2cf523b0928ea6
更新认证模式
1个文件已添加
5个文件已修改
109 ■■■■ 已修改文件
src/main/java/com/zy/common/web/AuthController.java 8 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/system/controller/LicenseCreatorController.java 17 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/system/entity/license/CustomKeyStoreParam.java 3 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/system/entity/license/LicenseCheckListener.java 32 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/system/entity/license/LicenseVerify.java 9 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/zy/system/timer/LicenseTimer.java 40 ●●●●● 补丁 | 查看 | 原始文档 | 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,13 @@
    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,8 @@
    @Autowired
    private LicenseCheckListener licenseCheckListener;
    @Autowired
    private LicenseTimer licenseTimer;
    /**
     * 获取服务器硬件信息
@@ -61,19 +64,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().add(licenseTimer.getLicenseDays());
    }
    @RequestMapping(value = "/updateLicense")
src/main/java/com/zy/system/entity/license/CustomKeyStoreParam.java
@@ -47,7 +47,8 @@
     */
    @Override
    public InputStream getStream() throws IOException {
        final InputStream in = new FileInputStream(new File(storePath));
        final InputStream in = this.getClass().getClassLoader().getResourceAsStream(storePath);
//        final InputStream in = new FileInputStream(new File(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,22 @@
                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.poi.util.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;
    }
}