1
13693261870
2025-01-14 042eb4875c91a38b19f160bf68fb658552f7e7bc
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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;
        }
    }
}