|
|
1 hónapja | |
|---|---|---|
| .. | ||
| example | 1 hónapja | |
| src | 1 hónapja | |
| 1 密钥与License生成流程.mermaid | 1 hónapja | |
| 2 License验证流程.mermaid | 1 hónapja | |
| 3 部署结构示意图 .mermaid | 1 hónapja | |
| LICENSE_SIGNATURE_ISSUE_SOLUTION.md | 1 hónapja | |
| QUICK_START.md | 1 hónapja | |
| README.md | 1 hónapja | |
| SIGNATURE_FIX_SUMMARY.md | 1 hónapja | |
| TestSignature.class | 1 hónapja | |
| TestSignature.java | 1 hónapja | |
| pom.xml | 1 hónapja | |
基于国密SM2/SM3算法的Spring Boot License授权管理组件,提供完整的软件授权解决方案。
NOTIFICATION_ONLY): 仅发送通知,不影响系统运行PREVENT_RESTART): 许可证过期后阻止应用重启GRACEFUL_SHUTDOWN): 许可证过期后优雅关闭应用在您的Spring Boot项目中添加依赖:
<dependency>
<groupId>com.bskj.license</groupId>
<artifactId>byjc-spring-boot-starter-license</artifactId>
<version>1.0.0</version>
</dependency>
在 application.yml 中添加配置:
bskj:
license:
file-path: "license.json"
public-key-path: "public.key"
# 生成RSA密钥对
java -cp bskj-license-starter-2025.08-SNAPSHOT.jar com.bskj.framework.license.generator.KeyGenerator private.key public.key
# 引导式生成license文件
java -cp "target/classes;target/dependency/*" com.bskj.framework.license.generator.LicenseGenerator
# 或者指定私钥和输出文件路径
java -cp "target/classes;target/dependency/*" com.bskj.framework.license.generator.LicenseGenerator private.key license.json
程序会引导您输入以下信息:
# 使用java -cp命令生成license文件
java -cp "target/classes;target/dependency/*" com.bskj.framework.license.generator.LicenseGenerator private.key license.json "客户名称"
@RestController
public class PatientController {
@LicenseRequired(modules = {"patient_management"})
@GetMapping("/patients")
public List<Patient> getPatients() {
// 需要patient_management模块授权
return patientService.getAllPatients();
}
@LicenseRequired(checkUserLimit = true, strict = true)
@PostMapping("/patients")
public Patient createPatient(@RequestBody Patient patient) {
// 检查用户数限制,严格模式
return patientService.createPatient(patient);
}
}
bskj:
license:
# 授权策略
default-strategy: "notification_only" # notification_only | prevent_restart | graceful_shutdown
# 检查配置
check-on-startup: true
enable-scheduled-check: true
check-interval-seconds: 3600
# 时间配置
default-warning-days: 30 # 提前预警天数
default-grace-period-days: 7 # 宽限期天数
# 通知配置
notification:
log-enabled: true
email-enabled: true
email-recipients:
- "admin@hospital.com"
- "it@hospital.com"
jmx-enabled: false
@LicenseRequired(
modules = {"billing", "reporting"}, // 需要的模块权限
checkUserLimit = true, // 是否检查用户数限制
allowExpired = false, // 是否允许过期时执行
strict = false, // 是否为严格模式
message = "需要计费模块授权" // 自定义错误消息
)
License文件采用JSON格式,包含以下信息:
{
"licenseId": "uuid",
"customerName": "医院名称",
"productName": "医疗管理系统",
"productVersion": "1.0.0",
"startTime": "2024-01-01T00:00:00",
"endTime": "2025-01-01T00:00:00",
"strategy": "notification_only",
"gracePeriodDays": 7,
"warningDays": 30,
"maxUsers": 100,
"modulePermissions": {
"patient_management": true,
"billing": true,
"reporting": false
},
"extraProperties": {
"hospitalCode": "H001",
"region": "北京"
},
"createdTime": "2024-01-01T00:00:00",
"signature": "base64-encoded-signature"
}
您可以监听license相关事件来实现自定义逻辑:
@Component
public class LicenseEventListener {
@EventListener
public void handleStatusChange(LicenseMonitorService.LicenseStatusChangeEvent event) {
// 处理状态变化
logger.info("License status changed from {} to {}",
event.getPreviousStatus(), event.getCurrentStatus());
}
@EventListener
public void handleNotification(LicenseMonitorService.LicenseNotificationEvent event) {
// 处理通知事件
sendEmailNotification(event.getTitle(), event.getMessage());
}
@EventListener
public void handleShutdown(LicenseMonitorService.LicenseServiceShutdownEvent event) {
// 处理服务停机事件
logger.error("Service shutdown requested due to license: {}", event.getReason());
// 执行优雅停机逻辑
}
}
除了注解方式,您也可以直接使用LicenseService:
@Service
public class BusinessService {
@Autowired
private LicenseService licenseService;
public void performBusinessOperation() {
// 检查license状态
if (!licenseService.isLicenseValid()) {
throw new BusinessException("License无效");
}
// 检查模块权限
if (!licenseService.isModulePermitted("advanced_analytics")) {
throw new BusinessException("高级分析模块未授权");
}
// 检查用户数限制
int currentUsers = getCurrentUserCount();
if (!licenseService.isUserCountWithinLimit(currentUsers)) {
throw new BusinessException("用户数超过限制");
}
// 执行业务逻辑
doBusinessLogic();
}
}
项目提供了完整的测试套件,确保所有功能正常工作:
# 运行完整的集成测试
mvn test -Dtest=LicenseIntegrationTest
# 运行所有测试
mvn test
[测试] 密钥生成测试
✓ 密钥生成测试通过
- 私钥文件: /tmp/private.key
- 公钥文件: /tmp/public.key
[测试] License生成测试
✓ License生成测试通过
- License文件: /tmp/license.json
- License ID: TEST-LICENSE-1692345678901
- 客户名称: 测试客户
[测试] 签名验证测试
✓ 签名验证测试通过
- 签名数据长度: 245
- 签名数据: licenseId=TEST-LICENSE-1692345678901;customerName=测试客户;...
============================================================
所有测试通过!License系统工作正常
============================================================
# 编译项目
mvn compile
# 复制依赖
mvn dependency:copy-dependencies
# 生成密钥对
java -cp "target/classes;target/dependency/*" com.bskj.framework.license.generator.KeyGenerator private.key public.key
或者使用Maven exec插件:
mvn "exec:java@keygen" "-Dexec.args=private.key public.key"
# 生成License文件(需要先生成密钥对)
java -cp "target/classes;target/dependency/*" com.bskj.framework.license.generator.LicenseGenerator private.key license.json
KeyGenerator generator = new KeyGenerator();
generator.generateAndSaveKeyPair("private.key", "public.key");
LicenseGenerator generator = new LicenseGenerator();
generator.loadPrivateKey("private.key");
LicenseInfo license = LicenseGenerator.builder()
.customerName("北京某医院")
.productName("医疗管理系统")
.productVersion("2.0.0")
.startTime(LocalDateTime.now())
.endTime(LocalDateTime.now().plusYears(1))
.strategy(LicenseStrategy.PREVENT_RESTART)
.maxUsers(200)
.modulePermission("patient_management", true)
.modulePermission("billing", true)
.modulePermission("reporting", true)
.extraProperty("hospitalLevel", "三甲")
.build();
generator.generateLicense(license, "license.json");
TimestampLocalDateTimeSerializer将时间转为时间戳appendField方法中特殊处理LocalDateTime字段私钥保护
License分发
监控告警
生产环境
测试环境
关键指标
告警设置
代码同步
测试驱动
常见问题处理
技术支持
management:
endpoints:
web:
exposure:
include: health,info,metrics
metrics:
tags:
application: license-system
@Component
public class LicenseMetrics {
private final Counter verificationSuccessCounter;
private final Counter verificationFailureCounter;
private final Timer verificationTimer;
// 监控License验证性能
}
# 1. 生成新的License文件
java -jar license-generator.jar --emergency
# 2. 验证新License
mvn test -Dtest=LicenseIntegrationTest
# 3. 部署新License
cp new-license.json /path/to/application/license.json
# 1. 备份当前License
cp license.json license.json.backup
# 2. 恢复之前版本
cp license.json.old license.json
# 3. 重启应用验证
systemctl restart application
问题描述: [详细描述问题现象]
错误信息: [完整的错误日志]
环境信息: [操作系统、Java版本、应用版本]
复现步骤: [详细的复现步骤]
测试结果: [LicenseIntegrationTest运行结果]
Fork项目
git clone https://github.com/your-username/bskj-license-starter.git
cd bskj-license-starter
创建功能分支
git checkout -b feature/your-feature-name
开发和测试
# 运行测试确保功能正常
mvn test -Dtest=LicenseIntegrationTest
# 运行所有测试
mvn test
提交代码
git add .
git commit -m "feat: 添加新功能描述"
git push origin feature/your-feature-name
创建Pull Request
Java代码规范
测试规范
提交信息规范
type(scope): description
[optional body]
[optional footer]
类型说明:
feat: 新功能fix: 修复bugdocs: 文档更新test: 测试相关refactor: 代码重构Bug报告
LicenseIntegrationTest并提供结果功能请求
版本规划
发布检查清单
本项目采用 MIT 许可证 - 查看 LICENSE 文件了解详情。
感谢所有为本项目做出贡献的开发者和用户。
特别感谢:
📧 联系我们: tech-support@bskj.com
🌐 官方网站: https://www.bskj.com
📖 在线文档: https://docs.bskj.com/license