代理租赁实现

This commit is contained in:
lu7213 committed 2026-10-22 19:58:57 -04:00
1 parent 1c0544afe0
commit 2a9f1c4c7c
38 files changed
+5824 -6

No files matched your search

@@ -0,0 +1,133 @@
package com.ruoyi.system.rental;
import static com.ruoyi.system.rental.RentalValues.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.Instant;
import java.util.*;
/** 外部APP和本地NODE共用凭证认证,登录用户归属仍由sys_user及RBAC决定。 */
@Service
public class RentalIdentity {
private final RentalStore store;
private final RentalCrypto crypto;
@Value("${rental.master-key-version:v1}")
private String masterVersion;
public RentalIdentity(RentalStore store, RentalCrypto crypto) {
this.store = store;
this.crypto = crypto;
}
public Map<String, Object> credential(String key) {
Map<String, Object> row = store.one("SELECT * FROM rent_credential WHERE api_key=?", key);
require(
row != null
&& "ACTIVE".equals(text(row, "status"))
&& (row.get("expires_at") == null
|| time(row.get("expires_at")).isAfter(Instant.now())),
"租赁凭证无效");
return row;
}
public String secret(Map<String, Object> credential) {
return crypto.decrypt(text(credential, "api_secret"));
}
@Transactional(rollbackFor = Exception.class)
public Map<String, Object> authenticate(
String key,
String ownerType,
String scope,
long timestamp,
String nonce,
String method,
String path,
String body,
String signature) {
require(
key != null
&& key.length() <= 64
&& nonce != null
&& nonce.matches("[A-Za-z0-9_-]{16,64}")
&& timestamp > System.currentTimeMillis() - 60000
&& timestamp < System.currentTimeMillis() + 60000,
"认证时间或随机数无效");
Map<String, Object> row = credential(key);
require(
ownerType.equals(text(row, "owner_type"))
&& RentalCrypto.equal(
RentalCrypto.sign(
secret(row),
scope,
key,
timestamp,
nonce,
method,
path,
body),
signature),
"租赁签名无效");
store.insert(
"nonce",
map("api_key", key, "scope", scope, "nonce", nonce, "expires_at", after(180)));
return row;
}
/** 轮换保留历史Secret,仅明确在途任务可以使用VERIFY_ONLY,不能签发新任务。 */
@Transactional(rollbackFor = Exception.class)
public Map<String, Object> issue(String type, long owner, long actor) {
require("APP".equals(type) || "NODE".equals(type), "凭证主体非法");
Map<String, Object> row = store.get(type.toLowerCase(Locale.ROOT), owner, true);
require(number(row, "user_id") == actor, "无主体权限");
String secret = RentalCrypto.random();
String encrypted = crypto.encrypt(secret);
String key = id("RK");
store.execute(
"UPDATE rent_credential SET status='VERIFY_ONLY',version=version+1,updated_at=?"
+ " WHERE owner_type=? AND owner_id=? AND status='ACTIVE'",
now(),
type,
owner);
long id =
store.insert(
"credential",
map(
"owner_type",
type,
"owner_id",
owner,
"api_key",
key,
"api_secret",
encrypted,
"master_key_version",
masterVersion,
"status",
"ACTIVE"));
store.event(type, owner, "ROTATE_CREDENTIAL", actor, "新Secret仅通过本次TLS响应展示,旧凭证仅核对在途");
return map("id", id, "api_key", key, "api_secret", secret);
}
@Transactional(rollbackFor = Exception.class)
public void revoke(long id, long actor) {
Map<String, Object> credential = store.get("credential", id, true);
Map<String, Object> owner =
store.get(
text(credential, "owner_type").toLowerCase(Locale.ROOT),
number(credential, "owner_id"),
false);
require(number(owner, "user_id") == actor, "无主体权限");
store.update("credential", id, map("status", "REVOKED", "revoked_at", now()));
store.event(
"APP".equals(text(credential, "owner_type")) ? "APP" : "NODE",
number(credential, "owner_id"),
"REVOKE_CREDENTIAL",
actor,
"主动吊销凭证");
}
}