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.add(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, SM2.getPrivateKey());
|
|
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, SM2.getPrivateKey());
|
|
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;
|
}
|
|
|
public boolean updateTimeInUse(){
|
License license = licenseMapper.selectOne();
|
if(license!=null) {
|
String timeInUseDecrypt = license.getTimeInUse();
|
Long timeInUse = Long.valueOf(AESUtil.aesDecrypt(timeInUseDecrypt.toString()));
|
String timeInUseEncrypt = AESUtil.aesEncrypt(String.valueOf(timeInUse+60));
|
return licenseMapper.updateTimeInUse(timeInUseDecrypt)>0;
|
}else{
|
return false;
|
}
|
}
|
}
|