pom.xml
@@ -182,6 +182,11 @@ <version>1.1.3.3</version> <systemPath>${project.basedir}/src/main/resources/lib/xpp3-1.1.3.3.jar</systemPath> </dependency> <dependency> <groupId>de.schlichtherle.truelicense</groupId> <artifactId>truelicense-core</artifactId> <version>1.33</version> </dependency> </dependencies> <build> src/main/java/com/zy/asrs/controller/ConsoleController.java
@@ -26,6 +26,7 @@ import com.zy.core.properties.SlaveProperties; import com.zy.core.properties.SystemProperties; import com.zy.core.thread.*; import com.zy.system.entity.license.LicenseVerify; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -69,7 +70,16 @@ } } Thread.sleep(200L); SystemProperties.WCS_RUNNING_STATUS.set(param.getOperatorType()==1?Boolean.TRUE:Boolean.FALSE); //验证许可证是否有效 try{ if (new LicenseVerify().verify()){ SystemProperties.WCS_RUNNING_STATUS.set(param.getOperatorType()==1?Boolean.TRUE:Boolean.FALSE); return R.ok().add(Cools.add("status", SystemProperties.WCS_RUNNING_STATUS.get())); } } catch (Exception e){ log.error("许可证验证失败!!异常:{}",e.getMessage()); } SystemProperties.WCS_RUNNING_STATUS.set(Boolean.FALSE); return R.ok().add(Cools.add("status", SystemProperties.WCS_RUNNING_STATUS.get())); } src/main/java/com/zy/asrs/service/impl/MainServiceImpl.java
@@ -1,14 +1,20 @@ package com.zy.asrs.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.core.common.Cools; import com.core.common.R; import com.core.exception.CoolException; import com.zy.asrs.entity.*; import com.zy.asrs.service.BasCircularShuttleService; import com.zy.asrs.service.BasDevpPositionService; import com.zy.asrs.service.WrkMastService; import com.zy.asrs.task.core.ReturnT; import com.zy.asrs.utils.SortTheExecutionOfTheCarUtil; import com.zy.asrs.utils.TimeCalculatorUtils; import com.zy.common.CodeRes; import com.zy.common.utils.HttpHandler; import com.zy.core.cache.MessageQueue; import com.zy.core.cache.SlaveConnection; import com.zy.core.enums.*; @@ -18,6 +24,7 @@ import com.zy.core.model.protocol.RgvProtocol; import com.zy.core.properties.SlaveProperties; import com.zy.core.thread.RgvThread; import com.zy.system.entity.license.LicenseVerify; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -57,6 +64,19 @@ @Value("${constant-parameters.rgvDate}") private Double rgvDate; /* * 验证许可证是否有效 * */ public synchronized boolean licenseVerify() { try{ return new LicenseVerify().verify(); } catch (Exception e){ log.info("许可证验证失败!!异常:{}",e.getMessage()); } return false; } /** * 站点任务检测 更新小车位置信息 */ src/main/java/com/zy/asrs/utils/SortTheExecutionOfTheCarUtil.java
@@ -91,7 +91,7 @@ Integer integer = LatelyAndLessThan(devpPosition, nowPosition,perimeter); for (BasDevpPosition basDevpPosition:devpPosition){ if (basDevpPosition.getDevNo().equals(integer)){ if (basDevpPosition.getDevNo() == 133){ if (basDevpPosition.getDevNo() == 134){ result = 101; } break; src/main/java/com/zy/common/CodeRes.java
@@ -9,4 +9,5 @@ String USER_10002 = "10002-账号已被禁用"; String USER_10003 = "10003-密码错误"; String SYSTEM_20001 = "20001-许可证已失效"; } src/main/java/com/zy/common/web/AuthController.java
@@ -4,22 +4,19 @@ import com.baomidou.mybatisplus.mapper.EntityWrapper; import com.baomidou.mybatisplus.mapper.Wrapper; import com.core.annotations.ManagerAuth; import com.core.common.Cools; import com.core.common.R; import com.core.common.*; import com.core.exception.CoolException; import com.zy.common.CodeRes; import com.zy.common.entity.Parameter; import com.zy.common.model.PowerDto; import com.zy.common.utils.RandomValidateCodeUtil; import com.zy.system.entity.*; import com.zy.system.entity.license.LicenseVerify; import com.zy.system.service.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.util.*; @@ -49,15 +46,21 @@ @RequestMapping("/login.action") @ManagerAuth(value = ManagerAuth.Auth.NONE, memo = "登录") public R loginAction(String mobile, String password){ if (mobile.equals("super") && password.equals(Cools.md5(superPwd))) { public R loginAction(String username, String password){ //验证许可证是否有效 LicenseVerify licenseVerify = new LicenseVerify(); boolean verify = licenseVerify.verify(); if (!verify) {//许可证已失效 return R.parse(CodeRes.SYSTEM_20001); } if (username.equals("super") && password.equals(Cools.md5(superPwd))) { Map<String, Object> res = new HashMap<>(); res.put("username", mobile); res.put("token", Cools.enToken(System.currentTimeMillis() + mobile, superPwd)); res.put("username", username); res.put("token", Cools.enToken(System.currentTimeMillis() + username, superPwd)); return R.ok(res); } EntityWrapper<User> userWrapper = new EntityWrapper<>(); userWrapper.eq("mobile", mobile); userWrapper.eq("username", username); User user = userService.selectOne(userWrapper); if (Cools.isEmpty(user)){ return R.parse(CodeRes.USER_10001); @@ -68,11 +71,12 @@ if (!user.getPassword().equals(password)){ return R.parse(CodeRes.USER_10003); } String token = Cools.enToken(System.currentTimeMillis() + mobile, user.getPassword()); String token = Cools.enToken(System.currentTimeMillis() + username, user.getPassword()); userLoginService.delete(new EntityWrapper<UserLogin>().eq("user_id", user.getId())); UserLogin userLogin = new UserLogin(); userLogin.setUserId(user.getId()); userLogin.setToken(token); userLogin.setCreateTime(new Date()); userLoginService.insert(userLogin); Map<String, Object> res = new HashMap<>(); res.put("username", user.getUsername()); @@ -113,64 +117,71 @@ return R.ok(userService.selectById(getUserId())); } @RequestMapping("/menu/auth") @ManagerAuth(memo = "首页菜单") public R menu(){ // 获取所有一级菜单 List<Resource> oneLevel = resourceService.selectList(new EntityWrapper<Resource>().eq("level", 1).eq("status", 1).orderBy("sort")); User user = null; Wrapper<Resource> resourceWrapper; if (getUserId() == 9527) { resourceWrapper = new EntityWrapper<Resource>().eq("level", 2).eq("status", 1).orderBy("sort"); } else { // 获取当前用户的所有二级菜单 user = userService.selectById(getUserId()); List<RoleResource> roleResources = roleResourceService.selectList(new EntityWrapper<RoleResource>().eq("role_id", user.getRoleId())); List<Long> resourceIds = new ArrayList<>(); roleResources.forEach(roleResource -> resourceIds.add(roleResource.getResourceId())); if (resourceIds.isEmpty()){ return R.ok(); } resourceWrapper = new EntityWrapper<Resource>().in("id", resourceIds).eq("level", 2).eq("status", 1).orderBy("sort"); } List<Resource> twoLevel = resourceService.selectList(resourceWrapper); List<Map<String, Object>> result = new ArrayList<>(); for (Resource menu : oneLevel) { Map<String, Object> map = new HashMap<>(); List<Resource> subMenu = new ArrayList<>(); Iterator<Resource> iterator = twoLevel.iterator(); while (iterator.hasNext()) { Resource resource = iterator.next(); if (resource.getResourceId() != null && resource.getResourceId().equals(menu.getId())) { // 是否拥有查看权限 if (getUserId() != 9527) { Resource view = resourceService.selectOne(new EntityWrapper<Resource>().eq("resource_id", resource.getId()).like("code", "view")); if (!Cools.isEmpty(view)){ RoleResource param = new RoleResource(); param.setResourceId(view.getId()); param.setRoleId(user.getRoleId()); if (null == roleResourceService.selectOne(new EntityWrapper<>(param))){ continue; } } } subMenu.add(resource); iterator.remove(); } } if (subMenu.isEmpty()) { continue; } map.put("menuId", menu.getId()); map.put("menuCode", menu.getCode()); map.put("menu", menu.getName()); map.put("subMenu", subMenu); result.add(map); } return R.ok(result); } // @RequestMapping("/menu/auth") // @ManagerAuth // public R menu(){ // // 获取所有一级菜单 // List<Resource> oneLevel; // User user = null; // Wrapper<Resource> resourceWrapper; // if (getUserId() == 9527) { // oneLevel = resourceService.selectList(new EntityWrapper<Resource>().eq("level", 1).orderBy("sort")); // resourceWrapper = new EntityWrapper<Resource>().eq("level", 2).eq("status", 1).orderBy("sort"); // } else { // // 激活码验证 // if (!SystemProperties.SYSTEM_ACTIVATION) { // return R.ok(); // } // oneLevel = resourceService.selectList(new EntityWrapper<Resource>().eq("level", 1).eq("status", 1).orderBy("sort")); // // 获取当前用户的所有二级菜单 // user = userService.selectById(getUserId()); // List<RoleResource> roleResources = roleResourceService.selectList(new EntityWrapper<RoleResource>().eq("role_id", user.getRoleId())); // List<Long> resourceIds = new ArrayList<>(); // roleResources.forEach(roleResource -> resourceIds.add(roleResource.getResourceId())); // if (resourceIds.isEmpty()){ // return R.ok(); // } // resourceWrapper = new EntityWrapper<Resource>().in("id", resourceIds).eq("level", 2).eq("status", 1).orderBy("sort"); // } // List<Resource> twoLevel = resourceService.selectList(resourceWrapper); // List<Map<String, Object>> result = new ArrayList<>(); // for (Resource menu : oneLevel) { // Map<String, Object> map = new HashMap<>(); // List<Resource> subMenu = new ArrayList<>(); // Iterator<Resource> iterator = twoLevel.iterator(); // while (iterator.hasNext()) { // Resource resource = iterator.next(); // if (resource.getResourceId() != null && resource.getResourceId().equals(menu.getId())) { // // // 是否拥有查看权限 // if (getUserId() != 9527) { // Resource view = resourceService.selectOne(new EntityWrapper<Resource>().eq("resource_id", resource.getId()).like("code", "#view")); // if (!Cools.isEmpty(view)){ // RoleResource param = new RoleResource(); // param.setResourceId(view.getId()); // param.setRoleId(user.getRoleId()); // if (null == roleResourceService.selectOne(new EntityWrapper<>(param))){ // continue; // } // } // } // // subMenu.add(resource); // iterator.remove(); // } // } // if (subMenu.isEmpty()) { // continue; // } // map.put("menuId", menu.getId()); // map.put("menuCode", menu.getCode()); // map.put("menuIcon", HtmlNavIconType.get(menu.getCode())); // map.put("menu", menu.getName()); // map.put("subMenu", subMenu); // result.add(map); // } // return R.ok(result); // } @RequestMapping("/power/list/auth") @ManagerAuth @@ -323,6 +334,49 @@ } return R.ok(resources); } // // @GetMapping(value = "/system/activation/auth") // public R activation() { // if (SystemProperties.SYSTEM_ACTIVATION) { // String activationCode = SystemProperties.getActivationCode(OSinfo.getOSname().getActivationCodePath()); // String timeStr = AesUtils.decrypt(activationCode, SystemProperties.SALT); // if (null == timeStr) { // SystemProperties.SYSTEM_ACTIVATION = Boolean.FALSE; // return R.error(); // } // Date exprTime = DateUtils.convert(timeStr, DateUtils.yyyyMMddHHmmss); // if (new Date().getTime() < exprTime.getTime()) { // return R.ok().add(DateUtils.convert(exprTime)); // } else { // SystemProperties.SYSTEM_ACTIVATION = Boolean.FALSE; // return R.error(); // } // } else { // return R.error(); // } // } // // @PostMapping(value = "/system/secret/auth") // @ManagerAuth // public R systemSecret(@RequestParam(value = "secret") String secret) { // if (Cools.isEmpty(secret)) { // return R.error("请输入激活码"); // } // // 验证激活码 // String timeStr = AesUtils.decrypt(secret, SystemProperties.SALT); // if (null == timeStr) { // return R.error("激活码错误"); // } // Date exprTime = DateUtils.convert(timeStr, DateUtils.yyyyMMddHHmmss); // if (new Date().getTime() >= exprTime.getTime()) { // return R.error("激活码已失效"); // } // boolean result = SystemProperties.saveActivationCode(OSinfo.getOSname().getActivationCodePath(), secret); // if (!result) { // return R.error("激活失败"); // } // SystemProperties.SYSTEM_ACTIVATION = Boolean.TRUE; // return R.ok("激活成功,有效期至"+DateUtils.convert(exprTime)); // } } src/main/java/com/zy/core/MainProcess.java
@@ -35,6 +35,17 @@ */ public void start(){ thread = new Thread(() -> { try{ Thread.sleep(200); log.info("++++++++ 开始验证许可证 ++++++++"); if (!mainService.licenseVerify()){ SystemProperties.WCS_RUNNING_STATUS.set(Boolean.FALSE); log.info("++++++++ 验证许可证失败 ++++++++"); } else { SystemProperties.WCS_RUNNING_STATUS.set(Boolean.TRUE); log.info("++++++++ 验证许可证成功 ++++++++"); } } catch (Exception e) {} while (!Thread.currentThread().isInterrupted()) { try { // 间隔 @@ -44,6 +55,7 @@ if (!SystemProperties.WCS_RUNNING_STATUS.get()) { continue; } //更新位置信息 mainService.updateStePositionNearby(); //更新位置信息 src/main/java/com/zy/core/ServerBootstrap.java
@@ -5,7 +5,9 @@ import com.zy.core.enums.SlaveType; import com.zy.core.model.*; import com.zy.core.properties.SlaveProperties; import com.zy.core.properties.SystemProperties; import com.zy.core.thread.*; import com.zy.system.entity.license.LicenseVerify; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Async; @@ -130,4 +132,15 @@ public void destroy() { } @Component class licenseVerifyThread { @Scheduled(cron = "0 0 4 * * ?") public void licenseVerify() { if (!new LicenseVerify().verify()){ SystemProperties.WCS_RUNNING_STATUS.set(Boolean.FALSE); } } } } src/main/java/com/zy/system/controller/LicenseCreatorController.java
New file @@ -0,0 +1,120 @@ package com.zy.system.controller; import com.core.annotations.ManagerAuth; import com.core.common.Cools; import com.core.common.R; import com.zy.system.entity.license.*; 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.util.ClassUtils; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; 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.time.temporal.ChronoUnit; import java.util.Date; import java.util.HashMap; import java.util.Map; import java.util.prefs.Preferences; /** * * 用于生成证书文件,不能放在给客户部署的代码里 */ @RestController @RequestMapping("/license") public class LicenseCreatorController { @Value("${license.licensePath}") private String licensePath; @Autowired private LicenseCheckListener licenseCheckListener; /** * 获取服务器硬件信息 * @param osName 操作系统类型,如果为空则自动判断 */ @RequestMapping(value = "/getServerInfos",produces = {MediaType.APPLICATION_JSON_UTF8_VALUE}) public LicenseCheck getServerInfos(@RequestParam(value = "osName",required = false) String osName) { //操作系统类型 if(Cools.isEmpty(osName)){ osName = System.getProperty("os.name"); } osName = osName.toLowerCase(); AbstractServerInfos abstractServerInfos = null; //根据不同操作系统类型选择不同的数据获取方法 if (osName.startsWith("windows")) { abstractServerInfos = new WindowsServerInfos(); } else if (osName.startsWith("linux")) { // abstractServerInfos = new LinuxServerInfos(); }else{//其他服务器类型 abstractServerInfos = new WindowsServerInfos(); } return abstractServerInfos.getServerInfos(); } /** * 获取许可证有效期天数 */ @RequestMapping(value = "/getLicenseDays") public R getLicenseDays() { LicenseVerify licenseVerify = new LicenseVerify(); LicenseContent verifyInfo = licenseVerify.getVerifyInfo(); if (verifyInfo == null) { return R.error(); } Date start = new Date(); Date end = verifyInfo.getNotAfter(); Long starTime = start.getTime(); Long endTime = end.getTime(); Long num = endTime - starTime;//时间戳相差的毫秒数 int day = (int) (num / 24 / 60 / 60 / 1000); return R.ok().add(Cools.add("day",day)); } @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(); } } try { //创建文件 serverFile.createNewFile(); //将上传的文件写入到服务器端文件内 file.transferTo(serverFile); } catch (IOException e) { e.printStackTrace(); } //重新加载许可证 boolean loadedLicense = licenseCheckListener.loadLicense(); if (loadedLicense) { return R.ok(); } return R.error("许可证更新失败"); } } src/main/java/com/zy/system/entity/license/AbstractServerInfos.java
New file @@ -0,0 +1,111 @@ package com.zy.system.entity.license; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; /** * 用于获取客户服务器的基本信息,如:IP、Mac地址、CPU序列号、主板序列号等 */ public abstract class AbstractServerInfos { private static Logger logger = LogManager.getLogger(AbstractServerInfos.class); /** * 组装需要额外校验的License参数 */ public LicenseCheck getServerInfos(){ LicenseCheck result = new LicenseCheck(); try { result.setIpAddress(this.getIpAddress()); result.setMacAddress(this.getMacAddress()); result.setCpuSerial(this.getCPUSerial()); result.setMainBoardSerial(this.getMainBoardSerial()); }catch (Exception e){ logger.error("获取服务器硬件信息失败",e); } return result; } /** * 获取IP地址 */ protected abstract List<String> getIpAddress() throws Exception; /** * 获取Mac地址 */ protected abstract List<String> getMacAddress() throws Exception; /** * 获取CPU序列号 */ protected abstract String getCPUSerial() throws Exception; /** * 获取主板序列号 */ protected abstract String getMainBoardSerial() throws Exception; /** * 获取当前服务器所有符合条件的InetAddress */ protected List<InetAddress> getLocalAllInetAddress() throws Exception { List<InetAddress> result = new ArrayList<>(4); // 遍历所有的网络接口 for (Enumeration networkInterfaces = NetworkInterface.getNetworkInterfaces(); networkInterfaces.hasMoreElements(); ) { NetworkInterface iface = (NetworkInterface) networkInterfaces.nextElement(); // 在所有的接口下再遍历IP for (Enumeration inetAddresses = iface.getInetAddresses(); inetAddresses.hasMoreElements(); ) { InetAddress inetAddr = (InetAddress) inetAddresses.nextElement(); //排除LoopbackAddress、SiteLocalAddress、LinkLocalAddress、MulticastAddress类型的IP地址 if(!inetAddr.isLoopbackAddress() /*&& !inetAddr.isSiteLocalAddress()*/ && !inetAddr.isLinkLocalAddress() && !inetAddr.isMulticastAddress()){ result.add(inetAddr); } } } return result; } /** * 获取某个网络接口的Mac地址 */ protected String getMacByInetAddress(InetAddress inetAddr){ try { byte[] mac = NetworkInterface.getByInetAddress(inetAddr).getHardwareAddress(); StringBuffer stringBuffer = new StringBuffer(); for(int i=0;i<mac.length;i++){ if(i != 0) { stringBuffer.append("-"); } //将十六进制byte转化为字符串 String temp = Integer.toHexString(mac[i] & 0xff); if(temp.length() == 1){ stringBuffer.append("0" + temp); }else{ stringBuffer.append(temp); } } return stringBuffer.toString().toUpperCase(); } catch (SocketException e) { e.printStackTrace(); } return null; } } src/main/java/com/zy/system/entity/license/CustomKeyStoreParam.java
New file @@ -0,0 +1,57 @@ package com.zy.system.entity.license; import de.schlichtherle.license.AbstractKeyStoreParam; import java.io.*; /** * 自定义KeyStoreParam,用于将公私钥存储文件存放到其他磁盘位置而不是项目中 */ public class CustomKeyStoreParam extends AbstractKeyStoreParam { /** * 公钥/私钥在磁盘上的存储路径 */ private String storePath; private String alias; private String storePwd; private String keyPwd; public CustomKeyStoreParam(Class clazz, String resource, String alias, String storePwd, String keyPwd) { super(clazz, resource); this.storePath = resource; this.alias = alias; this.storePwd = storePwd; this.keyPwd = keyPwd; } @Override public String getAlias() { return alias; } @Override public String getStorePwd() { return storePwd; } @Override public String getKeyPwd() { return keyPwd; } /** * 复写de.schlichtherle.license.AbstractKeyStoreParam的getStream()方法<br/> * 用于将公私钥存储文件存放到其他磁盘位置而不是项目中 */ @Override public InputStream getStream() throws IOException { final InputStream in = new FileInputStream(new File(storePath)); if (null == in) { throw new FileNotFoundException(storePath); } return in; } } src/main/java/com/zy/system/entity/license/CustomLicenseManager.java
New file @@ -0,0 +1,241 @@ package com.zy.system.entity.license; import com.core.common.Cools; import de.schlichtherle.license.*; import de.schlichtherle.xml.GenericCertificate; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.beans.XMLDecoder; import java.io.BufferedInputStream; import java.io.ByteArrayInputStream; import java.io.UnsupportedEncodingException; import java.util.Date; import java.util.List; /** * 自定义LicenseManager,用于增加额外的服务器硬件信息校验 */ public class CustomLicenseManager extends LicenseManager{ private static Logger logger = LogManager.getLogger(CustomLicenseManager.class); //XML编码 private static final String XML_CHARSET = "UTF-8"; //默认BUFSIZE private static final int DEFAULT_BUFSIZE = 8 * 1024; public CustomLicenseManager() { } public CustomLicenseManager(LicenseParam param) { super(param); } /** * 复写create方法 */ @Override protected synchronized byte[] create( LicenseContent content, LicenseNotary notary) throws Exception { initialize(content); this.validateCreate(content); final GenericCertificate certificate = notary.sign(content); return getPrivacyGuard().cert2key(certificate); } /** * 复写install方法,其中validate方法调用本类中的validate方法,校验IP地址、Mac地址等其他信息 */ @Override protected synchronized LicenseContent install( final byte[] key, final LicenseNotary notary) throws Exception { final GenericCertificate certificate = getPrivacyGuard().key2cert(key); notary.verify(certificate); final LicenseContent content = (LicenseContent)this.load(certificate.getEncoded()); this.validate(content); setLicenseKey(key); setCertificate(certificate); return content; } /** * 复写verify方法,调用本类中的validate方法,校验IP地址、Mac地址等其他信息 */ @Override protected synchronized LicenseContent verify(final LicenseNotary notary) throws Exception { GenericCertificate certificate = getCertificate(); // Load license key from preferences, final byte[] key = getLicenseKey(); if (null == key){ throw new NoLicenseInstalledException(getLicenseParam().getSubject()); } certificate = getPrivacyGuard().key2cert(key); notary.verify(certificate); final LicenseContent content = (LicenseContent)this.load(certificate.getEncoded()); this.validate(content); setCertificate(certificate); return content; } /** * 校验生成证书的参数信息 */ protected synchronized void validateCreate(final LicenseContent content) throws LicenseContentException { final LicenseParam param = getLicenseParam(); final Date now = new Date(); final Date notBefore = content.getNotBefore(); final Date notAfter = content.getNotAfter(); if (null != notAfter && now.after(notAfter)){ throw new LicenseContentException("证书失效时间不能早于当前时间"); } if (null != notBefore && null != notAfter && notAfter.before(notBefore)){ throw new LicenseContentException("证书生效时间不能晚于证书失效时间"); } final String consumerType = content.getConsumerType(); if (null == consumerType){ throw new LicenseContentException("用户类型不能为空"); } } /** * 复写validate方法,增加IP地址、Mac地址等其他信息校验 */ @Override protected synchronized void validate(final LicenseContent content) throws LicenseContentException { // 1. 首先调用父类的validate方法 super.validate(content); //2. 然后校验自定义的License参数 //License中可被允许的参数信息 LicenseCheck expectedCheckModel = (LicenseCheck) content.getExtra(); //当前服务器真实的参数信息 LicenseCheck serverCheckModel = getServerInfos(); if(expectedCheckModel != null && serverCheckModel != null){ //校验IP地址 if(!checkIpAddress(expectedCheckModel.getIpAddress(),serverCheckModel.getIpAddress())){ throw new LicenseContentException("当前服务器的IP没在授权范围内"); } //校验Mac地址 if(!checkIpAddress(expectedCheckModel.getMacAddress(),serverCheckModel.getMacAddress())){ throw new LicenseContentException("当前服务器的Mac地址没在授权范围内"); } //校验主板序列号 if(!checkSerial(expectedCheckModel.getMainBoardSerial(),serverCheckModel.getMainBoardSerial())){ throw new LicenseContentException("当前服务器的主板序列号没在授权范围内"); } //校验CPU序列号 if(!checkSerial(expectedCheckModel.getCpuSerial(),serverCheckModel.getCpuSerial())){ throw new LicenseContentException("当前服务器的CPU序列号没在授权范围内"); } }else{ throw new LicenseContentException("不能获取服务器硬件信息"); } } /** * 重写XMLDecoder解析XML */ private Object load(String encoded){ BufferedInputStream inputStream = null; XMLDecoder decoder = null; try { inputStream = new BufferedInputStream(new ByteArrayInputStream(encoded.getBytes(XML_CHARSET))); decoder = new XMLDecoder(new BufferedInputStream(inputStream, DEFAULT_BUFSIZE),null,null); return decoder.readObject(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } finally { try { if(decoder != null){ decoder.close(); } if(inputStream != null){ inputStream.close(); } } catch (Exception e) { logger.error("XMLDecoder解析XML失败",e); } } return null; } /** * 获取当前服务器需要额外校验的License参数 */ private LicenseCheck getServerInfos(){ //操作系统类型 String osName = System.getProperty("os.name").toLowerCase(); AbstractServerInfos abstractServerInfos = null; //根据不同操作系统类型选择不同的数据获取方法 if (osName.startsWith("windows")) { abstractServerInfos = new WindowsServerInfos(); } else if (osName.startsWith("linux")) { // abstractServerInfos = new LinuxServerInfos(); }else{//其他服务器类型 abstractServerInfos = new WindowsServerInfos(); } return abstractServerInfos.getServerInfos(); } /** * 校验当前服务器的IP/Mac地址是否在可被允许的IP范围内<br/> * 如果存在IP在可被允许的IP/Mac地址范围内,则返回true */ private boolean checkIpAddress(List<String> expectedList,List<String> serverList){ if(expectedList != null && expectedList.size() > 0){ if(serverList != null && serverList.size() > 0){ for(String expected : expectedList){ if(serverList.contains(expected.trim())){ return true; } } } return false; }else { return true; } } /** * 校验当前服务器硬件(主板、CPU等)序列号是否在可允许范围内 */ private boolean checkSerial(String expectedSerial,String serverSerial){ if(!Cools.isEmpty(expectedSerial)){ if(!Cools.isEmpty(serverSerial)){ if(expectedSerial.equals(serverSerial)){ return true; } } return false; }else{ return true; } } } src/main/java/com/zy/system/entity/license/LicenseCheck.java
New file @@ -0,0 +1,45 @@ package com.zy.system.entity.license; import lombok.Data; import java.io.Serializable; import java.util.List; /** * 自定义需要校验的License参数 */ @Data public class LicenseCheck implements Serializable { private static final long serialVersionUID = 8600137500316662317L; /** * 可被允许的IP地址 */ private List<String> ipAddress; /** * 可被允许的MAC地址 */ private List<String> macAddress; /** * 可被允许的CPU序列号 */ private String cpuSerial; /** * 可被允许的主板序列号 */ private String mainBoardSerial; @Override public String toString() { return "LicenseCheckModel{" + "ipAddress=" + ipAddress + ", macAddress=" + macAddress + ", cpuSerial='" + cpuSerial + '\'' + ", mainBoardSerial='" + mainBoardSerial + '\'' + '}'; } } src/main/java/com/zy/system/entity/license/LicenseCheckListener.java
New file @@ -0,0 +1,94 @@ package com.zy.system.entity.license; import com.core.common.Cools; import de.schlichtherle.license.LicenseContent; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; import java.io.File; /** * 在项目启动时安装证书 */ @Component public class LicenseCheckListener implements ApplicationListener<ContextRefreshedEvent> { private static Logger logger = LogManager.getLogger(LicenseCheckListener.class); /** * 证书subject */ @Value("${license.subject}") private String subject; /** * 公钥别称 */ @Value("${license.publicAlias}") private String publicAlias; /** * 访问公钥库的密码 */ @Value("${license.storePass}") private String storePass; /** * 证书生成路径 */ @Value("${license.licensePath}") private String licensePath; /** * 密钥库存储路径 */ @Value("${license.publicKeysStorePath}") private String publicKeysStorePath; @Override public void onApplicationEvent(ContextRefreshedEvent event) { //root application context 没有parent ApplicationContext context = event.getApplicationContext().getParent(); if(context == null){ loadLicense(); } } //加载证书 public boolean loadLicense() { if(!Cools.isEmpty(licensePath)){ logger.info("++++++++ 开始加载许可证 ++++++++"); try { String publicKeysStoreFileName = this.getClass().getClassLoader().getResource(publicKeysStorePath).getPath(); File publicKeysStoreFile = new File(publicKeysStoreFileName); String licensePathFileName = this.getClass().getClassLoader().getResource(licensePath).getPath(); File licensePathFile = new File(licensePathFileName); LicenseVerifyParam param = new LicenseVerifyParam(); param.setSubject(subject); param.setPublicAlias(publicAlias); param.setStorePass(storePass); param.setLicensePath(licensePathFile.getPath()); param.setPublicKeysStorePath(publicKeysStoreFile.getPath()); LicenseVerify licenseVerify = new LicenseVerify(); //安装证书 LicenseContent install = licenseVerify.install(param); logger.info("++++++++ 许可证加载结束 ++++++++"); return install != null; } catch (Exception e) { return false; } } return false; } } src/main/java/com/zy/system/entity/license/LicenseCreator.java
New file @@ -0,0 +1,86 @@ package com.zy.system.entity.license; import de.schlichtherle.license.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import javax.security.auth.x500.X500Principal; import java.io.File; import java.text.MessageFormat; import java.util.prefs.Preferences; /** * License生成类 */ public class LicenseCreator { private static Logger logger = LogManager.getLogger(LicenseCreator.class); private final static X500Principal DEFAULT_HOLDER_AND_ISSUER = new X500Principal("CN=localhost, OU=localhost, O=localhost, L=SH, ST=SH, C=CN"); private LicenseCreatorParam param; public LicenseCreator(LicenseCreatorParam param) { this.param = param; } /** * 生成License证书 */ public boolean generateLicense(){ try { LicenseManager licenseManager = new CustomLicenseManager(initLicenseParam()); LicenseContent licenseContent = initLicenseContent(); licenseManager.store(licenseContent,new File(param.getLicensePath())); return true; }catch (Exception e){ logger.error(MessageFormat.format("证书生成失败:{0}",param),e); return false; } } /** * 初始化证书生成参数 */ private LicenseParam initLicenseParam(){ Preferences preferences = Preferences.userNodeForPackage(LicenseCreator.class); //设置对证书内容加密的秘钥 CipherParam cipherParam = new DefaultCipherParam(param.getStorePass()); KeyStoreParam privateStoreParam = new CustomKeyStoreParam(LicenseCreator.class ,param.getPrivateKeysStorePath() ,param.getPrivateAlias() ,param.getStorePass() ,param.getKeyPass()); LicenseParam licenseParam = new DefaultLicenseParam(param.getSubject() ,preferences ,privateStoreParam ,cipherParam); return licenseParam; } /** * 设置证书生成正文信息 */ private LicenseContent initLicenseContent(){ LicenseContent licenseContent = new LicenseContent(); licenseContent.setHolder(DEFAULT_HOLDER_AND_ISSUER); licenseContent.setIssuer(DEFAULT_HOLDER_AND_ISSUER); licenseContent.setSubject(param.getSubject()); licenseContent.setIssued(param.getIssuedTime()); licenseContent.setNotBefore(param.getIssuedTime()); licenseContent.setNotAfter(param.getExpiryTime()); licenseContent.setConsumerType(param.getConsumerType()); licenseContent.setConsumerAmount(param.getConsumerAmount()); licenseContent.setInfo(param.getDescription()); //扩展校验服务器硬件信息 licenseContent.setExtra(param.getLicenseCheck()); return licenseContent; } } src/main/java/com/zy/system/entity/license/LicenseCreatorParam.java
New file @@ -0,0 +1,96 @@ package com.zy.system.entity.license; import com.fasterxml.jackson.annotation.JsonFormat; import lombok.Data; import java.io.Serializable; import java.util.Date; /** * License生成类需要的参数 */ @Data public class LicenseCreatorParam implements Serializable { private static final long serialVersionUID = -7793154252684580872L; /** * 证书subject */ private String subject; /** * 密钥别称 */ private String privateAlias; /** * 密钥密码(需要妥善保管,不能让使用者知道) */ private String keyPass; /** * 访问秘钥库的密码 */ private String storePass; /** * 证书生成路径 */ private String licensePath; /** * 密钥库存储路径 */ private String privateKeysStorePath; /** * 证书生效时间 */ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date issuedTime = new Date(); /** * 证书失效时间 */ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date expiryTime; /** * 用户类型 */ private String consumerType = "user"; /** * 用户数量 */ private Integer consumerAmount = 1; /** * 描述信息 */ private String description = ""; /** * 额外的服务器硬件校验信息 */ private LicenseCheck licenseCheck; @Override public String toString() { return "LicenseCreatorParam{" + "subject='" + subject + '\'' + ", privateAlias='" + privateAlias + '\'' + ", keyPass='" + keyPass + '\'' + ", storePass='" + storePass + '\'' + ", licensePath='" + licensePath + '\'' + ", privateKeysStorePath='" + privateKeysStorePath + '\'' + ", issuedTime=" + issuedTime + ", expiryTime=" + expiryTime + ", consumerType='" + consumerType + '\'' + ", consumerAmount=" + consumerAmount + ", description='" + description + '\'' + ", licenseCheck=" + licenseCheck + '}'; } } src/main/java/com/zy/system/entity/license/LicenseManagerHolder.java
New file @@ -0,0 +1,22 @@ package com.zy.system.entity.license; import de.schlichtherle.license.LicenseManager; import de.schlichtherle.license.LicenseParam; public class LicenseManagerHolder { private static volatile LicenseManager LICENSE_MANAGER; public static LicenseManager getInstance(LicenseParam param) { if (LICENSE_MANAGER == null) { synchronized (LicenseManagerHolder.class) { if (LICENSE_MANAGER == null) { LICENSE_MANAGER = new CustomLicenseManager(param); } } } return LICENSE_MANAGER; } } src/main/java/com/zy/system/entity/license/LicenseVerify.java
New file @@ -0,0 +1,133 @@ package com.zy.system.entity.license; import de.schlichtherle.license.*; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import java.io.File; import java.text.DateFormat; import java.text.MessageFormat; import java.text.SimpleDateFormat; import java.util.prefs.Preferences; /** * License校验类 */ public class LicenseVerify { private static Logger logger = LogManager.getLogger(LicenseVerify.class); /** * 安装License证书 */ public synchronized LicenseContent install(LicenseVerifyParam param){ LicenseContent result = null; DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //1. 安装证书 try{ LicenseManager licenseManager = LicenseManagerHolder.getInstance(initLicenseParam(param)); licenseManager.uninstall(); result = licenseManager.install(new File(param.getLicensePath())); logger.info(MessageFormat.format("许可证加载成功,许可证有效期:{0} - {1}",format.format(result.getNotBefore()),format.format(result.getNotAfter()))); }catch (Exception e){ logger.error("许可证加载失败!{}",e.getMessage()); } return result; } /** * 校验License证书 */ public boolean verify(){ try { LicenseManager licenseManager = LicenseManagerHolder.getInstance(null); DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); if (!updateSystemTime()) { //时间更新失败,系统时间被更改 return false; } LicenseContent licenseContent = licenseManager.verify(); logger.info(MessageFormat.format("许可证校验通过,许可证有效期:{0} - {1}",format.format(licenseContent.getNotBefore()),format.format(licenseContent.getNotAfter()))); return true; }catch (Exception e){ logger.error("许可证校验失败!{}",e.getMessage()); return false; } } /** * 校验License证书并获取证书信息 */ public LicenseContent getVerifyInfo(){ LicenseManager licenseManager = LicenseManagerHolder.getInstance(null); if (!updateSystemTime()) { //时间更新失败,系统时间被更改 return null; } //校验证书 try { LicenseContent licenseContent = licenseManager.verify(); return licenseContent; }catch (Exception e){ logger.error("许可证校验失败!",e); return null; } } /** * 初始化证书生成参数 * @param param License校验类需要的参数 * @return de.schlichtherle.license.LicenseParam */ private LicenseParam initLicenseParam(LicenseVerifyParam param){ Preferences preferences = Preferences.userNodeForPackage(LicenseVerify.class); CipherParam cipherParam = new DefaultCipherParam(param.getStorePass()); KeyStoreParam publicStoreParam = new CustomKeyStoreParam(LicenseVerify.class ,param.getPublicKeysStorePath() ,param.getPublicAlias() ,param.getStorePass() ,null); return new DefaultLicenseParam(param.getSubject() ,preferences ,publicStoreParam ,cipherParam); } /** * 更新时间到注册表中 */ private boolean updateSystemTime() { // 获取用户根节点 Preferences userRoot = Preferences.userRoot(); // 获取指定路径下的节点 Preferences node = userRoot.node("/zhongyang"); String key = "time"; // 读取注册表 String value = node.get(key, null); if (value != null) { long originTime = Long.parseLong(value); long now = System.currentTimeMillis(); long diff = now - originTime;//现在时间 - 源时间 = 时间差 if (diff > 0) { //时间差大于0才允许更新注册表时间 node.put(key, String.valueOf(System.currentTimeMillis())); return true; } }else { // 写入注册表 node.put(key, String.valueOf(System.currentTimeMillis())); return true; } return false; } } src/main/java/com/zy/system/entity/license/LicenseVerifyParam.java
New file @@ -0,0 +1,48 @@ package com.zy.system.entity.license; import lombok.Data; /** * 校验签名文件 */ @Data public class LicenseVerifyParam { /** * 证书subject */ private String subject; /** * 公钥别称 */ private String publicAlias; /** * 访问公钥库的密码 */ private String storePass; /** * 证书生成路径 */ private String licensePath; /** * 密钥库存储路径 */ private String publicKeysStorePath; public LicenseVerifyParam() { } public LicenseVerifyParam(String subject, String publicAlias, String storePass, String licensePath, String publicKeysStorePath) { this.subject = subject; this.publicAlias = publicAlias; this.storePass = storePass; this.licensePath = licensePath; this.publicKeysStorePath = publicKeysStorePath; } } src/main/java/com/zy/system/entity/license/WindowsServerInfos.java
New file @@ -0,0 +1,85 @@ package com.zy.system.entity.license; import java.net.InetAddress; import java.util.List; import java.util.Scanner; import java.util.stream.Collectors; /** * 用于获取客户Windows服务器的基本信息 */ public class WindowsServerInfos extends AbstractServerInfos { @Override protected List<String> getIpAddress() throws Exception { List<String> result = null; //获取所有网络接口 List<InetAddress> inetAddresses = getLocalAllInetAddress(); if(inetAddresses != null && inetAddresses.size() > 0){ result = inetAddresses.stream().map(InetAddress::getHostAddress).distinct().map(String::toLowerCase).collect(Collectors.toList()); } return result; } @Override protected List<String> getMacAddress() throws Exception { List<String> result = null; //1. 获取所有网络接口 List<InetAddress> inetAddresses = getLocalAllInetAddress(); if(inetAddresses != null && inetAddresses.size() > 0){ //2. 获取所有网络接口的Mac地址 result = inetAddresses.stream().map(this::getMacByInetAddress).distinct().collect(Collectors.toList()); } return result; } @Override protected String getCPUSerial() throws Exception { //序列号 String serialNumber = ""; //使用WMIC获取CPU序列号 Process process = Runtime.getRuntime().exec("wmic cpu get processorid"); process.getOutputStream().close(); Scanner scanner = new Scanner(process.getInputStream()); if(scanner.hasNext()){ scanner.next(); } if(scanner.hasNext()){ serialNumber = scanner.next().trim(); } scanner.close(); return serialNumber; } @Override protected String getMainBoardSerial() throws Exception { //序列号 String serialNumber = ""; //使用WMIC获取主板序列号 Process process = Runtime.getRuntime().exec("wmic baseboard get serialnumber"); process.getOutputStream().close(); Scanner scanner = new Scanner(process.getInputStream()); if(scanner.hasNext()){ scanner.next(); } if(scanner.hasNext()){ serialNumber = scanner.next().trim(); } scanner.close(); return serialNumber; } } src/main/resources/application.yml
@@ -8,8 +8,8 @@ name: @pom.build.finalName@ datasource: driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver url: jdbc:sqlserver://127.0.0.1:50751;databasename=gdhmasrs # url: jdbc:sqlserver://127.0.0.1:50948;databasename=gdhmasrs # url: jdbc:sqlserver://127.0.0.1:50751;databasename=gdhmasrs url: jdbc:sqlserver://127.0.0.1:50948;databasename=gdhmasrs username: sa password: sa@123 mvc: @@ -32,6 +32,14 @@ swagger: enable: false #License相关配置 license: subject: gdhmasrs publicAlias: publicCert storePass: public_zhongyang_123456789 licensePath: license.lic publicKeysStorePath: publicCerts.keystore constant-parameters: # 轨道总长 perimeter: 1737000 src/main/resources/license.licBinary files differ
src/main/resources/publicCerts.keystoreBinary files differ
src/main/webapp/views/index.html
@@ -184,6 +184,9 @@ <!--</header>--> <div id="app"> <div class="map"> <div v-if="licenseDayI <=30 "> <div style="color: red">{{licenseDay}}</div> </div> <!-- <div class="inner-ring"></div>--> <!-- <div class="outer-ring"></div>--> <!-- <div v-for="track in energyGatheringRing" class="inner-ring" :style="{ borderColor: track.trackColor , boxShadow : 'inset 0 0 30px '+track.radiationColor+', 0 0 20px '+track.radiationColor}"></div>--> @@ -352,6 +355,8 @@ activeNames: ['1'], valueLeft: '0', valueRight: '0', licenseDay: '已过期', licenseDayI: 100, valueSystem: false, tableDataRgv: [], tableDataDev: [], @@ -414,6 +419,7 @@ this.getTableDataLeft() this.getTableDataRight() this.getValueSystem() this.getLicenseDays() setInterval(() => { this.getTableDataRgv() @@ -422,6 +428,7 @@ this.getTableDataLeft() this.getTableDataRight() this.getValueSystem() this.getLicenseDays() }, 1000) }, @@ -461,6 +468,29 @@ } return ''; }, getLicenseDays(){ let that = this; $.ajax({ url: baseUrl + "/license/getLicenseDays", headers: {'token': localStorage.getItem('token')}, method: 'POST', success: function (res) { if (res.code == 200) { if (res.data.day<0){ that.licenseDay = "已过期"+res.data.day+"天"; that.licenseDayI = -1; } else { that.licenseDay = "许可证有效期"+res.data.day+"天"; that.licenseDayI = res.data.day; } }else { that.licenseDay = "已过期"; that.licenseDayI = -1; } } }); }, getValueSystem() { let that = this; $.ajax({