liusuyi
2023-04-24 4737f1e038743ced243c9e52423404d9034d6107
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
package com.ruoyi.authorize.service;
 
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.authorize.common.AuthorizeUtil;
import com.ruoyi.authorize.common.DateTimeUtil;
import com.ruoyi.authorize.domain.AuthorizeInfo;
import com.ruoyi.authorize.domain.ParsLicenseInfo;
import com.ruoyi.common.constant.CacheConstants;
import com.ruoyi.common.core.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.utils.IOUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
 
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.util.concurrent.TimeUnit;
 
 
@Service
@Slf4j(topic = "Authorize")
public class AuthorizeServiceImpl implements AuthorizeService {
    @Resource
    private RedisCache redisCache;
 
    @PostConstruct
    public void init() {
        log.debug("尝试获取获取机器码");
        AuthorizeUtil.MachineCode = AuthorizeUtil.getMachineCode();
        log.debug("机器码获取成功:" + AuthorizeUtil.MachineCode);
        redisCache.setCacheObject(getCacheKey("MachineCode"), AuthorizeUtil.MachineCode);
    }
 
 
    /**
     * 系统授权时间判断
     *
     * @return boolean
     */
    public boolean isExpired(AuthorizeInfo Info) {
        try {
            if (Info != null) {
                String dateStr = Info.getSystemAuthorizeDate();
                dateStr = DateTimeUtil.strToDateFormat(dateStr);
                //授权到期时间
                long dates = DateTimeUtil.getLongDate(dateStr);
                long currentTime = DateTimeUtil.getCurrentLongDate();
                //授权是否到期
                if (dates >= currentTime) {
                    return true;
                }
            }
        } catch (ParseException e) {
            log.error("系统授权时间判断异常:" + e.getMessage());
        }
        return false;
    }
 
    /**
     * 解析授权文件
     *
     * @return AuthorizeInfo
     */
    public AuthorizeInfo parseFiles(File file) {
        BufferedReader reader = null;
        try {
            //用流读取文件
            reader = new BufferedReader(new FileReader(file));
            String line;
            StringBuffer content = new StringBuffer();
            // 读取想定文件
            while ((line = reader.readLine()) != null) {
                content.append(line);
            }
            String MachineCode = AuthorizeUtil.MachineCode;
            String str = AuthorizeUtil.parsLicense(MachineCode, content.toString());
            if (str != null) {
                AuthorizeInfo info = JSONObject.parseObject(str, AuthorizeInfo.class);
                return info;
            }
        } catch (IOException e) {
            log.error("解析授权文件异常:" + e.getMessage());
            return null;
        } finally {
            if (reader != null) {
                IOUtils.closeQuietly(reader);
            }
        }
        return null;
    }
 
    @Override
    public JSONObject verifyAuthorization() {
        try {
            JSONObject data = new JSONObject();
            String savedDir = System.getProperty("user.dir") + "\\ardLicense";
            File dir = new File(savedDir);
            if (!dir.exists()) { //如果不存在
                boolean dr = dir.mkdirs(); //创建目录
            }
            //从缓存中获取最后一次验证授权的时间
            Object auth_time = redisCache.getCacheObject(getCacheKey("AuthTime"));
            if (auth_time != null) {
                long lasttime = Long.parseLong(auth_time.toString());
                long currentTime = DateTimeUtil.getCurrentLongDate();
                if (currentTime <= lasttime) {
                    data.put("Type", "error");
                    data.put("Message", "系统时间异常");
                    return data;
                }
            }
            File LicenseFile = new File(savedDir + "/license.cpy");//获取授权文件
            if (LicenseFile.exists()) {
                AuthorizeInfo info = parseFiles(LicenseFile);//解析授权文件
                if (info != null) {
                    if (isExpired(info)) {
                        data.put("Type", "normal");
                        data.put("Message", info.getSystemAuthorizeDate());
 
                    } else {
                        data.put("Type", "expired");
                        data.put("Message", info.getSystemAuthorizeDate());
                    }
 
                } else {
                    data.put("Type", "mismatch");
                    data.put("Message", "License mismatch");
                }
            } else {
                data.put("Type", "mismatch");
                data.put("Message", "License mismatch");
            }
            //验证结束,写入最后一次验证的时间到缓存
            redisCache.setCacheObject(getCacheKey("AuthTime"), DateTimeUtil.getCurrentLongDate().toString());
            return data;
        } catch (Exception ex) {
            log.error("验证授权异常:" + ex.getMessage());
            return null;
        }
    }
 
    @Override
    public JSONObject makeTempLicense() {
        String savedDir = System.getProperty("user.dir") + "\\ardLicense";
        String tempLicenseDate = AuthorizeUtil.makeTempLicense(savedDir + "\\license.cpy");
        JSONObject data = new JSONObject();
        data.put("Type", "makeTempLicense");
        data.put("Message", tempLicenseDate);
        return data;
    }
 
    @Override
    public JSONObject getMachineCode() {
        try {
            String machineCode = AuthorizeUtil.getMachineCode();
            AuthorizeUtil.MachineCode = machineCode;
            log.debug("获取到机器码:" + machineCode);
            JSONObject data = new JSONObject();
            data.put("Type", "machineCode");
            data.put("Message", machineCode);
            return data;
        } catch (Exception ex) {
            log.error("获取机器码异常:" + ex.getMessage());
            return null;
        }
    }
 
    @Override
    public JSONObject uploadLicense(MultipartFile file) {
        try {
            String fileRealName = file.getOriginalFilename(); //获得原始文件名;
            String savedDir = System.getProperty("user.dir") + "\\ardLicense";
            File dir = new File(savedDir);
            if (!dir.exists()) { //如果不存在
                boolean dr = dir.mkdirs(); //创建目录
            }
            File LicenseFile = new File(savedDir, fileRealName);
            try {
                file.transferTo(LicenseFile);  //转存文件
            } catch (IOException e) {
                e.printStackTrace();
            }
            JSONObject data = new JSONObject();
            data.put("Type", "uploadLicenseFile");
            data.put("Message", "Dir:" + savedDir);
            return data;
        } catch (Exception ex) {
            log.error("获取机器码异常:" + ex.getMessage());
            return null;
        }
    }
 
    @Override
    public String getLicense(String data) {
        String codeString = "";
        try {
            if (data != "") {
                //解析json
                AuthorizeInfo rt = JSON.parseObject(data, AuthorizeInfo.class);
                String machineCode = rt.getMachineCode();
                String key = data;
                if (machineCode != "" && key != "") {
                    codeString = AuthorizeUtil.getLicense(machineCode, key);
                }
            }
            return codeString;
        } catch (Exception ex) {
            log.error("加密异常:" + ex.getMessage());
            return codeString;
        }
    }
 
    @Override
    public String parsLicense(String data) {
        String codeString = "";
        try {
            if (data != "") {
                //解析json
                ParsLicenseInfo rt = JSON.parseObject(data, ParsLicenseInfo.class);
                String machineCode = rt.getMachineCode();
                String key = rt.getEncryptedStr();
                codeString = AuthorizeUtil.parsLicense(machineCode, key);
            }
        } catch (Exception ex) {
            log.error("解密异常:" + ex.getMessage());
            return codeString;
        }
        return codeString;
 
    }
 
    /**
     * 设置cache key
     *
     * @param configKey 参数键
     * @return 缓存键key
     */
    private String getCacheKey(String configKey) {
        return CacheConstants.AUTH_CONFIG_KEY + configKey;
    }
}