|
|
||
|---|---|---|
| docs | ||
| hardware-agent | ||
| hardware-info-tool | ||
| licence-client | ||
| licence-core | ||
| licence-server | ||
| .gitignore | ||
| Dockerfile | ||
| LICENSE | ||
| LICENSE_USAGE.md | ||
| README.md | ||
| agent | ||
| docker-compose.yml | ||
| pom.xml | ||
| sss | ||
README.md
License 认证系统
软件授权管理系统,支持硬件绑定、在线验证、客户管理等功能。
项目结构
├── licence-core # 核心模块(公私钥、证书生成校验逻辑)
├── licence-server # 服务端(Web界面 + API)
├── licence-client # 客户端(软件工厂集成)
├── hardware-info-tool # 硬件信息获取工具(给客户用)
├── hardware-agent # 宿主机 Agent(防拷贝方案)
└── docs # SQL 脚本
功能特性
- 公私钥签名证书,硬件绑定(MAC/IP/CPU/主板序列号)
- 系统时间回退检测
- 客户管理和 License 记录
- Agent 模式(宿主机安装,获取真实硬件信息,容器部署防拷贝)
快速开始
1. 初始化数据库
-- 创建数据库
CREATE DATABASE license_db;
-- 执行 SQL 脚本
mysql -u root -p license_db < docs/license_db.sql
2. 配置数据库连接
修改 licence-server/src/main/resources/application.yml:
spring:
datasource:
url: jdbc:mysql://localhost:3306/license_db?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
username: root
password: your_password
3. 启动服务
cd licence-server
mvn spring-boot:run
4. 访问
- 首页: http://localhost:8801/
- 生成证书: http://localhost:8801/generateLicense.html
- 管理后台: http://localhost:8801/license-admin.html
- H2 控制台(开发用): http://localhost:8801/h2-console
客户使用流程
生成 License
① 客户运行 hardware-info-tool 生成硬件信息配置文件
java -jar hardware-info-tool-1.0.jar gen
→ 生成 hardware-info.properties
② 客户把 hardware-info.properties 发给你
③ 你打开生成证书页面,填入硬件信息,生成 license.lic + publicCerts.keystore
④ 你把这两个文件发给客户
客户部署
客户部署需要先在宿主机上安装 Agent,容器内的业务服务通过调用 Agent API 获取硬件信息进行验证。
📁 license/
├── license.lic ← 你发的
└── publicCerts.ystore ← 你发的
配置 docker-compose.yml:
services:
your-app:
volumes:
- ./license:/data/license:ro
extra_hosts:
- "host.docker.internal:host-gateway"
配置 application.yml:
license:
subject: 软件工厂
publicAlias: publicCert
storePass: xxx
licensePath: /data/license/license.lic
publicKeysStorePath: /data/license/publicCerts.keystore
agent-api-url: http://host.docker.internal:8899/api/hardware-info
软件工厂接入
1. 引入 jar
<dependency>
<groupId>com.lcz</groupId>
<artifactId>licence-client</artifactId>
<version>1.0</version>
</dependency>
2. 配置 application.yml
licence-client 通过 Agent 模式获取硬件信息进行验证:
license:
subject: 你的软件名
publicAlias: publicCert
storePass: xxx
licensePath: /data/license/license.lic
publicKeysStorePath: /data/license/publicCerts.keystore
agent-api-url: http://host.docker.internal:8899/api/hardware-info
Agent 模式通过 HTTP 调用宿主机上的 Agent 服务获取真实硬件信息,容器内动态验证。
3. 启动时自动验证
licence-client 会在项目启动时自动读取配置并验证 License。
防拷贝方案
Agent 模式(更安全,推荐)
在宿主机上安装 Agent(详见 hardware-agent/README.md):
宿主机安装 Agent → 容器通过 Agent API 获取真实硬件信息 → 复制到其他机器无法调用 Agent → 验证失败
编译 Agent:
# Linux/macOS 本地编译
cd hardware-agent
CGO_ENABLED=0 go build -ldflags="-s -w" -o hardware-agent main.go
# Windows 交叉编译 Linux 版本(上传到 Linux 宿主机)
SET GOOS=linux SET GOARCH=amd64 SET CGO_ENABLED=0 go build -ldflags="-s -w" -o hardware-agent main.go
# powershell
$env:GOOS = "linux"
$env:GOARCH = "amd64"
$env:CGO_ENABLED = "0"
go build -ldflags="-s -w" -o agent main.go
# powershell
$env:GOOS = "linux"
$env:GOARCH = "arm64"
$env:CGO_ENABLED = "0"
go build -ldflags="-s -w" -o agent main.go
# macOS 交叉编译 Linux 版本
GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -ldflags="-s -w" -o hardware-agent main.go
部署:
scp hardware-agent user@host:/usr/local/bin/
ssh user@host "mv hardware-agent hardware-agent && chmod +x hardware-agent && nohup ./hardware-agent serve > /var/log/hardware-agent.log 2>&1 &"
● # 使用 systemd 管理开机自启
创建 service 文件:
sudo tee /etc/systemd/system/agent.service > /dev/null << 'EOF'
[Unit]
Description=Agent Service
After=network.target
[Service]
Type=simple
ExecStart=/usr/local/bin/agent serve
Restart=always
RestartSec=5
StandardOutput=append:/data/docker-data/microservices/logs/agent.log
StandardError=append:/data/docker-data/microservices/logs/agent.log
[Install]
WantedBy=multi-user.target
EOF
设置权限并启用服务:
chmod 755 /usr/local/bin/agent
sudo systemctl daemon-reload
sudo systemctl enable agent
sudo systemctl start agent
查看运行状态
sudo systemctl status agent
查看日志
sudo journalctl -u agent -f
停止
sudo systemctl stop agent
重启
sudo systemctl restart agent
参数说明:
- serve - 启动 HTTP 服务(默认端口 8899)
- serve 8888 - 指定端口 8889
Docker 容器配置:
Agent 默认监听端口 8899,容器内访问需要添加 extra_hosts:
services:
your-app:
extra_hosts:
- "host.docker.internal:host-gateway"
Agent API 接口:
| 接口 | 方法 | 说明 |
|---|---|---|
/health |
GET | 健康检查 |
/api/hardware-info |
GET | 获取硬件信息 |
响应示例:
{
"macAddress": ["00:1A:2B:3C:4D:5E"],
"ipAddress": ["192.168.1.100"],
"cpuSerial": "ABC123DEF456",
"mainBoardSerial": "XYZ789"
}
打包
# 打包所有模块
mvn clean package -DskipTests
# 输出
# licence-server/target/licence-server-1.0.war
# licence-client/target/licence-client-1.0.jar
# hardware-info-tool/target/hardware-info-tool-1.0.jar
部署架构
Agent 模式(容器部署)
┌─────────────────────────────────────────────────────────┐
│ 客户服务器 │
│ │
│ ┌───────────────┐ ┌─────────────────────────┐ │
│ │ Agent │ │ Docker 容器 │ │
│ │ (宿主机) │ │ ┌─────────────────┐ │ │
│ │ 端口:8899 │◄─────│ │ 业务服务 │ │ │
│ └───────────────┘ HTTP │ │ + licence-client│ │ │
│ │ │ │ │ │ │
│ │ │ ① install() │ │ │
│ │ │ ② verify() │ │ │
│ │ │ ↓ │ │ │
│ │ │ ③ 调用 Agent │ │ │
│ │ │ API 获取硬件 │ │ │
│ │ │ ↓ │ │ │
│ │ │ ④ 比对 license │ │ │
│ │ │ 中的硬件信息 │ │ │
│ │ └─────────────────┘ │ │
│ │ ▲ │ │
│ │ │ │ │
│ │ ┌──────┴──────┐ │ │
│ │ │ license/ │ │ │
│ │ │ - license.lic│ │ │
│ │ └─────────────┘ │ │
│ └─────────────────────────┘ │
└─────────────────────────────────────────────────────────┘