bifang-api 授权调用支持

This commit is contained in:
fengjunfeng
2021-12-29 11:37:15 +08:00
commit d6826dcd42
17 changed files with 1148 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
package com.sentinel.license;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class LicenseApplication {
public static void main(String[] args) {
SpringApplication.run(LicenseApplication.class, args);
}
}

View File

@@ -0,0 +1,45 @@
package com.sentinel.license.bean;
/**
* @author xzyuan
* @date Created in 2019/5/9 14:08
* @description状态码枚举类
* @version: 1.0
*/
public enum Code {
/*
OK[GET]服务器成功返回用户请求的数据该操作是幂等的Idempotent
*/
C200(200, "Success"),
/*
INTERNAL SERVER ERROR - [*]:服务器发生错误,用户将无法判断发出的请求是否成功
*/
C500(500, "Internal Server Error"),
C999(999, "Error")
;
/**
* Code 状态码
*/
private Integer code;
/**
* desc 描述
*/
private String desc;
Code(Integer code, String desc) {
this.code = code;
this.desc = desc;
}
public Integer getCode() {
return code;
}
public String getDesc() {
return desc;
}
}

View File

@@ -0,0 +1,23 @@
package com.sentinel.license.bean;
import lombok.Data;
/**
* @author fengjunfeng
* @date 2021/12/28
* @time 18:06
* @description
**/
@Data
public class LicenseInfo {
private String V2C;
private String vendorCode;
private Integer featureId;
// 1 获取指纹 2 更新license 3 获取license信息 4 验证license信息
private Integer type;
}

View File

@@ -0,0 +1,73 @@
package com.sentinel.license.bean;
import java.util.Arrays;
public class Ret<T> {
private Integer code;
private String msg;
private T data;
private boolean success;
private Integer [] normal = {200,201,202,204,304};
public Ret() {
}
public Ret(Integer code, String msg, T data) {
this.code = code;
this.msg = msg;
this.data = data;
if(Arrays.asList(normal).contains(code)){
this.success=true;
}else {
this.success=false;
}
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("{");
builder.append("'code':").append(code).append(",");
builder.append("'msg':").append(msg).append(",");
builder.append("'success':").append(success).append(",");
builder.append("}");
return builder.toString();
}
public Integer getCode() {
return code;
}
public void setCode(Integer code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
}

View File

@@ -0,0 +1,27 @@
package com.sentinel.license.bean;
public class Rets {
public static Ret success(Object data) {
return new Ret(Code.C200.getCode(), Code.C200.getDesc(), data);
}
public static Ret failure(String msg) {
return new Ret(Code.C999.getCode(), msg, null);
}
public static Ret success() {
return new Ret(Code.C200.getCode(), Code.C200.getDesc(), null);
}
public static Ret failure(Integer code, String msg, Object data) {
return new Ret(code, msg, data);
}
public static Ret failure(Integer code, String msg) {
return new Ret(code, msg, null);
}
}

View File

@@ -0,0 +1,44 @@
package com.sentinel.license.controller;
import cn.hutool.log.Log;
import com.sentinel.license.bean.LicenseInfo;
import com.sentinel.license.utils.HaspUtil;
import org.springframework.web.bind.annotation.*;
/**
* @author fengjunfeng
* @date 2021/12/28
* @time 16:00
* @description
**/
@RestController
public class LicenseController {
private static final Log log = Log.get();
/**
* license锁信息交互
*
* @return
*/
@PostMapping("/licenseOperation")
public Object licenseOperation(@RequestBody LicenseInfo licenseInfo) {
if (licenseInfo.getType() == 1){
//获取C2V信息
return HaspUtil.readC2V(licenseInfo.getVendorCode(),licenseInfo.getFeatureId());
}else if (licenseInfo.getType() == 2){
//上传license信息
return HaspUtil.updateKeyWithLicense(licenseInfo.getV2C());
}else if (licenseInfo.getType() == 3){
//获取license信息
return HaspUtil.getLicenseInfo(licenseInfo.getVendorCode(),licenseInfo.getFeatureId());
}else if (licenseInfo.getType() == 4){
//验证license信息
return HaspUtil.verify(licenseInfo.getVendorCode(),licenseInfo.getFeatureId());
}else {
log.info("type error");
return null;
}
}
}

View File

@@ -0,0 +1,190 @@
package com.sentinel.license.utils;
import Aladdin.Hasp;
import Aladdin.HaspStatus;
import cn.hutool.log.Log;
public class HaspUtil {
private static final Log log = Log.get();
/** Runtime query scope */
protected final static String KEY_SCOPE = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
+ "<haspscope>\n"
+ "<license_manager hostname=\"localhost\" />\n"
+ "</haspscope>\n";
/** Runtime query view */
protected final static String KEY_VIEW = "<haspformat format=\"host_fingerprint\"/>";
protected final static String LICENSE_STATUS =
"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>" +
"<haspformat root=\"hasp_info\">" +
" <feature>" +
" <attribute name=\"id\" />" +
" <element name=\"license\" />" +
" <hasp>" +
" <attribute name=\"id\" />" +
" <attribute name=\"type\" />" +
" </hasp>" +
" </feature>" +
"</haspformat>" +
"";
protected final static String KEY_C2V_FORMAT = "<haspformat format=\"updateinfo\"/>";
public final static String ALL_FORMAT = "<haspformat>\n" +
" <hasp>\n" +
" <element name=\"id\"/>\n" +
" <element name=\"type\"/>\n" +
" <element name=\"configuration\"/>\n" +
" <element name=\"clone_protected\"/>\n" +
" <element name=\"disabled\"/>\n" +
" <element name=\"production_date\"/>\n" +
" <element name=\"updatecounter\"/>\n" +
" <element name=\"parentid\"/>\n" +
" <feature>\n" +
" <element name=\"id\"/>\n" +
" <element name=\"locked\"/>\n" +
" <element name=\"license\"/>\n" +
" <element name=\"name\"/>\n" +
" <element name=\"concurrency\"/>\n" +
" <element name=\"usable\"/>\n" +
" </feature>\n" +
" <product>\n" +
" <element name=\"id\"/>\n" +
" <element name=\"name\"/>\n" +
" </product>\n" +
" <license_manager>\n" +
" <element name=\"host_fingerprint\"/>\n" +
" <element name=\"hostname\"/>\n" +
" <element name=\"osname\"/>\n" +
" <element name=\"osversion\"/>\n" +
" <element name=\"name\"/>\n" +
" <element name=\"time\"/>\n" +
" <element name=\"uptime\"/>\n" +
" <element name=\"architecture\"/>\n" +
" <element name=\"ip\"/>\n" +
" </license_manager>\n" +
" <vendor>\n" +
" <element name=\"id\"/>\n" +
" <element name=\"name\"/>\n" +
" </vendor>\n" +
" </hasp>\n" +
"</haspformat>";
public final static String HASP_FORMAT = "<haspformat>\n" +
" <hasp>\n" +
" <element name=\"id\"/>\n" +
" <element name=\"type\"/>\n" +
" <element name=\"configuration\"/>\n" +
" <element name=\"clone_protected\"/>\n" +
" <element name=\"disabled\"/>\n" +
" <element name=\"production_date\"/>\n" +
" <element name=\"updatecounter\"/>\n" +
" <element name=\"parentid\"/>\n" +
" <feature>\n" +
" <element name=\"id\"/>\n" +
" <element name=\"locked\"/>\n" +
" <element name=\"license\"/>\n" +
" <element name=\"name\"/>\n" +
" <element name=\"concurrency\"/>\n" +
" <element name=\"usable\"/>\n" +
" </feature>\n" +
" <product>\n" +
" <element name=\"id\"/>\n" +
" <element name=\"name\"/>\n" +
" </product>\n" +
" </hasp>\n" +
"</haspformat>";
public static String getLicenseInfo(String vendorCode, Integer featureId) {
Hasp hasp = new Hasp(featureId);
hasp.login(vendorCode);
int status = hasp.getLastError();
log.info("license status: {}", status);
if(status == HaspStatus.HASP_STATUS_OK) {
String licenseInfo = hasp.getSessionInfo(LICENSE_STATUS);
status = hasp.getLastError();
log.info("license status: {}", status);
if (status != HaspStatus.HASP_STATUS_OK) {
log.error("Error: updateLicense Fails with status code :{}",status);
}
return licenseInfo;
}else if (status == HaspStatus.HASP_FEATURE_EXPIRED){
//过期
String licenseInfo = hasp.getInfo("<?xml version=\"1.0\" encoding=\"UTF-8\" ?><haspscope><feature id=\"\n" +
featureId +
"\"/></haspscope>", LICENSE_STATUS,vendorCode);
return licenseInfo;
}else {
log.error("Error: with status code :{}",status);
return null;
}
}
/**
* Updates the key with the license (v2c)
*
* @param v2c
* @return
* @throws Exception
*/
public static int updateKeyWithLicense(String v2c) {
Hasp hasp = new Hasp(Hasp.HASP_DEFAULT_FID);
hasp.update(v2c);
int status = hasp.getLastError();
log.info("license status: {}", status);
if (status != HaspStatus.HASP_STATUS_OK) {
log.error("Error: updateLicense Fails with status code :{}",status);
}
return status;
}
/**
*
* @param vendorCode
* @return
* @throws Exception
*/
public static String readC2V(String vendorCode, Integer featureId) {
Hasp hasp = new Hasp(featureId);
hasp.login(vendorCode);
int status = hasp.getLastError();
log.info("license status: {}", status);
if(status == HaspStatus.HASP_STATUS_OK) {
//如果已经安装过license ,读取指纹和 hasp id
String licenseInfo = hasp.getSessionInfo(KEY_C2V_FORMAT);
status = hasp.getLastError();
log.info("license status: {}", status);
if (status != HaspStatus.HASP_STATUS_OK) {
log.error("Error: updateLicense Fails with status code :{}",status);
}
return licenseInfo;
}else {
hasp = new Hasp(featureId);
String infos = hasp.getInfo(KEY_SCOPE, KEY_VIEW, vendorCode);
status = hasp.getLastError();
log.info("license status: {}", status);
if(status != HaspStatus.HASP_STATUS_OK) {
log.error("Error: getLicense c2v Fails with status code :{}",status);
throw new RuntimeException();
}
return infos;
}
}
public static int verify(String vendorCode, Integer fetureId) {
Hasp hasp = new Hasp(fetureId);
hasp.login(vendorCode);
int status = hasp.getLastError();
log.info("license status: {}", status);
if(status != HaspStatus.HASP_STATUS_OK) {
log.error("Error: license login Fails with status code :{}",status);
}
hasp.logout();
return status;
}
}