1
13693261870
2025-01-14 042eb4875c91a38b19f160bf68fb658552f7e7bc
1
已添加2个文件
已修改1个文件
198 ■■■■■ 文件已修改
se-modules/se-system/src/main/java/com/se/system/domain/vo/LicenseVerifyParamVo.java 11 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
se-modules/se-system/src/main/java/com/se/system/service/CustomVerifyListenerService.java 48 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
se-modules/se-system/src/main/java/com/se/system/service/LiceVerifyInstallListener.java 139 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
se-modules/se-system/src/main/java/com/se/system/domain/vo/LicenseVerifyParamVo.java
@@ -88,4 +88,15 @@
                ", publicKeysStorePath='" + publicKeysStorePath + '\'' +
                '}';
    }
    public LicenseVerifyParamVo getVerifyParam() {
        LicenseVerifyParamVo param = new LicenseVerifyParamVo();
        param.setSubject(subject);
        param.setPublicAlias(publicAlias);
        param.setStorePass(storePass);
        param.setLicensePath(licensePath);
        param.setPublicKeysStorePath(publicKeysStorePath);
        return param;
    }
}
se-modules/se-system/src/main/java/com/se/system/service/CustomVerifyListenerService.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,48 @@
package com.se.system.service;
import com.se.system.domain.vo.LicenseExtraParamVo;
import com.se.system.service.inte.IACustomVerifyListener;
@SuppressWarnings("ALL")
public class CustomVerifyListenerService extends IACustomVerifyListener {
    /**
     * å•例Bean,这个静态变量会缓存证书中最大用户注册数的
     */
    private static Long registerAmount = 0L;
    @Override
    public boolean verify(LicenseExtraParamVo licenseExtra) {
        registerAmount = null == licenseExtra.getRegisterAmount() ? 0 : licenseExtra.getRegisterAmount();
        System.out.println("======= è‡ªå®šä¹‰è¯ä¹¦éªŒè¯ç›‘听器A å®žçްverify方法  =======");
        System.out.println("证书最大验证人数是:" + registerAmount);
        /**
         * è¿™é‡Œï¼Œå¯ä»¥åšä¸€äº›å’Œä¸šåŠ¡ç³»ç»Ÿæœ‰å…³çš„å‚æ•°éªŒè¯
         * åœ¨è¿™ä¸ªä¸šåŠ¡ç³»ç»Ÿè¿™ä¸ªç›‘å¬æ–¹æ³•ä¸­ï¼Œæˆ‘ä»¬å¯ä»¥å…ˆåŽ»user表查询下,当前系统的注册用户数量
         * ç„¶åŽæ‹¿ç€è¿™ä¸ªæ•°é‡å’Œlic参数中约束的最大注册用户量进行比较,如果>,则抛出异常
         * è¿™æ ·åšçš„目的是防止业务系统部署到客户端本地服务器上时,数据库是公开的,防止对方通过手动改表来添加用户
         */
        long count = 1001;
        if (count >= registerAmount) {
            System.out.println("系统当前用户数超过最大用户注册限制数【" + registerAmount + "】");
        }
        System.out.println("系统注册最大用户数量未超过" + count + ",验证用过!");
        return true;
    }
    /**
     * è¿™ä¸ªå•独的重载的验证方法,主要是用于业务系统中进行调用,带参数的verify方法用于注解
     * è€Œè¿™ä¸ªä¸å¸¦å‚æ•°çš„verify,更加灵活些,比如在系统中注册接口(Controller)上,除了自身@VLicense注解外,
     * å¯ä»¥åœ¨è°ƒç”¨è¿™ä¸ªæ–¹æ³•,来额外的验证注册人数是否已满
     *
     * @return
     * @throws
     */
    public boolean verify() {
        Long count = 100L;
        if (count.equals(registerAmount)) {
            System.out.println("系统当前用户数已达到最大用户注册限制数【" + registerAmount + "】,无法再注册新用户。如需扩充用户注册的数量,请联系我们重新购买License!");
        }
        return true;
    }
}
se-modules/se-system/src/main/java/com/se/system/service/LiceVerifyInstallListener.java
¶Ô±ÈÐÂÎļþ
@@ -0,0 +1,139 @@
package com.se.system.service;
import com.se.system.domain.vo.LicenseResultVo;
import com.se.system.domain.vo.LicenseVerifyMg;
import com.se.system.domain.vo.LicenseVerifyParamVo;
import com.se.system.utils.CommonUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
@SuppressWarnings("ALL")
public class LiceVerifyInstallListener {
    public LicenseVerifyParamVo licenseVerifyProperties;
    private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    private ScheduledFuture<?> scheduledFuture;
    // å¯åŠ¨å®šæ—¶ä»»åŠ¡
    public void startTimer() {
        scheduledFuture = scheduler.scheduleAtFixedRate(this::timer, 0, 15, TimeUnit.SECONDS);
    }
    // åœæ­¢å®šæ—¶ä»»åŠ¡
    public void stopTimer() {
        if (scheduledFuture != null) {
            scheduledFuture.cancel(false);
        }
    }
    /**
     * æ–‡ä»¶å”¯ä¸€èº«ä»½æ ‡è¯† == ç›¸å½“于人类的指纹一样
     */
    private static String md5 = "";
    private static boolean isLoad = false;
    public static void LicenseVerifyInstall(LicenseVerifyParamVo licenseVerifyProperties) {
        new LiceVerifyInstallListener(licenseVerifyProperties);
    }
    public LiceVerifyInstallListener(LicenseVerifyParamVo licenseVerifyProperties) {
        // startTimer();
        this.licenseVerifyProperties = licenseVerifyProperties;
        if (CommonUtils.isNotEmpty(licenseVerifyProperties.getLicensePath())) {
            install();
            try {
                String readMd5 = getMd5(licenseVerifyProperties.getLicensePath());
                isLoad = true;
                if (LiceVerifyInstallListener.md5 == null || "".equals(LiceVerifyInstallListener.md5)) {
                    LiceVerifyInstallListener.md5 = readMd5;
                }
            } catch (Exception e) {
            }
        }
    }
    /**
     * 15秒检测一次,不能太快也不能太慢
     */
    protected void timer() {
        if (!isLoad) {
            return;
        }
        String readMd5 = null;
        try {
            readMd5 = getMd5(licenseVerifyProperties.getLicensePath());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        // ä¸ç›¸ç­‰ï¼Œè¯´æ˜Žlic变化了
        if (!readMd5.equals(LiceVerifyInstallListener.md5)) {
            install();
            LiceVerifyInstallListener.md5 = readMd5;
        }
    }
    private void install() {
        System.out.println("++++++++ å¼€å§‹å®‰è£…证书 ++++++++");
        LicenseVerifyMg licenseVerifyManager = new LicenseVerifyMg();
        /** èµ°å®šä¹‰æ ¡éªŒè¯ä¹¦å¹¶å®‰è£… */
        LicenseResultVo result = licenseVerifyManager.install(licenseVerifyProperties.getVerifyParam());
        if (result.getResult()) {
            System.out.println("++++++++ è¯ä¹¦å®‰è£…成功 ++++++++");
        } else {
            System.out.println("++++++++ è¯ä¹¦å®‰è£…失败 ++++++++");
        }
    }
    /**
     * <p>获取文件的md5</p>
     */
    public String getMd5(String filePath) throws Exception {
        File file;
        String md5 = "";
        try {
            file = new File(filePath);
            if (file.exists()) {
                FileInputStream is = new FileInputStream(file);
                byte[] data = new byte[is.available()];
                is.read(data);
                md5 = calculateMD5(data);
                is.close();
            }
        } catch (FileNotFoundException e) {
        }
        return md5;
    }
    public static String calculateMD5(byte[] data) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            byte[] hashBytes = md.digest(data);
            StringBuilder hexString = new StringBuilder();
            for (byte b : hashBytes) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            return hexString.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace(); // å¤„理异常,例如打印错误信息
            return null;
        }
    }
}