lyh
3 天以前 b508ec38ddf9ed93d4435e8f1e3c4effef798aaa
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
package com.lxzn.auth.security;
 
import com.alibaba.fastjson.JSON;
import com.lxzn.auth.LicenseModel;
import com.lxzn.framework.model.response.CommonCode;
import com.lxzn.framework.model.response.ResponseResult;
import com.lxzn.framework.utils.LicenseUtil;
import com.lxzn.framework.utils.ValidateUtil;
import com.lxzn.framework.utils.date.DateUtil;
import com.lxzn.websocket.LicenseWebSocket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
 
import java.util.Date;
 
/**
 * @Description:
 * @Author: zhangherong
 * @Date: Created in 2021/6/22 17:37
 * @Version: 1.0
 * @Modified By:
 */
@Service
public class LicenseService {
 
    @Value("${license.tip_days}")
    private Integer expiredTip;
    @Value("${license.path}")
    private String licensePath;
 
    @Autowired
    private LicenseWebSocket webSocket;
 
    @Scheduled(cron = "0/10 * * * * ?")
    public void checkLicense(){
        String licenseTmp = "EFcAB7+J2pScncHlrSqf/8GfGGIV2nn/o1+cN9rB0i2M/KyVNZ5FG8G9TYh3Y7A1";
        if(!ValidateUtil.validateString(licenseTmp)) {
            String s = JSON.toJSONString(new ResponseResult(CommonCode.LICENSE_ERROR));
            webSocket.sendMessage(s);
            return;
        }
        if(!licenseTmp.equals(LicenseModel.getLicense())) {
            LicenseModel.init(licenseTmp);
        }
        if(LicenseModel.CPU_SERIAL_NUMBER == null || LicenseModel.getCpuSerialNumber() == null
                || LicenseModel.getDncDeviceNumber() == null || LicenseModel.getMdcDeviceNumber() == null
                || LicenseModel.getExpiredDate() == null
                || !LicenseModel.CPU_SERIAL_NUMBER.contains(LicenseModel.getCpuSerialNumber())){
            String s = JSON.toJSONString(new ResponseResult(CommonCode.LICENSE_ERROR));
            webSocket.sendMessage(s);
            return;
        }
 
        Date now = DateUtil.getNow();
        Date expire = DateUtil.toDate(LicenseModel.getExpiredDate(), DateUtil.STR_DATE);
        if(now.getTime() >= expire.getTime()){
            String s = JSON.toJSONString(new ResponseResult(CommonCode.LICENSE_EXPIRED));
            webSocket.sendMessage(s);
            return;
        }
        Date willExpire = DateUtil.addDay(expire, expiredTip * -1);
        if(now.getTime() >= willExpire.getTime()) {
            String s = JSON.toJSONString(new ResponseResult(CommonCode.LICENSE_WILL_EXPIRED));
            webSocket.sendMessage(s);
            return;
        }
    }
}