whyclxw
2022-01-10 00b61a61b2a90c85aeae872e351ddf4bb2b516cf
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.whyc.dto.Response;
import com.whyc.encryption.ByteConvertUtil;
import com.whyc.encryption.SM2;
import com.whyc.mapper.LicenseMapper;
import com.whyc.mapper.UserInfMapper;
import com.whyc.pojo.License;
import com.whyc.pojo.UserInf;
import com.whyc.util.AESUtil;
import com.whyc.util.SerialNumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ClassUtils;
 
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;
 
@Service
public class LicenseService {
 
    @Autowired
    private UserInfMapper userInfMapper;
 
    @Autowired
    private LicenseMapper licenseMapper;
 
    public boolean add(License license){
        return licenseMapper.insert(license)>0;
    }
 
    /**
     * 证书文件校验,检查有效性并将文件存储在数据库
     */
    public Response checkLicense(String license){
        Response model = new Response();
        boolean res = true;
        String origin = "Company: Fuguang Electronic\n"
                + "Project:BTS monitor platform\n"
                + "Licence type:";
 
        //初始化sm2参数x
        SM2 x = new SM2();
//        String realPath = ServletActionContext.getServletContext().getRealPath("/");
//        BigInteger privKey = x.importPrivateKey(realPath+"WEB-INF/classes/pri_key.ksm");
        String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();
        BigInteger privKey = x.importPrivateKey(path+"config/pri_key.ksm");
 
 
        //解密
        try {
            byte[] bytes = ByteConvertUtil.hexToByteArray(license);
            String decryptResult = x.decrypt(bytes, privKey);
 
            if(decryptResult.indexOf(origin)!=-1){
                String[] split = decryptResult.split("Valid time:");
                if(split.length>1){
                    String validTime=split[1];
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String today = dateFormat.format(new Date());
                    if(validTime.compareTo(today)<0){
                        res=false;
                    }
                }
            }else{
                res=false;
            }
 
        }catch (Exception e){
            res=false;
            e.printStackTrace();
        }
        //license校验通过,校验数据库中是否已经存在
        if(res){
//            res =!(new User_infImpl().licenseExist(license));
        }
        if(res){
            model.setCode(1);
        }else{
            model.setMsg("无效的license");
        }
        return model;
    }
    /**
     * 每次用户登录,校验License中的有效期,无效则无法登录
     * 这个license针对用户
     */
    public Response checkLicenseValidity(String uName){
        Response model = new Response();
        model.setCode(1);
        QueryWrapper<UserInf> userInfQueryWrapper = new QueryWrapper<>();
        userInfQueryWrapper.select("license").eq("uName",uName);
        String license = userInfMapper.selectOne(userInfQueryWrapper).getLicense();
        //String license = new User_infImpl().getLicense(uName);
        if(license==null){
            model.setCode(0);
            model.setMsg("当前用户无license,请添加license后登录");
        }else {
            //初始化sm2参数x
            SM2 x = new SM2();
//            String realPath = ServletActionContext.getServletContext().getRealPath("/");
//            BigInteger privKey = x.importPrivateKey(realPath+"WEB-INF/classes/pri_key.ksm");
            String path = ClassUtils.getDefaultClassLoader().getResource("").getPath();;
            BigInteger privKey = x.importPrivateKey(path+"config/pri_key.ksm");
 
            //解密
            try {
                byte[] bytes = ByteConvertUtil.hexToByteArray(license);
                String decryptResult = x.decrypt(bytes, privKey);
 
                String[] split = decryptResult.split("Valid time:");
                if(split.length>1){
                    String validTime=split[1];
                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    String today = dateFormat.format(new Date());
                    if(validTime.compareTo(today)<0){
                        model.setCode(0);
                        model.setMsg("license过期,有效期为:"+validTime);
                    }
 
                }
 
            }catch (Exception e){
                model.setCode(0);
                model.setMsg("无效的license");
                e.printStackTrace();
            }
        }
        return model;
 
    }
 
    /**
     * 获取用户的license
     * @param uName
     * @return
     */
    public Response getLicense(String uName){
        Response model = new Response();
        //String license = new User_infImpl().getLicense(uName);
        QueryWrapper<UserInf> userInfQueryWrapper = new QueryWrapper<>();
        userInfQueryWrapper.select("license").eq("uName",uName);
        String license = userInfMapper.selectOne(userInfQueryWrapper).getLicense();
        model.setCode(1);
        model.setData(true);
        model.setData(license);
        return model;
    }
 
 
    public Response checkLicenseExpired() {
        Response model = new Response();
        boolean res=licenseMapper.selectExist()>0;
        if(!res) { //表不存在,新建表,同时初始化参数
            boolean created = licenseMapper.createTable()>0;
        }
        QueryWrapper<License> queryWrapper = new QueryWrapper<>();
        License license = licenseMapper.selectOne(queryWrapper);
        //License license = new LicenseDao().searchOne();
        if(license!=null){
            //校验凭证序列号,项目绑定序列号
            String serialNumberInDB = license.getSerialNumber();
            String serialNumberLocal= SerialNumberUtil.getSerialNumber();
            if(!serialNumberInDB.equals(serialNumberLocal)){
                model.setCode(0);
                model.setMsg("凭证校验失败,请检查项目是否已经迁移");
            }else{
                //校验时间
                //有效时长,单位:天
                //duration和timeInUse解密后再做对比
                String durationEncrypt = license.getDuration();
                double duration = Double.parseDouble(AESUtil.aesDecrypt(durationEncrypt));
                //如果不是永久
                if(duration!=-1){
                    //使用时长,单位:秒
                    String timeInUseEncrypt = license.getTimeInUse();
                    Long timeInUse = Long.valueOf(AESUtil.aesDecrypt(timeInUseEncrypt));
                    if (duration * 24 * 60 * 60 < timeInUse) {
                        model.setCode(1);
                        model.setData(false);
                        model.setMsg("凭证已过期,有效期为:" + duration + "天");
                    }else{
                        model.setCode(1);
                        model.setData(true);
                    }
                }else {
                    model.setCode(1);
                    model.setData(true);
                }
            }
 
        }else{
            model.setCode(1);
            model.setData(false);
            model.setMsg("注册码尚未输入");
        }
        return model;
    }
 
    public Response time2DeadLine() {
        Response model = new Response();
        QueryWrapper<License> queryWrapper = new QueryWrapper<>();
        License license = licenseMapper.selectOne(queryWrapper);
        String durationEncrypt = license.getDuration();
        double duration = Double.parseDouble(AESUtil.aesDecrypt(durationEncrypt));
        //-1表明license是永久的
        if(duration==-1){
            model.setCode(2);
        }else {
            String timeInUseEncrypt = license.getTimeInUse();
            Long timeInUse = Long.valueOf(AESUtil.aesDecrypt(timeInUseEncrypt));
            int durationSecond = (int) (duration * 24 * 60 * 60);
            Long leftTimeSecond = durationSecond - timeInUse;
            if(leftTimeSecond<=0){
                model.setCode(1);
                model.setData(0);
            }else {
                double leftTimeDay = Math.ceil(1.0 * leftTimeSecond / 60 / 60 / 24);
                model.setCode(1);
                model.setData(leftTimeDay);
            }
        }
        return model;
    }
 
 
}