|
@@ -0,0 +1,169 @@
|
|
|
+package cn.com.qmth.stmms.biz.config.ukey;
|
|
|
+
|
|
|
+import net.sf.json.JSONArray;
|
|
|
+import net.sf.json.JSONObject;
|
|
|
+import net.sf.json.JsonConfig;
|
|
|
+
|
|
|
+import com.senseyun.openapi.SSRuntimeEasyJava.ErrorCode;
|
|
|
+import com.senseyun.openapi.SSRuntimeEasyJava.SSRuntimeEasy;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * U盾工具类
|
|
|
+ */
|
|
|
+public class UKeyHelper {
|
|
|
+
|
|
|
+ private final static long Language = 2;// 语言ID:中文=1、英语=2
|
|
|
+
|
|
|
+ public static boolean UKEY_STATUS = false;
|
|
|
+
|
|
|
+ public static String UKEY_MESSAGE = "U盾环境正在初始化";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * U盾环境初始化
|
|
|
+ */
|
|
|
+ public static void slmInitEasy(String apiPsd) throws Exception {
|
|
|
+ long result = SSRuntimeEasy.SlmInitEasy(apiPsd);
|
|
|
+ if (result != ErrorCode.SS_OK) {
|
|
|
+ SSRuntimeEasy.SlmErrorFormatEasy(result, Language);
|
|
|
+ // log.warn("SlmInitEasy Fail! {}", msg);
|
|
|
+ throw new Exception("U盾环境初始化失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ // log.info("SlmInitEasy Success!");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * U盾环境清理释放
|
|
|
+ */
|
|
|
+ public static void slmCleanupEasy() {
|
|
|
+ long result = SSRuntimeEasy.SlmCleanupEasy();
|
|
|
+ SSRuntimeEasy.SlmErrorFormatEasy(result, Language);
|
|
|
+ // log.info("SlmCleanupEasy {}", msg);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 释放分配的堆区域
|
|
|
+ */
|
|
|
+ public static void slmFreeEasy(Object obj) {
|
|
|
+ SSRuntimeEasy.SlmFreeEasy(obj);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取到当前计算机上所有插入的加密锁
|
|
|
+ */
|
|
|
+ public static List<DeviceInfo> slmEnumDeviceEasy() throws Exception {
|
|
|
+ Object obj = SSRuntimeEasy.SlmEnumDeviceEasy();
|
|
|
+ long result = SSRuntimeEasy.SlmGetLastError();
|
|
|
+ if (result != ErrorCode.SS_OK) {
|
|
|
+ SSRuntimeEasy.SlmErrorFormatEasy(result, Language);
|
|
|
+ // log.warn("SlmEnumDeviceEasy Fail! {}", msg);
|
|
|
+ throw new Exception("获取U盾信息失败");
|
|
|
+ }
|
|
|
+ JSONArray array = JSONArray.fromObject(obj.toString());
|
|
|
+ List<DeviceInfo> list = JSONArray.toList(array, new DeviceInfo(), new JsonConfig());
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取加密锁中的所有许可ID数组
|
|
|
+ */
|
|
|
+ public static List<Long> slmEnumLicenseIDEasy(String deviceInfo) throws Exception {
|
|
|
+ Object obj = SSRuntimeEasy.SlmEnumLicenseIDEasy(deviceInfo);
|
|
|
+ long result = SSRuntimeEasy.SlmGetLastError();
|
|
|
+ if (result != ErrorCode.SS_OK) {
|
|
|
+ SSRuntimeEasy.SlmErrorFormatEasy(result, Language);
|
|
|
+ // log.warn("SlmEnumLicenseIDEasy Fail! {}", msg);
|
|
|
+ throw new Exception("获取U盾许可信息失败");
|
|
|
+ }
|
|
|
+ JSONArray array = JSONArray.fromObject(obj.toString());
|
|
|
+ List<Long> list = JSONArray.toList(array,Long.class, new JsonConfig());
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取许可的详细信息
|
|
|
+ */
|
|
|
+ public static LicenseInfo slmGetLicenseInfoEasy(String deviceInfo, Long licenseId) throws Exception {
|
|
|
+ Object obj = SSRuntimeEasy.SlmGetLicenseInfoEasy(deviceInfo, licenseId);
|
|
|
+ long result = SSRuntimeEasy.SlmGetLastError();
|
|
|
+ if (result != ErrorCode.SS_OK) {
|
|
|
+ SSRuntimeEasy.SlmErrorFormatEasy(result, Language);
|
|
|
+ // log.warn("SlmGetLicenseInfoEasy Fail! {}", msg);
|
|
|
+ throw new Exception("获取U盾许可信息失败");
|
|
|
+ }
|
|
|
+ JSONObject json = JSONObject.fromObject(obj.toString());
|
|
|
+ LicenseInfo list = (LicenseInfo) JSONObject.toBean(json,new JsonConfig());
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * U盾许可验证
|
|
|
+ */
|
|
|
+ public static boolean verifyUKey(Long curLicenseId) throws Exception {
|
|
|
+ List<DeviceInfo> devices = UKeyHelper.slmEnumDeviceEasy();
|
|
|
+ if (devices==null || devices.isEmpty()) {
|
|
|
+ throw new Exception("未检测到U盾设备");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
+
|
|
|
+ for (DeviceInfo device : devices) {
|
|
|
+ String deviceJson = JSONObject.fromObject(device).toString();
|
|
|
+ // System.out.println(deviceJson);
|
|
|
+
|
|
|
+ List<Long> licenseIds = slmEnumLicenseIDEasy(deviceJson);
|
|
|
+ if (licenseIds==null || licenseIds.isEmpty()) {
|
|
|
+ throw new Exception("获取U盾许可信息失败");
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Long licenseId : licenseIds) {
|
|
|
+ // System.out.println("licenseId:" + licenseId);
|
|
|
+
|
|
|
+ if (!licenseId.equals(curLicenseId)) {
|
|
|
+ // 跳过不匹配的许可信息
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ LicenseInfo license = slmGetLicenseInfoEasy(deviceJson, licenseId);
|
|
|
+ // System.out.println(jsonMapper.toJson(license));
|
|
|
+
|
|
|
+ LockInfo lock = device.getLock_info();
|
|
|
+ if (lock == null || license.getStart_time() == null || license.getEnd_time() == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ boolean status = lock.getClock() >= license.getStart_time() && lock.getClock() <= license.getEnd_time();
|
|
|
+ // Date startTime = new Date(license.getStart_time() * 1000L);
|
|
|
+ // Date endTime = new Date(license.getEnd_time() * 1000L);
|
|
|
+
|
|
|
+ // log.info("licenseId:{} 许可开始时间:{} 许可结束时间:{} 状态:{}",
|
|
|
+ // licenseId, sdf.format(startTime), sdf.format(endTime), status ? "正常可用" :
|
|
|
+ // "已过期");
|
|
|
+
|
|
|
+ slmFreeEasy(license);
|
|
|
+ return status;
|
|
|
+ }
|
|
|
+
|
|
|
+ slmFreeEasy(licenseIds);
|
|
|
+ }
|
|
|
+
|
|
|
+ slmFreeEasy(devices);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String arg[]) throws Exception {
|
|
|
+ try {
|
|
|
+// System.setProperty("java.library.path", "/Users/yin/Desktop/API/mac/Java/lib64/libSSRuntimeEasyJava.dylib");
|
|
|
+
|
|
|
+ UKeyHelper.slmInitEasy("3266297f8519bc7e8107ac0871dd4915");
|
|
|
+
|
|
|
+ UKeyHelper.verifyUKey(100000002L);
|
|
|
+ } finally {
|
|
|
+ UKeyHelper.slmCleanupEasy();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|