mirror of
https://gitee.com/qqtt-ah/ecom-dmb.git
synced 2026-11-24 01:19:19 +08:00
feat: 新增授权管理
This commit is contained in:
+23
@@ -148,6 +148,29 @@ public class LicenseValidator {
|
||||
return cachedLicense.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 授权页展示用:缓存为空时从库加载并验签(不依赖 ENFORCED)。
|
||||
* 失败时返回 null,不抛错。
|
||||
*/
|
||||
public synchronized LicenseInfo tryLoadActiveLicense() {
|
||||
if (cachedLicense.get() != null) {
|
||||
return cachedLicense.get();
|
||||
}
|
||||
String licenseKey = resolveLicenseKey();
|
||||
if (!StringUtils.hasText(licenseKey) || publicJwk == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
LicenseInfo info = parseJwt(licenseKey.trim());
|
||||
validateBusinessRules(info);
|
||||
cachedLicense.set(info);
|
||||
return info;
|
||||
} catch (Exception e) {
|
||||
log.warn("加载已激活 License 失败: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/** 持久化 License 到数据库并刷新缓存 */
|
||||
public void persistLicense(LicenseInfo info) {
|
||||
// 旧记录全部置为无效
|
||||
|
||||
+19
-6
@@ -4,10 +4,12 @@ import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import top.xyquan.ecomdmbbackend.common.annotation.RequirePermission;
|
||||
import top.xyquan.ecomdmbbackend.common.response.ApiResponse;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.license.MachineFingerprintUtil;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.license.model.dto.ActivateLicenseDTO;
|
||||
@@ -16,9 +18,9 @@ import top.xyquan.ecomdmbbackend.modules.sys.license.model.vo.LicenseSummaryVO;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.license.service.LicenseService;
|
||||
|
||||
/**
|
||||
* 已登录用户的 License 管理 API
|
||||
* 系统设置 - 授权管理 API(已登录)
|
||||
*/
|
||||
@Tag(name = "系统-License", description = "License 信息查看与续期")
|
||||
@Tag(name = "系统-授权管理", description = "机器指纹查看与 License 激活/续期")
|
||||
@RestController
|
||||
@RequestMapping("/system")
|
||||
public class SystemLicenseController {
|
||||
@@ -29,21 +31,32 @@ public class SystemLicenseController {
|
||||
this.licenseService = licenseService;
|
||||
}
|
||||
|
||||
@Operation(summary = "获取当前 License 信息")
|
||||
@Operation(summary = "获取当前授权信息")
|
||||
@GetMapping("/license")
|
||||
@RequirePermission(value = "system:license:read", description = "查看授权信息")
|
||||
public ApiResponse<LicenseSummaryVO> getLicense() {
|
||||
return ApiResponse.success(licenseService.getLicenseSummary());
|
||||
}
|
||||
|
||||
@Operation(summary = "更新/续期 License")
|
||||
@Operation(summary = "激活/更新授权 License")
|
||||
@PutMapping("/license")
|
||||
@RequirePermission(value = "system:license:manage", description = "激活或更新授权")
|
||||
public ApiResponse<LicenseSummaryVO> updateLicense(@Valid @RequestBody ActivateLicenseDTO dto) {
|
||||
LicenseSummaryVO summary = licenseService.updateLicense(dto);
|
||||
return ApiResponse.success("License 更新成功", summary);
|
||||
return ApiResponse.success("授权更新成功", summary);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取机器指纹")
|
||||
@Operation(summary = "激活授权 License")
|
||||
@PostMapping("/license/activate")
|
||||
@RequirePermission(value = "system:license:manage", description = "激活授权")
|
||||
public ApiResponse<LicenseSummaryVO> activateLicense(@Valid @RequestBody ActivateLicenseDTO dto) {
|
||||
LicenseSummaryVO summary = licenseService.updateLicense(dto);
|
||||
return ApiResponse.success("授权激活成功", summary);
|
||||
}
|
||||
|
||||
@Operation(summary = "获取本机机器指纹")
|
||||
@GetMapping("/fingerprint")
|
||||
@RequirePermission(value = "system:license:read", description = "查看机器指纹")
|
||||
public ApiResponse<FingerprintVO> fingerprint() {
|
||||
return ApiResponse.success(FingerprintVO.builder()
|
||||
.fingerprint(MachineFingerprintUtil.computeFingerprint())
|
||||
|
||||
+17
-4
@@ -2,6 +2,7 @@ package top.xyquan.ecomdmbbackend.modules.sys.license.service;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
import top.xyquan.ecomdmbbackend.common.exception.BusinessException;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.license.LicenseEnforcement;
|
||||
import top.xyquan.ecomdmbbackend.modules.sys.license.LicenseInfo;
|
||||
@@ -14,7 +15,7 @@ import java.time.temporal.ChronoUnit;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* License 管理服务(不包含系统初始化逻辑)
|
||||
* License 管理服务(系统设置-授权页)
|
||||
*/
|
||||
@Service
|
||||
public class LicenseService {
|
||||
@@ -25,20 +26,32 @@ public class LicenseService {
|
||||
this.licenseValidator = licenseValidator;
|
||||
}
|
||||
|
||||
/**
|
||||
* 激活或更新 License:始终验签并落库,与 ENFORCED 开关无关(开关只影响业务门禁)。
|
||||
*/
|
||||
@Transactional
|
||||
public LicenseSummaryVO updateLicense(ActivateLicenseDTO dto) {
|
||||
if (!LicenseEnforcement.isEnforced()) {
|
||||
throw new BusinessException("License 校验未启用,无法更新");
|
||||
if (dto == null || !StringUtils.hasText(dto.getLicenseKey())) {
|
||||
throw new BusinessException("License Key 不能为空");
|
||||
}
|
||||
LicenseInfo info = licenseValidator.parseAndValidate(dto.getLicenseKey().trim());
|
||||
licenseValidator.persistLicense(info);
|
||||
return toSummary(info);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前授权摘要;无 License 时返回 inactive,不抛错,便于授权页展示。
|
||||
*/
|
||||
public LicenseSummaryVO getLicenseSummary() {
|
||||
LicenseInfo info = licenseValidator.getCachedLicense();
|
||||
if (info == null) {
|
||||
throw new BusinessException("当前无有效 License");
|
||||
info = licenseValidator.tryLoadActiveLicense();
|
||||
}
|
||||
if (info == null) {
|
||||
return LicenseSummaryVO.builder()
|
||||
.status(LicenseEnforcement.isEnforced() ? "inactive" : "dev-disabled")
|
||||
.features(List.of())
|
||||
.build();
|
||||
}
|
||||
return toSummary(info);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user