| | |
| | | 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 org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * |
| | |
| | | |
| | | @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)); |
| | | } |
| | | |
| | | /** |
| | |
| | | return R.ok().add(licenseDays); |
| | | } |
| | | |
| | | @RequestMapping(value = "/updateLicense") |
| | | public R updateLicense(@RequestParam("file") MultipartFile[] files){ |
| | | MultipartFile file = files[0]; |
| | | |
| | | String licensePathFileName = this.getClass().getClassLoader().getResource(licensePath).getPath(); |
| | | File licensePathFile = new File(licensePathFileName); |
| | | //服务器端保存的文件对象 |
| | | File serverFile = new File(licensePathFile.getPath()); |
| | | if (serverFile.exists()) { |
| | | try { |
| | | serverFile.delete();//存在文件,删除 |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | @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("许可证内容不能为空"); |
| | | } |
| | | |
| | | try { |
| | | //创建文件 |
| | | serverFile.createNewFile(); |
| | | //将上传的文件写入到服务器端文件内 |
| | | file.transferTo(serverFile); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | String licenseBase64 = param.getLicense().trim(); |
| | | LicenseVerifyParam verifyParam = buildVerifyParam(); |
| | | LicenseVerify licenseVerify = new LicenseVerify(); |
| | | LicenseContent install = licenseVerify.install(verifyParam, licenseBase64); |
| | | if (install == null) { |
| | | return R.error("许可证内容无效"); |
| | | } |
| | | |
| | | //重新加载许可证 |
| | | boolean loadedLicense = licenseCheckListener.loadLicense(); |
| | | if (loadedLicense) { |
| | | return R.ok(); |
| | | 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("许可证保存失败"); |
| | | } |
| | | 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(); |
| | | } |
| | | |
| | |
| | | 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()); |
| | | } |
| | | |
| | | } |