#
Junjie
2 天以前 686fe55892de7bf8d206cddbead77a5fbdb0e091
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
package com.zy.system.controller;
 
import com.core.common.R;
import com.zy.system.entity.LicenseInfos;
import com.zy.system.entity.license.*;
import com.zy.system.service.LicenseInfosService;
import com.zy.system.timer.LicenseTimer;
import de.schlichtherle.license.LicenseContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
/**
 *
 * 用于生成证书文件,不能放在给客户部署的代码里
 */
@RestController
@RequestMapping("/license")
public class LicenseCreatorController {
 
    @Value("${license.subject}")
    private String licenseSubject;
    @Value("${license.publicAlias}")
    private String publicAlias;
    @Value("${license.storePass}")
    private String storePass;
    @Value("${license.licensePath}")
    private String licensePath;
    @Value("${license.publicKeysStorePath}")
    private String publicKeysStorePath;
    @Autowired
    private LicenseCheckListener licenseCheckListener;
    @Autowired
    private LicenseTimer licenseTimer;
    @Autowired
    private LicenseInfosService licenseInfosService;
    /**
     * 获取服务器硬件信息
     * @param osName 操作系统类型,如果为空则自动判断
     */
    @RequestMapping(value = "/getServerInfos",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public LicenseCheck getServerInfos(@RequestParam(value = "osName",required = false) String osName) {
        return LicenseUtils.getServerInfos();
    }
 
    /**
     * 获取请求码。
     */
    @RequestMapping(value = "/getRequestCode")
    public R getRequestCode() {
        return R.ok(LicenseUtils.buildRequestCode(licenseSubject));
    }
 
    /**
     * 获取许可证有效期天数
     */
    @RequestMapping(value = "/getLicenseDays")
    public R getLicenseDays() {
        int licenseDays = licenseTimer.getLicenseDays();
        if (!licenseTimer.getSystemSupport()) {
            licenseDays = -1;
        }
        return R.ok().add(licenseDays);
    }
 
    @RequestMapping(value = "/updateLicense", produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public R updateLicense(@RequestBody LicenseUploadParam param){
        if (param == null || param.getLicense() == null || param.getLicense().trim().isEmpty()) {
            return R.error("许可证内容不能为空");
        }
 
        String licenseBase64 = param.getLicense().trim();
        LicenseVerifyParam verifyParam = buildVerifyParam();
        LicenseVerify licenseVerify = new LicenseVerify();
        LicenseContent install = licenseVerify.install(verifyParam, licenseBase64);
        if (install == null) {
            return R.error("许可证内容无效");
        }
 
        LicenseInfos licenseInfos = new LicenseInfos();
        licenseInfos.setLicense(licenseBase64);
        licenseInfos.setLicenseTime(formatLicenseTime(install));
        licenseInfos.setRequestCode(LicenseUtils.buildRequestCode(licenseSubject));
        licenseInfos.setCreateTime(new Date());
        if (!licenseInfosService.insert(licenseInfos)) {
            return R.error("许可证保存失败");
        }
 
        boolean loadedLicense = licenseCheckListener.loadLicense(false);
        if (!loadedLicense) {
            return R.error("许可证激活失败");
        }
        licenseTimer.verify();
        if (!licenseTimer.getSystemSupport()) {
            return R.error("许可证校验失败");
        }
        return R.ok();
    }
 
    @RequestMapping(value = "/activate")
    public R activate() {
        licenseTimer.timer();
        if (!licenseTimer.getSystemSupport()) {
            return R.error("许可证激活失败");
        }
        return R.ok();
    }
 
    @RequestMapping(value = "/getProjectName")
    public R getProjectName() {
        return R.ok(licenseSubject);
    }
 
    private LicenseVerifyParam buildVerifyParam() {
        LicenseVerifyParam param = new LicenseVerifyParam();
        param.setSubject(licenseSubject);
        param.setPublicAlias(publicAlias);
        param.setStorePass(storePass);
        param.setLicensePath(licensePath);
        param.setPublicKeysStorePath(publicKeysStorePath);
        return param;
    }
 
    private String formatLicenseTime(LicenseContent install) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(install.getNotBefore()) + "  -  " + format.format(install.getNotAfter());
    }
 
}