feat: migrate java control plane foundation
This commit is contained in:
1 parent
1615401185
commit
9da175c040
24 files changed
+1555
-12
No files matched your search
@@ -7,4 +7,6 @@ control-plane/node_modules/
|
||||
control-plane/dist/
|
||||
control-plane/.env
|
||||
infra/control-plane/.env
|
||||
infra/moyi/.env
|
||||
infra/moyi/.env.cloud
|
||||
prototype/.env
|
||||
@@ -302,16 +302,17 @@ MaaS 模型服务与应用开发
|
||||
- [D4 密钥与模型目录实施计划](docs/06-d4-integration-plan.md)
|
||||
- [首批模型供应商接入节点](docs/07-provider-integration.md)
|
||||
- [Java 后端与容器化部署基线](docs/08-java-container-deployment.md)
|
||||
- [Java Control Plane 节点一:基础控制面](docs/09-java-control-plane-node.md)
|
||||
|
||||
## 前端原型
|
||||
|
||||
阶段 2 的可交互 Vue 原型位于 `prototype/`,包含用户控制台、模型广场、Playground、API Key、任务中心、用量与账单、接入文档和运营后台。
|
||||
|
||||
Java 后端迁移基线位于 `control-plane-java/`,前后端生产容器编排位于 `infra/moyi/`。现有 TypeScript Control Plane 在 Java 接口迁移完成前继续作为过渡实现。
|
||||
Java 后端位于 `control-plane-java/`,前后端生产容器编排位于 `infra/moyi/`。Java 当前已覆盖认证、模型目录、项目、API Key、模型权限和最小 OpenAI 兼容接口;真实供应商、队列和计费完成前,TypeScript Control Plane 继续作为过渡实现。
|
||||
|
||||
Otari 本地网关基座位于 `infra/otari/`,仅用于后续联调和开发环境,不包含真实供应商密钥。
|
||||
|
||||
身份与权限服务位于 `control-plane/`,本地 PostgreSQL 基座位于 `infra/control-plane/`;认证接口、邀请码和角色守卫将在 D3 逐步接入前端。
|
||||
身份与权限服务的 TypeScript 参考实现位于 `control-plane/`,本地 PostgreSQL 基座位于 `infra/control-plane/`;Java 迁移使用同一套基础表和 Flyway 脚本。
|
||||
|
||||
```bash
|
||||
cd prototype
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# MOYI Java Control Plane
|
||||
|
||||
这是 MOYI Control Plane 的 Java 迁移基线,目标运行时为 Java 21,框架为 Spring Boot。当前提供健康检查、Flyway 数据库迁移和容器启动骨架,后续按接口契约逐步迁移现有 TypeScript 服务的认证、模型目录、API Key、路由和任务能力。
|
||||
这是 MOYI Control Plane 的 Java 版本,目标运行时为 Java 21,框架为 Spring Boot。当前已提供健康检查、Flyway 数据库迁移、邀请码认证、用户会话、供应商/模型目录、项目、API Key、模型权限和最小 OpenAI 兼容接口;真实供应商适配、异步任务队列和计费仍按后续节点迁移。
|
||||
|
||||
目标依赖边界:
|
||||
|
||||
@@ -11,4 +11,4 @@
|
||||
|
||||
Flyway 脚本位于 `src/main/resources/db/migration/`,与现有 TypeScript 服务使用同一套基础表命名,后续迁移数据时可以按表逐步切换。
|
||||
|
||||
本目录的镜像由 `infra/moyi/docker-compose.yml` 构建。当前 TypeScript Control Plane 仍是迁移期间的可用实现,不要在 Java 接口尚未完成前直接切换生产流量。
|
||||
本目录的镜像由 `infra/moyi/docker-compose.yml` 构建。切换生产流量前仍需完成真实供应商适配、Redis 限流/队列和完整集成测试;TypeScript Control Plane 继续作为迁移期间的参考实现。
|
||||
@@ -40,6 +40,10 @@
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-redis</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-crypto</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
|
||||
@@ -11,7 +11,11 @@ public class HealthController {
|
||||
return Map.of(
|
||||
"status", "ok",
|
||||
"service", "moyi-control-plane-java",
|
||||
"migration", "bootstrap"
|
||||
"migration", "catalog",
|
||||
"capabilities", Map.of(
|
||||
"verificationCode", false,
|
||||
"passwordReset", false
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class AdminBootstrap implements ApplicationRunner {
|
||||
private final AuthService authService;
|
||||
private final Environment environment;
|
||||
private final String account;
|
||||
private final String password;
|
||||
private final String displayName;
|
||||
|
||||
public AdminBootstrap(
|
||||
AuthService authService,
|
||||
Environment environment,
|
||||
@Value("${moyi.bootstrap.admin-account}") String account,
|
||||
@Value("${moyi.bootstrap.admin-password}") String password,
|
||||
@Value("${moyi.bootstrap.admin-display-name}") String displayName
|
||||
) {
|
||||
this.authService = authService;
|
||||
this.environment = environment;
|
||||
this.account = account;
|
||||
this.password = password;
|
||||
this.displayName = displayName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments arguments) {
|
||||
for (String profile : environment.getActiveProfiles()) {
|
||||
if ("production".equals(profile) && "admin123".equals(password)) {
|
||||
throw new IllegalStateException("ADMIN_PASSWORD must be changed in production");
|
||||
}
|
||||
}
|
||||
authService.ensureAdmin(account, password, displayName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
import jakarta.servlet.http.Cookie;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.time.Duration;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseCookie;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class AuthController {
|
||||
private static final String SESSION_COOKIE = "moyi_session";
|
||||
private final AuthService authService;
|
||||
private final int sessionTtlHours;
|
||||
private final boolean secureCookie;
|
||||
|
||||
public AuthController(
|
||||
AuthService authService,
|
||||
@Value("${moyi.auth.session-ttl-hours}") int sessionTtlHours,
|
||||
@Value("${moyi.auth.secure-cookie}") boolean secureCookie
|
||||
) {
|
||||
this.authService = authService;
|
||||
this.sessionTtlHours = sessionTtlHours;
|
||||
this.secureCookie = secureCookie;
|
||||
}
|
||||
|
||||
public record LoginRequest(String account, String password) {}
|
||||
|
||||
public record RegisterRequest(String invitationCode, String account, String password, String displayName) {}
|
||||
|
||||
public record InvitationRequest(String label, Integer maxUses, OffsetDateTime expiresAt) {}
|
||||
|
||||
@PostMapping("/auth/login")
|
||||
public Map<String, Object> login(
|
||||
@RequestBody LoginRequest input,
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response
|
||||
) {
|
||||
AuthService.AuthResult result = authService.login(input.account(), input.password(), meta(request));
|
||||
response.addHeader(HttpHeaders.SET_COOKIE, sessionCookie(result.token(), Duration.ofHours(sessionTtlHours)).toString());
|
||||
return Map.of("user", result.user());
|
||||
}
|
||||
|
||||
@PostMapping("/auth/register")
|
||||
public ResponseEntity<Map<String, Object>> register(
|
||||
@RequestBody RegisterRequest input,
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response
|
||||
) {
|
||||
AuthService.AuthResult result = authService.register(
|
||||
input.invitationCode(), input.account(), input.password(), input.displayName(), meta(request)
|
||||
);
|
||||
response.addHeader(HttpHeaders.SET_COOKIE, sessionCookie(result.token(), Duration.ofHours(sessionTtlHours)).toString());
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(Map.of("user", result.user()));
|
||||
}
|
||||
|
||||
@PostMapping("/auth/logout")
|
||||
public ResponseEntity<Void> logout(HttpServletRequest request, HttpServletResponse response) {
|
||||
authService.logout(sessionToken(request), meta(request));
|
||||
response.addHeader(HttpHeaders.SET_COOKIE, sessionCookie("", Duration.ZERO).toString());
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@GetMapping("/auth/me")
|
||||
public Map<String, Object> currentUser(HttpServletRequest request) {
|
||||
return Map.of("user", authService.requireUser(sessionToken(request)));
|
||||
}
|
||||
|
||||
@GetMapping("/admin/users")
|
||||
public Map<String, Object> users(HttpServletRequest request) {
|
||||
return Map.of("users", authService.listUsers(sessionToken(request)));
|
||||
}
|
||||
|
||||
@GetMapping("/admin/invitations")
|
||||
public Map<String, Object> invitations(HttpServletRequest request) {
|
||||
return Map.of("invitations", authService.listInvitations(sessionToken(request)));
|
||||
}
|
||||
|
||||
@PostMapping("/admin/invitations")
|
||||
public ResponseEntity<Map<String, Object>> createInvitation(
|
||||
@RequestBody InvitationRequest input,
|
||||
HttpServletRequest request
|
||||
) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(authService.createInvitation(
|
||||
sessionToken(request), input.label(), input.maxUses(), input.expiresAt(), meta(request)
|
||||
));
|
||||
}
|
||||
|
||||
@PostMapping("/admin/invitations/{id}/revoke")
|
||||
public ResponseEntity<Void> revokeInvitation(@PathVariable UUID id, HttpServletRequest request) {
|
||||
authService.revokeInvitation(sessionToken(request), id, meta(request));
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@GetMapping("/admin/audit")
|
||||
public Map<String, Object> audit(
|
||||
@RequestParam(defaultValue = "100") int limit,
|
||||
HttpServletRequest request
|
||||
) {
|
||||
return Map.of("events", authService.listAudit(sessionToken(request), limit));
|
||||
}
|
||||
|
||||
private ResponseCookie sessionCookie(String value, Duration maxAge) {
|
||||
return ResponseCookie.from(SESSION_COOKIE, value)
|
||||
.httpOnly(true)
|
||||
.secure(secureCookie)
|
||||
.sameSite("Lax")
|
||||
.path("/")
|
||||
.maxAge(maxAge)
|
||||
.build();
|
||||
}
|
||||
|
||||
public static String sessionToken(HttpServletRequest request) {
|
||||
Cookie[] cookies = request.getCookies();
|
||||
if (cookies == null) return null;
|
||||
for (Cookie cookie : cookies) {
|
||||
if (SESSION_COOKIE.equals(cookie.getName())) return cookie.getValue();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static AuthService.RequestMeta meta(HttpServletRequest request) {
|
||||
return new AuthService.RequestMeta(request.getRemoteAddr(), request.getHeader(HttpHeaders.USER_AGENT));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,407 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class AuthService {
|
||||
private static final Pattern EMAIL = Pattern.compile("^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$");
|
||||
private static final Pattern PHONE = Pattern.compile("^\\+?[0-9][0-9\\s-]{6,19}$");
|
||||
private final JdbcTemplate jdbc;
|
||||
private final PasswordHasher passwordHasher;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final int sessionTtlHours;
|
||||
|
||||
public AuthService(
|
||||
JdbcTemplate jdbc,
|
||||
PasswordHasher passwordHasher,
|
||||
ObjectMapper objectMapper,
|
||||
@Value("${moyi.auth.session-ttl-hours}") int sessionTtlHours
|
||||
) {
|
||||
this.jdbc = jdbc;
|
||||
this.passwordHasher = passwordHasher;
|
||||
this.objectMapper = objectMapper;
|
||||
this.sessionTtlHours = sessionTtlHours;
|
||||
}
|
||||
|
||||
public record UserView(
|
||||
UUID id,
|
||||
String account,
|
||||
String accountKind,
|
||||
String displayName,
|
||||
String role,
|
||||
String status,
|
||||
OffsetDateTime createdAt,
|
||||
OffsetDateTime lastLoginAt
|
||||
) {}
|
||||
|
||||
public record AuthResult(String token, UserView user) {}
|
||||
|
||||
public record RequestMeta(String ipAddress, String userAgent) {}
|
||||
|
||||
private record UserRow(
|
||||
UUID id,
|
||||
String account,
|
||||
String accountKind,
|
||||
String passwordHash,
|
||||
String displayName,
|
||||
String role,
|
||||
String status,
|
||||
OffsetDateTime createdAt,
|
||||
OffsetDateTime lastLoginAt
|
||||
) {}
|
||||
|
||||
@Transactional
|
||||
public void ensureAdmin(String accountInput, String password, String displayName) {
|
||||
String account = normalizeAccount(accountInput);
|
||||
if (account.isBlank() || password.isBlank()) {
|
||||
throw new ApiException("INVALID_ADMIN_CONFIG", "管理员初始配置无效", 500);
|
||||
}
|
||||
if (findUserByAccount(account) != null) {
|
||||
return;
|
||||
}
|
||||
OffsetDateTime now = now();
|
||||
jdbc.update(
|
||||
"INSERT INTO users (id, account, account_kind, password_hash, display_name, role, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, 'admin', 'active', ?, ?)",
|
||||
UUID.randomUUID(), account, accountKind(account), passwordHasher.hash(password),
|
||||
displayName == null || displayName.isBlank() ? "平台管理员" : displayName.trim(), now, now
|
||||
);
|
||||
}
|
||||
|
||||
public AuthResult login(String accountInput, String password, RequestMeta meta) {
|
||||
UserRow user = findUserByAccount(normalizeAccount(accountInput));
|
||||
if (user == null || !passwordHasher.matches(password, user.passwordHash())) {
|
||||
throw new ApiException("INVALID_CREDENTIALS", "账号或密码错误", 401);
|
||||
}
|
||||
if (!"active".equals(user.status())) {
|
||||
throw new ApiException("ACCOUNT_DISABLED", "账号已停用", 403);
|
||||
}
|
||||
OffsetDateTime loginAt = now();
|
||||
jdbc.update("UPDATE users SET last_login_at = ?, updated_at = ? WHERE id = ?", loginAt, loginAt, user.id());
|
||||
audit("user.logged_in", user.id(), meta, Map.of());
|
||||
return new AuthResult(createSession(user.id(), meta), toView(user, loginAt));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public AuthResult register(
|
||||
String invitationCode,
|
||||
String accountInput,
|
||||
String password,
|
||||
String displayName,
|
||||
RequestMeta meta
|
||||
) {
|
||||
String account = normalizeAccount(accountInput);
|
||||
validatePublicAccount(account);
|
||||
if (password == null || password.length() < 8) {
|
||||
throw new ApiException("WEAK_PASSWORD", "密码至少需要 8 位");
|
||||
}
|
||||
if (invitationCode == null || invitationCode.isBlank()) {
|
||||
throw new ApiException("INVITATION_REQUIRED", "第一版注册需要邀请码");
|
||||
}
|
||||
if (findUserByAccount(account) != null) {
|
||||
throw new ApiException("ACCOUNT_EXISTS", "账号已注册", 409);
|
||||
}
|
||||
List<Map<String, Object>> invitations = jdbc.queryForList(
|
||||
"SELECT id, max_uses, used_count, expires_at, disabled_at FROM invitation_codes WHERE code_hash = ? FOR UPDATE",
|
||||
hashSecret(invitationCode.trim())
|
||||
);
|
||||
if (invitations.isEmpty()) {
|
||||
throw new ApiException("INVITATION_INVALID", "邀请码无效或已失效", 403);
|
||||
}
|
||||
Map<String, Object> invitation = invitations.getFirst();
|
||||
OffsetDateTime now = now();
|
||||
OffsetDateTime expiresAt = toOffsetDateTime(invitation.get("expires_at"));
|
||||
if (invitation.get("disabled_at") != null
|
||||
|| (expiresAt != null && !expiresAt.isAfter(now))
|
||||
|| ((Number) invitation.get("used_count")).intValue() >= ((Number) invitation.get("max_uses")).intValue()) {
|
||||
throw new ApiException("INVITATION_EXHAUSTED", "邀请码无效、已用尽或账号已注册", 403);
|
||||
}
|
||||
UUID userId = UUID.randomUUID();
|
||||
String name = displayName == null || displayName.isBlank() ? defaultDisplayName(account) : displayName.trim();
|
||||
jdbc.update(
|
||||
"INSERT INTO users (id, account, account_kind, password_hash, display_name, role, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, 'user', 'active', ?, ?)",
|
||||
userId, account, accountKind(account), passwordHasher.hash(password), name, now, now
|
||||
);
|
||||
UUID invitationId = (UUID) invitation.get("id");
|
||||
jdbc.update("UPDATE invitation_codes SET used_count = used_count + 1 WHERE id = ?", invitationId);
|
||||
jdbc.update("INSERT INTO invitation_redemptions (invitation_id, user_id, redeemed_at) VALUES (?, ?, ?)", invitationId, userId, now);
|
||||
UserRow user = findUserById(userId);
|
||||
audit("user.registered", userId, meta, Map.of("invitationId", invitationId));
|
||||
return new AuthResult(createSession(userId, meta), toView(user, null));
|
||||
}
|
||||
|
||||
public UserView requireUser(String token) {
|
||||
return toView(requireUserRow(token), null);
|
||||
}
|
||||
|
||||
public UserView requireAdmin(String token) {
|
||||
UserRow user = requireUserRow(token);
|
||||
if (!"admin".equals(user.role())) {
|
||||
throw new ApiException("FORBIDDEN", "需要管理员权限", 403);
|
||||
}
|
||||
return toView(user, null);
|
||||
}
|
||||
|
||||
public void logout(String token, RequestMeta meta) {
|
||||
if (token == null || token.isBlank()) {
|
||||
return;
|
||||
}
|
||||
List<Map<String, Object>> sessions = jdbc.queryForList(
|
||||
"SELECT id, user_id FROM auth_sessions WHERE token_hash = ? AND revoked_at IS NULL",
|
||||
hashSecret(token)
|
||||
);
|
||||
if (sessions.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
Map<String, Object> session = sessions.getFirst();
|
||||
jdbc.update("UPDATE auth_sessions SET revoked_at = ? WHERE id = ?", now(), session.get("id"));
|
||||
audit("user.logged_out", (UUID) session.get("user_id"), meta, Map.of());
|
||||
}
|
||||
|
||||
public List<UserView> listUsers(String token) {
|
||||
requireAdmin(token);
|
||||
return jdbc.query("SELECT * FROM users ORDER BY created_at DESC", this::mapUser).stream().map(user -> toView(user, null)).toList();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> createInvitation(
|
||||
String token,
|
||||
String label,
|
||||
Integer maxUses,
|
||||
OffsetDateTime expiresAt,
|
||||
RequestMeta meta
|
||||
) {
|
||||
UserView admin = requireAdmin(token);
|
||||
String code = "moyi_" + randomToken(12);
|
||||
UUID id = UUID.randomUUID();
|
||||
OffsetDateTime createdAt = now();
|
||||
int allowedUses = maxUses == null ? 1 : Math.max(1, maxUses);
|
||||
jdbc.update(
|
||||
"INSERT INTO invitation_codes (id, code_hash, code_prefix, label, max_uses, used_count, expires_at, created_by, created_at) VALUES (?, ?, ?, ?, ?, 0, ?, ?, ?)",
|
||||
id, hashSecret(code), code.substring(0, Math.min(code.length(), 10)),
|
||||
label == null || label.isBlank() ? "内测邀请码" : label.trim(), allowedUses, expiresAt, admin.id(), createdAt
|
||||
);
|
||||
audit("invitation.created", admin.id(), meta, Map.of("invitationId", id));
|
||||
return Map.of(
|
||||
"code", code,
|
||||
"invitation", invitationView(id, code.substring(0, Math.min(code.length(), 10)),
|
||||
label == null || label.isBlank() ? "内测邀请码" : label.trim(), allowedUses, 0,
|
||||
expiresAt, null, admin.id(), createdAt)
|
||||
);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> listInvitations(String token) {
|
||||
requireAdmin(token);
|
||||
return jdbc.query(
|
||||
"SELECT id, code_prefix, label, max_uses, used_count, expires_at, disabled_at, created_by, created_at FROM invitation_codes ORDER BY created_at DESC",
|
||||
(resultSet, rowNumber) -> invitationView(
|
||||
resultSet.getObject("id", UUID.class), resultSet.getString("code_prefix"), resultSet.getString("label"),
|
||||
resultSet.getInt("max_uses"), resultSet.getInt("used_count"),
|
||||
resultSet.getObject("expires_at", OffsetDateTime.class), resultSet.getObject("disabled_at", OffsetDateTime.class),
|
||||
resultSet.getObject("created_by", UUID.class), resultSet.getObject("created_at", OffsetDateTime.class)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public void revokeInvitation(String token, UUID invitationId, RequestMeta meta) {
|
||||
UserView admin = requireAdmin(token);
|
||||
int changed = jdbc.update(
|
||||
"UPDATE invitation_codes SET disabled_at = ? WHERE id = ? AND disabled_at IS NULL",
|
||||
now(), invitationId
|
||||
);
|
||||
if (changed == 0) {
|
||||
throw new ApiException("INVITATION_NOT_FOUND", "邀请码不存在或已停用", 404);
|
||||
}
|
||||
audit("invitation.revoked", admin.id(), meta, Map.of("invitationId", invitationId));
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> listAudit(String token, int requestedLimit) {
|
||||
requireAdmin(token);
|
||||
int limit = Math.min(Math.max(requestedLimit, 1), 200);
|
||||
return jdbc.query(
|
||||
"SELECT id, actor_user_id, event_type, target_type, target_id, ip_address, user_agent, details, created_at FROM auth_audit_log ORDER BY created_at DESC LIMIT ?",
|
||||
(resultSet, rowNumber) -> {
|
||||
Map<String, Object> event = new java.util.LinkedHashMap<>();
|
||||
event.put("id", String.valueOf(resultSet.getLong("id")));
|
||||
UUID actorUserId = resultSet.getObject("actor_user_id", UUID.class);
|
||||
if (actorUserId != null) event.put("actorUserId", actorUserId);
|
||||
event.put("eventType", resultSet.getString("event_type"));
|
||||
String targetType = resultSet.getString("target_type");
|
||||
String targetId = resultSet.getString("target_id");
|
||||
String ipAddress = resultSet.getString("ip_address");
|
||||
if (targetType != null) event.put("targetType", targetType);
|
||||
if (targetId != null) event.put("targetId", targetId);
|
||||
if (ipAddress != null) event.put("ipAddress", ipAddress);
|
||||
event.put("details", readJson(resultSet.getString("details")));
|
||||
event.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class));
|
||||
return event;
|
||||
},
|
||||
limit
|
||||
);
|
||||
}
|
||||
|
||||
public void audit(String eventType, UUID actorUserId, RequestMeta meta, Map<String, Object> details) {
|
||||
jdbc.update(
|
||||
"INSERT INTO auth_audit_log (actor_user_id, event_type, ip_address, user_agent, details) VALUES (?, ?, CAST(? AS inet), ?, CAST(? AS jsonb))",
|
||||
actorUserId, eventType, blankToNull(meta.ipAddress()), blankToNull(meta.userAgent()), writeJson(details)
|
||||
);
|
||||
}
|
||||
|
||||
private UserRow requireUserRow(String token) {
|
||||
if (token == null || token.isBlank()) {
|
||||
throw new ApiException("UNAUTHENTICATED", "请先登录", 401);
|
||||
}
|
||||
List<UserRow> users = jdbc.query(
|
||||
"SELECT u.* FROM auth_sessions s JOIN users u ON u.id = s.user_id WHERE s.token_hash = ? AND s.revoked_at IS NULL AND s.expires_at > now() AND u.status = 'active'",
|
||||
this::mapUser,
|
||||
hashSecret(token)
|
||||
);
|
||||
if (users.isEmpty()) {
|
||||
throw new ApiException("UNAUTHENTICATED", "请先登录", 401);
|
||||
}
|
||||
return users.getFirst();
|
||||
}
|
||||
|
||||
private String createSession(UUID userId, RequestMeta meta) {
|
||||
String token = randomToken(32);
|
||||
OffsetDateTime createdAt = now();
|
||||
jdbc.update(
|
||||
"INSERT INTO auth_sessions (id, token_hash, user_id, expires_at, created_at, last_seen_at, user_agent, ip_address) VALUES (?, ?, ?, ?, ?, ?, ?, CAST(? AS inet))",
|
||||
UUID.randomUUID(), hashSecret(token), userId, createdAt.plusHours(sessionTtlHours), createdAt, createdAt,
|
||||
blankToNull(meta.userAgent()), blankToNull(meta.ipAddress())
|
||||
);
|
||||
return token;
|
||||
}
|
||||
|
||||
private UserRow findUserByAccount(String account) {
|
||||
try {
|
||||
return jdbc.queryForObject("SELECT * FROM users WHERE account = ?", this::mapUser, account);
|
||||
} catch (EmptyResultDataAccessException exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private UserRow findUserById(UUID id) {
|
||||
return jdbc.queryForObject("SELECT * FROM users WHERE id = ?", this::mapUser, id);
|
||||
}
|
||||
|
||||
private UserRow mapUser(ResultSet resultSet, int rowNumber) throws SQLException {
|
||||
return new UserRow(
|
||||
resultSet.getObject("id", UUID.class),
|
||||
resultSet.getString("account"),
|
||||
resultSet.getString("account_kind"),
|
||||
resultSet.getString("password_hash"),
|
||||
resultSet.getString("display_name"),
|
||||
resultSet.getString("role"),
|
||||
resultSet.getString("status"),
|
||||
resultSet.getObject("created_at", OffsetDateTime.class),
|
||||
resultSet.getObject("last_login_at", OffsetDateTime.class)
|
||||
);
|
||||
}
|
||||
|
||||
private UserView toView(UserRow user, OffsetDateTime lastLoginOverride) {
|
||||
return new UserView(
|
||||
user.id(), user.account(), user.accountKind(), user.displayName(), user.role(), user.status(),
|
||||
user.createdAt(), lastLoginOverride == null ? user.lastLoginAt() : lastLoginOverride
|
||||
);
|
||||
}
|
||||
|
||||
private Map<String, Object> invitationView(
|
||||
UUID id, String prefix, String label, int maxUses, int usedCount, OffsetDateTime expiresAt,
|
||||
OffsetDateTime disabledAt, UUID createdBy, OffsetDateTime createdAt
|
||||
) {
|
||||
Map<String, Object> result = new java.util.LinkedHashMap<>();
|
||||
result.put("id", id);
|
||||
result.put("codePrefix", prefix);
|
||||
result.put("label", label);
|
||||
result.put("maxUses", maxUses);
|
||||
result.put("usedCount", usedCount);
|
||||
if (expiresAt != null) result.put("expiresAt", expiresAt);
|
||||
if (disabledAt != null) result.put("disabledAt", disabledAt);
|
||||
result.put("createdBy", createdBy);
|
||||
result.put("createdAt", createdAt);
|
||||
return result;
|
||||
}
|
||||
|
||||
private Map<String, Object> readJson(String value) {
|
||||
try {
|
||||
return objectMapper.readValue(value, new TypeReference<>() {});
|
||||
} catch (JsonProcessingException exception) {
|
||||
return Map.of();
|
||||
}
|
||||
}
|
||||
|
||||
private String writeJson(Map<String, Object> value) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(value);
|
||||
} catch (JsonProcessingException exception) {
|
||||
throw new ApiException("JSON_ENCODING_FAILED", "无法记录审计信息", 500);
|
||||
}
|
||||
}
|
||||
|
||||
private void validatePublicAccount(String account) {
|
||||
if (!EMAIL.matcher(account).matches() && !PHONE.matcher(account).matches()) {
|
||||
throw new ApiException("INVALID_ACCOUNT", "请输入有效的邮箱或手机号");
|
||||
}
|
||||
}
|
||||
|
||||
private String normalizeAccount(String account) {
|
||||
String normalized = account == null ? "" : account.trim().toLowerCase();
|
||||
return normalized.contains("@") ? normalized : normalized.replaceAll("[\\s-]", "");
|
||||
}
|
||||
|
||||
private String accountKind(String account) {
|
||||
if (account.contains("@")) return "email";
|
||||
return PHONE.matcher(account).matches() ? "phone" : "username";
|
||||
}
|
||||
|
||||
private String defaultDisplayName(String account) {
|
||||
int separator = account.indexOf('@');
|
||||
return separator > 0 ? account.substring(0, separator) : "MOYI 用户";
|
||||
}
|
||||
|
||||
private String randomToken(int byteCount) {
|
||||
byte[] bytes = new byte[byteCount];
|
||||
new java.security.SecureRandom().nextBytes(bytes);
|
||||
return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
|
||||
}
|
||||
|
||||
public static String hashSecret(String value) {
|
||||
try {
|
||||
return java.util.HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8)));
|
||||
} catch (NoSuchAlgorithmException exception) {
|
||||
throw new IllegalStateException(exception);
|
||||
}
|
||||
}
|
||||
|
||||
private OffsetDateTime now() {
|
||||
return OffsetDateTime.now(ZoneOffset.UTC);
|
||||
}
|
||||
|
||||
private OffsetDateTime toOffsetDateTime(Object value) {
|
||||
return value instanceof OffsetDateTime dateTime ? dateTime : null;
|
||||
}
|
||||
|
||||
private String blankToNull(String value) {
|
||||
return value == null || value.isBlank() ? null : value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Base64;
|
||||
import javax.crypto.SecretKeyFactory;
|
||||
import javax.crypto.spec.PBEKeySpec;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class PasswordHasher {
|
||||
private static final int ITERATIONS = 210_000;
|
||||
private static final int KEY_LENGTH = 256;
|
||||
private final SecureRandom secureRandom = new SecureRandom();
|
||||
private final String pepper;
|
||||
|
||||
public PasswordHasher(@Value("${moyi.auth.password-pepper}") String pepper) {
|
||||
this.pepper = pepper;
|
||||
}
|
||||
|
||||
public String hash(String password) {
|
||||
byte[] salt = new byte[16];
|
||||
secureRandom.nextBytes(salt);
|
||||
byte[] result = derive(password, salt, ITERATIONS, KEY_LENGTH);
|
||||
return "pbkdf2$" + ITERATIONS + "$" + Base64.getUrlEncoder().withoutPadding().encodeToString(salt)
|
||||
+ "$" + Base64.getUrlEncoder().withoutPadding().encodeToString(result);
|
||||
}
|
||||
|
||||
public boolean matches(String password, String encoded) {
|
||||
try {
|
||||
String[] parts = encoded.split("\\$");
|
||||
if (parts.length != 4 || !"pbkdf2".equals(parts[0])) {
|
||||
return false;
|
||||
}
|
||||
int iterations = Integer.parseInt(parts[1]);
|
||||
byte[] salt = Base64.getUrlDecoder().decode(parts[2]);
|
||||
byte[] expected = Base64.getUrlDecoder().decode(parts[3]);
|
||||
byte[] actual = derive(password, salt, iterations, expected.length * 8);
|
||||
return MessageDigest.isEqual(expected, actual);
|
||||
} catch (RuntimeException exception) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] derive(String password, byte[] salt, int iterations, int keyLength) {
|
||||
char[] value = (password + pepper).toCharArray();
|
||||
PBEKeySpec specification = new PBEKeySpec(value, salt, iterations, keyLength);
|
||||
try {
|
||||
return SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256").generateSecret(specification).getEncoded();
|
||||
} catch (Exception exception) {
|
||||
throw new IllegalStateException("Password hashing is unavailable", exception);
|
||||
} finally {
|
||||
specification.clearPassword();
|
||||
ArraysSupport.clear(value);
|
||||
}
|
||||
}
|
||||
|
||||
private static final class ArraysSupport {
|
||||
private ArraysSupport() {
|
||||
}
|
||||
|
||||
static void clear(char[] value) {
|
||||
java.util.Arrays.fill(value, '\0');
|
||||
}
|
||||
}
|
||||
}
|
||||
+118
@@ -0,0 +1,118 @@
|
||||
package com.moyi.controlplane.catalog;
|
||||
|
||||
import com.moyi.controlplane.auth.AuthController;
|
||||
import com.moyi.controlplane.auth.AuthService;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api")
|
||||
public class CatalogController {
|
||||
private final CatalogService catalogService;
|
||||
private final AuthService authService;
|
||||
|
||||
public CatalogController(CatalogService catalogService, AuthService authService) {
|
||||
this.catalogService = catalogService;
|
||||
this.authService = authService;
|
||||
}
|
||||
|
||||
@GetMapping("/models")
|
||||
public Map<String, Object> models(@jakarta.servlet.http.HttpServletRequest request) {
|
||||
authService.requireUser(AuthController.sessionToken(request));
|
||||
return Map.of("providers", catalogService.listProviders(false), "models", catalogService.listModels(false));
|
||||
}
|
||||
|
||||
@GetMapping("/overview")
|
||||
public Map<String, Object> overview(@jakarta.servlet.http.HttpServletRequest request) {
|
||||
return catalogService.overview(AuthController.sessionToken(request), authService);
|
||||
}
|
||||
|
||||
@GetMapping("/projects")
|
||||
public Map<String, Object> projects(@jakarta.servlet.http.HttpServletRequest request) {
|
||||
return Map.of("projects", catalogService.listProjects(AuthController.sessionToken(request), authService));
|
||||
}
|
||||
|
||||
@PostMapping("/projects")
|
||||
public ResponseEntity<Map<String, Object>> createProject(
|
||||
@RequestBody Map<String, Object> input,
|
||||
jakarta.servlet.http.HttpServletRequest request
|
||||
) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(Map.of("project", catalogService.createProject(
|
||||
AuthController.sessionToken(request), input, authService
|
||||
)));
|
||||
}
|
||||
|
||||
@GetMapping("/keys")
|
||||
public Map<String, Object> keys(jakarta.servlet.http.HttpServletRequest request) {
|
||||
return Map.of("keys", catalogService.listKeys(AuthController.sessionToken(request), authService));
|
||||
}
|
||||
|
||||
@PostMapping("/keys")
|
||||
public ResponseEntity<Map<String, Object>> createKey(
|
||||
@RequestBody Map<String, Object> input,
|
||||
jakarta.servlet.http.HttpServletRequest request
|
||||
) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(catalogService.createKey(
|
||||
AuthController.sessionToken(request), input, authService
|
||||
));
|
||||
}
|
||||
|
||||
@PostMapping("/keys/{id}/models")
|
||||
public Map<String, Object> updateKeyModels(
|
||||
@PathVariable String id,
|
||||
@RequestBody Map<String, Object> input,
|
||||
jakarta.servlet.http.HttpServletRequest request
|
||||
) {
|
||||
return catalogService.updateKeyModels(
|
||||
AuthController.sessionToken(request), id, input.get("modelIds") instanceof List<?> values
|
||||
? values.stream().map(String::valueOf).toList() : List.of(), authService
|
||||
);
|
||||
}
|
||||
|
||||
@PostMapping("/keys/{id}/revoke")
|
||||
public ResponseEntity<Void> revokeKey(@PathVariable String id, jakarta.servlet.http.HttpServletRequest request) {
|
||||
catalogService.revokeKey(AuthController.sessionToken(request), id, authService);
|
||||
return ResponseEntity.noContent().build();
|
||||
}
|
||||
|
||||
@GetMapping("/admin/providers")
|
||||
public Map<String, Object> adminProviders(jakarta.servlet.http.HttpServletRequest request) {
|
||||
authService.requireAdmin(AuthController.sessionToken(request));
|
||||
return Map.of("providers", catalogService.listProviders(true));
|
||||
}
|
||||
|
||||
@PostMapping("/admin/providers")
|
||||
public ResponseEntity<Map<String, Object>> createProvider(
|
||||
@RequestBody Map<String, Object> input,
|
||||
jakarta.servlet.http.HttpServletRequest request
|
||||
) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(Map.of("provider", catalogService.createProvider(
|
||||
input, AuthController.sessionToken(request), authService
|
||||
)));
|
||||
}
|
||||
|
||||
@GetMapping("/admin/models")
|
||||
public Map<String, Object> adminModels(jakarta.servlet.http.HttpServletRequest request) {
|
||||
authService.requireAdmin(AuthController.sessionToken(request));
|
||||
return Map.of("models", catalogService.listModels(true));
|
||||
}
|
||||
|
||||
@PostMapping("/admin/models")
|
||||
public ResponseEntity<Map<String, Object>> createModel(
|
||||
@RequestBody Map<String, Object> input,
|
||||
jakarta.servlet.http.HttpServletRequest request
|
||||
) {
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(Map.of("model", catalogService.createModel(
|
||||
input, AuthController.sessionToken(request), authService
|
||||
)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,372 @@
|
||||
package com.moyi.controlplane.catalog;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.auth.AuthService;
|
||||
import com.moyi.controlplane.auth.AuthService.UserView;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.SecureRandom;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class CatalogService {
|
||||
private static final List<String> MODEL_TYPES = List.of("text", "code", "multimodal", "image", "audio", "video", "embedding", "3d");
|
||||
private static final List<String> CALL_MODES = List.of("sync", "stream", "async");
|
||||
private static final List<String> PROTOCOLS = List.of("openai-compatible", "http", "sdk");
|
||||
private static final List<String> SOURCE_TYPES = List.of("official", "relay", "self-hosted", "custom");
|
||||
private final JdbcTemplate jdbc;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final SecureRandom secureRandom = new SecureRandom();
|
||||
|
||||
public CatalogService(JdbcTemplate jdbc, ObjectMapper objectMapper) {
|
||||
this.jdbc = jdbc;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> listProviders(boolean includeDisabled) {
|
||||
String statusClause = includeDisabled ? "" : " WHERE status = 'active'";
|
||||
return jdbc.query("SELECT * FROM model_providers" + statusClause + " ORDER BY name", this::mapProvider);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> listModels(boolean includeDisabled) {
|
||||
String statusClause = includeDisabled ? "" : " WHERE status = 'active'";
|
||||
return jdbc.query("SELECT * FROM model_catalog" + statusClause + " ORDER BY public_name", this::mapModel);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> createProvider(Map<String, Object> input, String token, AuthService auth) {
|
||||
UserView admin = auth.requireAdmin(token);
|
||||
String code = required(input, "code").toLowerCase();
|
||||
String name = required(input, "name");
|
||||
String protocol = optional(input, "protocol", "http");
|
||||
String sourceType = optional(input, "sourceType", "custom");
|
||||
if (!code.matches("^[a-z0-9][a-z0-9._-]{1,63}$") || !PROTOCOLS.contains(protocol) || !SOURCE_TYPES.contains(sourceType)) {
|
||||
throw new ApiException("INVALID_PROVIDER", "供应商标识、协议或来源类型无效");
|
||||
}
|
||||
String id = UUID.randomUUID().toString();
|
||||
String baseUrl = optional(input, "baseUrl", null);
|
||||
String description = optional(input, "description", "");
|
||||
String credentialType = optional(input, "credentialType", null);
|
||||
String secret = optional(input, "credentialSecret", null);
|
||||
String masked = secret == null ? null : mask(secret);
|
||||
jdbc.update(
|
||||
"INSERT INTO model_providers (id, code, name, protocol, source_type, base_url, description, credential_type, credential_ciphertext, credential_masked, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, 'active')",
|
||||
id, code, name, protocol, sourceType, baseUrl, description, credentialType, null, masked
|
||||
);
|
||||
auth.audit("provider.created", admin.id(), new AuthService.RequestMeta(null, null), Map.of("providerId", id));
|
||||
return findProvider(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> createModel(Map<String, Object> input, String token, AuthService auth) {
|
||||
UserView admin = auth.requireAdmin(token);
|
||||
String providerId = required(input, "providerId");
|
||||
if (findProvider(providerId) == null) throw new ApiException("PROVIDER_NOT_FOUND", "供应商不存在", 404);
|
||||
String publicName = required(input, "publicName");
|
||||
String upstreamModel = required(input, "upstreamModel");
|
||||
String type = required(input, "type");
|
||||
String mode = required(input, "mode");
|
||||
if (!MODEL_TYPES.contains(type) || !CALL_MODES.contains(mode)) throw new ApiException("INVALID_MODEL", "模型类型或调用模式无效");
|
||||
String id = UUID.randomUUID().toString();
|
||||
String modelKey = optional(input, "modelKey", null);
|
||||
String sourceLabel = optional(input, "sourceLabel", null);
|
||||
String description = optional(input, "description", "");
|
||||
String inputTypes = jsonArray(input.get("inputTypes"));
|
||||
String outputTypes = jsonArray(input.get("outputTypes"));
|
||||
String capabilities = jsonObject(input.get("capabilities"));
|
||||
String pricing = jsonObjectOrNull(input.get("pricing"));
|
||||
Integer priority = integerOrNull(input.get("priority"));
|
||||
Integer maxConcurrency = integerOrNull(input.get("maxConcurrency"));
|
||||
if (priority != null && priority < 0) throw new ApiException("INVALID_MODEL_PRIORITY", "模型路由优先级必须大于等于 0");
|
||||
if (maxConcurrency != null && maxConcurrency < 0) throw new ApiException("INVALID_MODEL_CONCURRENCY", "模型并发上限必须大于等于 0");
|
||||
jdbc.update(
|
||||
"INSERT INTO model_catalog (id, provider_id, model_key, source_label, public_name, upstream_model, model_type, call_mode, description, input_types, output_types, capabilities, pricing, priority, max_concurrency, status) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, CAST(? AS jsonb), CAST(? AS jsonb), CAST(? AS jsonb), CAST(? AS jsonb), ?, ?, 'active')",
|
||||
id, providerId, modelKey, sourceLabel, publicName, upstreamModel, type, mode, description,
|
||||
inputTypes, outputTypes, capabilities, pricing, priority, maxConcurrency
|
||||
);
|
||||
auth.audit("model.created", admin.id(), new AuthService.RequestMeta(null, null), Map.of("modelId", id));
|
||||
return findModel(id);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> listProjects(String token, AuthService auth) {
|
||||
UserView user = auth.requireUser(token);
|
||||
return jdbc.query("SELECT * FROM model_projects WHERE owner_user_id = ? AND status = 'active' ORDER BY created_at DESC", this::mapProject, user.id());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> createProject(String token, Map<String, Object> input, AuthService auth) {
|
||||
UserView user = auth.requireUser(token);
|
||||
String name = required(input, "name");
|
||||
if (name.length() > 80) throw new ApiException("INVALID_PROJECT_NAME", "项目名称不能超过 80 个字符");
|
||||
String id = "project_" + UUID.randomUUID().toString().replace("-", "").substring(0, 20);
|
||||
OffsetDateTime now = now();
|
||||
jdbc.update(
|
||||
"INSERT INTO model_projects (id, owner_user_id, name, description, status, created_at, updated_at) VALUES (?, ?, ?, ?, 'active', ?, ?)",
|
||||
id, user.id(), name, optional(input, "description", ""), now, now
|
||||
);
|
||||
return findProject(id);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> listKeys(String token, AuthService auth) {
|
||||
UserView user = auth.requireUser(token);
|
||||
return jdbc.query("SELECT * FROM platform_api_keys WHERE user_id = ? ORDER BY created_at DESC", this::mapApiKey, user.id());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> createKey(String token, Map<String, Object> input, AuthService auth) {
|
||||
UserView user = auth.requireUser(token);
|
||||
String name = optional(input, "name", "默认开发 Key");
|
||||
if (name.length() > 80) throw new ApiException("INVALID_KEY_NAME", "Key 名称不能超过 80 个字符");
|
||||
String projectId = optional(input, "projectId", null);
|
||||
if (projectId != null && !ownsProject(projectId, user.id())) throw new ApiException("PROJECT_NOT_FOUND", "项目不存在或无权访问", 404);
|
||||
List<String> scopes = stringList(input.get("scopes"));
|
||||
if (scopes.isEmpty()) scopes = List.of("invoke");
|
||||
List<String> modelIds = stringList(input.get("modelIds"));
|
||||
for (String modelId : modelIds) if (findActiveModel(modelId) == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
||||
String plaintext = "sk-moyi-" + randomToken(24);
|
||||
String id = UUID.randomUUID().toString();
|
||||
OffsetDateTime now = now();
|
||||
jdbc.update(
|
||||
"INSERT INTO platform_api_keys (id, user_id, project_id, name, key_prefix, key_hash, scopes, model_ids, status, created_at) VALUES (?, ?, ?, ?, ?, ?, CAST(? AS jsonb), CAST(? AS jsonb), 'active', ?)",
|
||||
id, user.id(), projectId, name, plaintext.substring(0, 16), sha256(plaintext), writeJson(scopes), writeJson(modelIds), now
|
||||
);
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("key", plaintext);
|
||||
result.put("apiKey", findKey(id));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> updateKeyModels(String token, String keyId, List<String> modelIds, AuthService auth) {
|
||||
UserView user = auth.requireUser(token);
|
||||
List<String> uniqueIds = modelIds == null ? List.of() : modelIds.stream().map(String::trim).filter(value -> !value.isBlank()).distinct().toList();
|
||||
for (String modelId : uniqueIds) if (findActiveModel(modelId) == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
||||
int changed = jdbc.update("UPDATE platform_api_keys SET model_ids = CAST(? AS jsonb) WHERE id = ? AND user_id = ? AND status = 'active'", writeJson(uniqueIds), UUID.fromString(keyId), user.id());
|
||||
if (changed == 0) throw new ApiException("KEY_NOT_FOUND", "Key 不存在、已停用或无权修改", 404);
|
||||
return Map.of("apiKey", findKey(keyId));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void revokeKey(String token, String keyId, AuthService auth) {
|
||||
UserView user = auth.requireUser(token);
|
||||
int changed = jdbc.update("UPDATE platform_api_keys SET status = 'disabled', revoked_at = ? WHERE id = ? AND user_id = ? AND status = 'active'", now(), UUID.fromString(keyId), user.id());
|
||||
if (changed == 0) throw new ApiException("KEY_NOT_FOUND", "Key 不存在或已停用", 404);
|
||||
}
|
||||
|
||||
public Map<String, Object> overview(String token, AuthService auth) {
|
||||
UserView user = auth.requireUser(token);
|
||||
Integer callsToday = jdbc.queryForObject("SELECT COUNT(*) FROM usage_events WHERE user_id = ? AND event_kind = 'call' AND created_at >= CURRENT_DATE", Integer.class, user.id());
|
||||
Integer callsMonth = jdbc.queryForObject("SELECT COUNT(*) FROM usage_events WHERE user_id = ? AND event_kind = 'call' AND created_at >= date_trunc('month', now())", Integer.class, user.id());
|
||||
Integer tasks = jdbc.queryForObject("SELECT COUNT(*) FROM usage_events WHERE user_id = ? AND event_kind = 'task'", Integer.class, user.id());
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("generatedAt", now());
|
||||
result.put("callsToday", callsToday == null ? 0 : callsToday);
|
||||
result.put("callsThisMonth", callsMonth == null ? 0 : callsMonth);
|
||||
result.put("asyncTasks", tasks == null ? 0 : tasks);
|
||||
result.put("tasks", Map.of("queued", 0, "running", 0, "succeeded", 0, "failed", 0));
|
||||
result.put("series", List.of());
|
||||
result.put("ledger", List.of());
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<String, Object> authenticateKey(String plaintext) {
|
||||
if (plaintext == null || plaintext.isBlank()) return null;
|
||||
try {
|
||||
return jdbc.queryForObject("SELECT * FROM platform_api_keys WHERE key_hash = ? AND status = 'active'", this::mapApiKey, sha256(plaintext.trim()));
|
||||
} catch (EmptyResultDataAccessException exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Object> activeModel(String id) {
|
||||
return findActiveModel(id);
|
||||
}
|
||||
|
||||
public boolean keyCanInvoke(Map<String, Object> key, String modelId) {
|
||||
if (key == null || !"active".equals(key.get("status"))) return false;
|
||||
Object scopes = key.get("scopes");
|
||||
Object modelIds = key.get("modelIds");
|
||||
return scopes instanceof List<?> scopeList && scopeList.contains("invoke")
|
||||
&& modelIds instanceof List<?> modelList && modelList.contains(modelId);
|
||||
}
|
||||
|
||||
public void touchKey(Map<String, Object> key) {
|
||||
if (key != null && key.get("id") != null) jdbc.update("UPDATE platform_api_keys SET last_used_at = ? WHERE id = ?", now(), UUID.fromString(String.valueOf(key.get("id"))));
|
||||
}
|
||||
|
||||
public String providerIdForModel(String modelId) {
|
||||
try {
|
||||
return jdbc.queryForObject("SELECT provider_id FROM model_catalog WHERE id = ? AND status = 'active'", String.class, modelId);
|
||||
} catch (EmptyResultDataAccessException exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Object> findProvider(String id) {
|
||||
try { return jdbc.queryForObject("SELECT * FROM model_providers WHERE id = ?", this::mapProvider, id); }
|
||||
catch (EmptyResultDataAccessException exception) { return null; }
|
||||
}
|
||||
|
||||
private Map<String, Object> findModel(String id) {
|
||||
try { return jdbc.queryForObject("SELECT * FROM model_catalog WHERE id = ?", this::mapModel, id); }
|
||||
catch (EmptyResultDataAccessException exception) { return null; }
|
||||
}
|
||||
|
||||
private Map<String, Object> findActiveModel(String id) {
|
||||
try { return jdbc.queryForObject("SELECT * FROM model_catalog WHERE id = ? AND status = 'active'", this::mapModel, id); }
|
||||
catch (EmptyResultDataAccessException exception) { return null; }
|
||||
}
|
||||
|
||||
private Map<String, Object> findProject(String id) {
|
||||
try { return jdbc.queryForObject("SELECT * FROM model_projects WHERE id = ?", this::mapProject, id); }
|
||||
catch (EmptyResultDataAccessException exception) { return null; }
|
||||
}
|
||||
|
||||
private Map<String, Object> findKey(String id) {
|
||||
return jdbc.queryForObject("SELECT * FROM platform_api_keys WHERE id = ?", this::mapApiKey, UUID.fromString(id));
|
||||
}
|
||||
|
||||
private boolean ownsProject(String projectId, UUID userId) {
|
||||
Integer count = jdbc.queryForObject("SELECT COUNT(*) FROM model_projects WHERE id = ? AND owner_user_id = ? AND status = 'active'", Integer.class, projectId, userId);
|
||||
return count != null && count > 0;
|
||||
}
|
||||
|
||||
private Map<String, Object> mapProvider(ResultSet resultSet, int rowNumber) throws SQLException {
|
||||
Map<String, Object> provider = new LinkedHashMap<>();
|
||||
provider.put("id", resultSet.getString("id"));
|
||||
provider.put("code", resultSet.getString("code"));
|
||||
provider.put("name", resultSet.getString("name"));
|
||||
provider.put("protocol", resultSet.getString("protocol"));
|
||||
provider.put("sourceType", resultSet.getString("source_type"));
|
||||
putIfNotNull(provider, "baseUrl", resultSet.getString("base_url"));
|
||||
putIfNotNull(provider, "description", resultSet.getString("description"));
|
||||
putIfNotNull(provider, "credentialType", resultSet.getString("credential_type"));
|
||||
putIfNotNull(provider, "credentialMasked", resultSet.getString("credential_masked"));
|
||||
provider.put("status", resultSet.getString("status"));
|
||||
provider.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class));
|
||||
return provider;
|
||||
}
|
||||
|
||||
private Map<String, Object> mapModel(ResultSet resultSet, int rowNumber) throws SQLException {
|
||||
Map<String, Object> model = new LinkedHashMap<>();
|
||||
model.put("id", resultSet.getString("id"));
|
||||
model.put("providerId", resultSet.getString("provider_id"));
|
||||
putIfNotNull(model, "modelKey", resultSet.getString("model_key"));
|
||||
putIfNotNull(model, "sourceLabel", resultSet.getString("source_label"));
|
||||
model.put("publicName", resultSet.getString("public_name"));
|
||||
model.put("upstreamModel", resultSet.getString("upstream_model"));
|
||||
model.put("type", resultSet.getString("model_type"));
|
||||
model.put("mode", resultSet.getString("call_mode"));
|
||||
model.put("description", resultSet.getString("description"));
|
||||
model.put("inputTypes", readList(resultSet.getString("input_types")));
|
||||
model.put("outputTypes", readList(resultSet.getString("output_types")));
|
||||
model.put("capabilities", readMap(resultSet.getString("capabilities")));
|
||||
putIfNotNull(model, "pricing", readOptionalMap(resultSet.getString("pricing")));
|
||||
putIfNotNull(model, "priority", resultSet.getObject("priority"));
|
||||
putIfNotNull(model, "maxConcurrency", resultSet.getObject("max_concurrency"));
|
||||
model.put("status", resultSet.getString("status"));
|
||||
model.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class));
|
||||
return model;
|
||||
}
|
||||
|
||||
private Map<String, Object> mapProject(ResultSet resultSet, int rowNumber) throws SQLException {
|
||||
Map<String, Object> project = new LinkedHashMap<>();
|
||||
project.put("id", resultSet.getString("id"));
|
||||
project.put("ownerUserId", resultSet.getObject("owner_user_id", UUID.class));
|
||||
project.put("name", resultSet.getString("name"));
|
||||
project.put("description", resultSet.getString("description"));
|
||||
project.put("status", resultSet.getString("status"));
|
||||
project.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class));
|
||||
return project;
|
||||
}
|
||||
|
||||
private Map<String, Object> mapApiKey(ResultSet resultSet, int rowNumber) throws SQLException {
|
||||
Map<String, Object> key = new LinkedHashMap<>();
|
||||
UUID id = resultSet.getObject("id", UUID.class);
|
||||
String projectId = resultSet.getString("project_id");
|
||||
key.put("id", id);
|
||||
key.put("scopeType", projectId == null ? "personal" : "project");
|
||||
putIfNotNull(key, "projectId", projectId);
|
||||
key.put("name", resultSet.getString("name"));
|
||||
key.put("keyPrefix", resultSet.getString("key_prefix"));
|
||||
key.put("scopes", readList(resultSet.getString("scopes")));
|
||||
key.put("modelIds", readList(resultSet.getString("model_ids")));
|
||||
key.put("status", resultSet.getString("status"));
|
||||
key.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class));
|
||||
putIfNotNull(key, "lastUsedAt", resultSet.getObject("last_used_at", OffsetDateTime.class));
|
||||
putIfNotNull(key, "revokedAt", resultSet.getObject("revoked_at", OffsetDateTime.class));
|
||||
return key;
|
||||
}
|
||||
|
||||
private String required(Map<String, Object> input, String key) {
|
||||
String value = optional(input, key, "");
|
||||
if (value.isBlank()) throw new ApiException("INVALID_INPUT", key + " 不能为空");
|
||||
return value;
|
||||
}
|
||||
|
||||
private String optional(Map<String, Object> input, String key, String fallback) {
|
||||
Object value = input == null ? null : input.get(key);
|
||||
return value == null ? fallback : String.valueOf(value).trim();
|
||||
}
|
||||
|
||||
private Integer integerOrNull(Object value) {
|
||||
if (value == null || String.valueOf(value).isBlank()) return null;
|
||||
try { return Integer.valueOf(String.valueOf(value)); }
|
||||
catch (NumberFormatException exception) { throw new ApiException("INVALID_NUMBER", "数值格式无效"); }
|
||||
}
|
||||
|
||||
private List<String> stringList(Object value) {
|
||||
if (value == null) return new ArrayList<>();
|
||||
if (value instanceof List<?> list) return list.stream().map(String::valueOf).map(String::trim).filter(item -> !item.isBlank()).distinct().toList();
|
||||
return List.of(String.valueOf(value));
|
||||
}
|
||||
|
||||
private String jsonArray(Object value) { return writeJson(stringList(value)); }
|
||||
|
||||
private String jsonObject(Object value) { return value instanceof Map<?, ?> ? writeJson(value) : "{}"; }
|
||||
|
||||
private String jsonObjectOrNull(Object value) { return value instanceof Map<?, ?> ? writeJson(value) : null; }
|
||||
|
||||
private String writeJson(Object value) {
|
||||
try { return objectMapper.writeValueAsString(value); }
|
||||
catch (JsonProcessingException exception) { throw new ApiException("INVALID_JSON", "JSON字段格式无效"); }
|
||||
}
|
||||
|
||||
private List<String> readList(String value) {
|
||||
try { return value == null ? List.of() : objectMapper.readValue(value, new TypeReference<>() {}); }
|
||||
catch (JsonProcessingException exception) { return List.of(); }
|
||||
}
|
||||
|
||||
private Map<String, Object> readMap(String value) {
|
||||
try { return value == null ? Map.of() : objectMapper.readValue(value, new TypeReference<>() {}); }
|
||||
catch (JsonProcessingException exception) { return Map.of(); }
|
||||
}
|
||||
|
||||
private Map<String, Object> readOptionalMap(String value) { return value == null ? null : readMap(value); }
|
||||
|
||||
private void putIfNotNull(Map<String, Object> target, String key, Object value) { if (value != null) target.put(key, value); }
|
||||
|
||||
private String mask(String value) { return value.length() <= 8 ? "••••••••" : value.substring(0, 4) + "••••••••" + value.substring(value.length() - 4); }
|
||||
|
||||
private String randomToken(int byteCount) { byte[] bytes = new byte[byteCount]; secureRandom.nextBytes(bytes); return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes); }
|
||||
|
||||
private String sha256(String value) { try { return java.util.HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8))); } catch (Exception exception) { throw new IllegalStateException(exception); } }
|
||||
|
||||
private OffsetDateTime now() { return OffsetDateTime.now(ZoneOffset.UTC); }
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.moyi.controlplane.common;
|
||||
|
||||
public class ApiException extends RuntimeException {
|
||||
private final String code;
|
||||
private final int status;
|
||||
|
||||
public ApiException(String code, String message, int status) {
|
||||
super(message);
|
||||
this.code = code;
|
||||
this.status = status;
|
||||
}
|
||||
|
||||
public ApiException(String code, String message) {
|
||||
this(code, message, 400);
|
||||
}
|
||||
|
||||
public String getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public int getStatus() {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.moyi.controlplane.common;
|
||||
|
||||
import java.util.Map;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class ApiExceptionHandler {
|
||||
private static final Logger log = LoggerFactory.getLogger(ApiExceptionHandler.class);
|
||||
|
||||
@ExceptionHandler(ApiException.class)
|
||||
public ResponseEntity<Map<String, Object>> handleApiException(ApiException exception) {
|
||||
return ResponseEntity.status(exception.getStatus()).body(error(exception.getCode(), exception.getMessage()));
|
||||
}
|
||||
|
||||
@ExceptionHandler(DataIntegrityViolationException.class)
|
||||
public ResponseEntity<Map<String, Object>> handleConflict(DataIntegrityViolationException exception) {
|
||||
return ResponseEntity.status(409).body(error("DATA_CONFLICT", "数据已存在或关联关系无效"));
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public ResponseEntity<Map<String, Object>> handleUnexpected(Exception exception) {
|
||||
log.error("Unhandled API error", exception);
|
||||
return ResponseEntity.internalServerError().body(error("INTERNAL_ERROR", "服务暂时不可用"));
|
||||
}
|
||||
|
||||
private Map<String, Object> error(String code, String message) {
|
||||
return Map.of("error", Map.of("code", code, "message", message));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.moyi.controlplane.config;
|
||||
|
||||
import java.util.Arrays;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.config.annotation.CorsRegistry;
|
||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||
|
||||
@Configuration
|
||||
public class WebConfig implements WebMvcConfigurer {
|
||||
private final String[] allowedOrigins;
|
||||
|
||||
public WebConfig(@Value("${moyi.web.cors-origins}") String allowedOrigins) {
|
||||
this.allowedOrigins = Arrays.stream(allowedOrigins.split(","))
|
||||
.map(String::trim)
|
||||
.filter(origin -> !origin.isBlank())
|
||||
.toArray(String[]::new);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addCorsMappings(CorsRegistry registry) {
|
||||
registry.addMapping("/**")
|
||||
.allowedOrigins(allowedOrigins)
|
||||
.allowedMethods("GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS")
|
||||
.allowedHeaders("Content-Type", "Authorization")
|
||||
.allowCredentials(true)
|
||||
.maxAge(3600);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.time.Instant;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/v1")
|
||||
public class GatewayController {
|
||||
private final CatalogService catalogService;
|
||||
|
||||
public GatewayController(CatalogService catalogService) {
|
||||
this.catalogService = catalogService;
|
||||
}
|
||||
|
||||
@GetMapping("/models")
|
||||
public Map<String, Object> models(@RequestHeader(value = "Authorization", required = false) String authorization) {
|
||||
Map<String, Object> key = requireKey(authorization);
|
||||
List<Map<String, Object>> data = catalogService.listModels(false).stream()
|
||||
.filter(model -> catalogService.keyCanInvoke(key, String.valueOf(model.get("id"))))
|
||||
.map(model -> {
|
||||
Map<String, Object> item = new LinkedHashMap<>();
|
||||
item.put("id", model.get("id"));
|
||||
item.put("object", "model");
|
||||
item.put("owned_by", model.get("providerId"));
|
||||
item.put("type", model.get("type"));
|
||||
item.put("mode", model.get("mode"));
|
||||
return item;
|
||||
}).toList();
|
||||
catalogService.touchKey(key);
|
||||
return Map.of("object", "list", "data", data);
|
||||
}
|
||||
|
||||
@PostMapping("/chat/completions")
|
||||
public ResponseEntity<Map<String, Object>> chatCompletions(
|
||||
@RequestHeader(value = "Authorization", required = false) String authorization,
|
||||
@RequestBody Map<String, Object> input
|
||||
) {
|
||||
Map<String, Object> key = requireKey(authorization);
|
||||
String modelId = String.valueOf(input.getOrDefault("model", ""));
|
||||
Map<String, Object> model = catalogService.activeModel(modelId);
|
||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
||||
if (!catalogService.keyCanInvoke(key, modelId)) throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403);
|
||||
if ("async".equals(model.get("mode"))) throw new ApiException("ASYNC_MODEL", "该模型需要使用异步任务接口", 400);
|
||||
String prompt = extractPrompt(input.get("messages"));
|
||||
String content = "MOYI Java Mock 已收到:" + (prompt.isBlank() ? "(空消息)" : prompt);
|
||||
catalogService.touchKey(key);
|
||||
Map<String, Object> response = new LinkedHashMap<>();
|
||||
response.put("id", "chatcmpl-" + UUID.randomUUID().toString().replace("-", "").substring(0, 12));
|
||||
response.put("object", "chat.completion");
|
||||
response.put("created", Instant.now().getEpochSecond());
|
||||
response.put("model", modelId);
|
||||
response.put("choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", content), "finish_reason", "stop")));
|
||||
response.put("usage", Map.of("prompt_tokens", prompt.length(), "completion_tokens", content.length(), "total_tokens", prompt.length() + content.length()));
|
||||
return ResponseEntity.status(HttpStatus.OK).body(response);
|
||||
}
|
||||
|
||||
private Map<String, Object> requireKey(String authorization) {
|
||||
if (authorization == null || !authorization.startsWith("Bearer ")) throw new ApiException("UNAUTHENTICATED", "请提供有效的 API Key", 401);
|
||||
Map<String, Object> key = catalogService.authenticateKey(authorization.substring(7));
|
||||
if (key == null) throw new ApiException("INVALID_API_KEY", "API Key 无效或已停用", 401);
|
||||
return key;
|
||||
}
|
||||
|
||||
private String extractPrompt(Object messages) {
|
||||
if (!(messages instanceof List<?> list)) return "";
|
||||
return list.stream().filter(item -> item instanceof Map<?, ?>).map(item -> {
|
||||
Object content = ((Map<?, ?>) item).get("content");
|
||||
return content == null ? "" : String.valueOf(content);
|
||||
}).reduce((left, right) -> left + "\n" + right).orElse("");
|
||||
}
|
||||
}
|
||||
@@ -31,9 +31,18 @@ management:
|
||||
enabled: true
|
||||
|
||||
moyi:
|
||||
web:
|
||||
cors-origins: ${CONTROL_PLANE_CORS_ORIGINS:http://localhost:5173,http://127.0.0.1:5173}
|
||||
auth:
|
||||
session-ttl-hours: ${SESSION_TTL_HOURS:168}
|
||||
password-pepper: ${AUTH_PASSWORD_PEPPER:}
|
||||
secure-cookie: ${AUTH_SECURE_COOKIE:false}
|
||||
bootstrap:
|
||||
admin-account: ${ADMIN_ACCOUNT:adimin}
|
||||
admin-password: ${ADMIN_PASSWORD:admin123}
|
||||
admin-display-name: ${ADMIN_DISPLAY_NAME:平台管理员}
|
||||
credentials:
|
||||
encryption-key: ${CREDENTIAL_ENCRYPTION_KEY:}
|
||||
storage:
|
||||
oss-endpoint: ${OSS_ENDPOINT:}
|
||||
oss-region: ${OSS_REGION:}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
ALTER TABLE model_providers
|
||||
ADD COLUMN IF NOT EXISTS source_type varchar(24) NOT NULL DEFAULT 'custom',
|
||||
ADD COLUMN IF NOT EXISTS credential_type varchar(24),
|
||||
ADD COLUMN IF NOT EXISTS credential_ciphertext text,
|
||||
ADD COLUMN IF NOT EXISTS credential_masked varchar(64);
|
||||
|
||||
ALTER TABLE model_catalog
|
||||
ADD COLUMN IF NOT EXISTS model_key varchar(160),
|
||||
ADD COLUMN IF NOT EXISTS source_label varchar(160),
|
||||
ADD COLUMN IF NOT EXISTS pricing jsonb,
|
||||
ADD COLUMN IF NOT EXISTS priority integer,
|
||||
ADD COLUMN IF NOT EXISTS max_concurrency integer;
|
||||
|
||||
CREATE INDEX IF NOT EXISTS model_catalog_model_key_idx ON model_catalog(model_key);
|
||||
@@ -0,0 +1,18 @@
|
||||
CREATE TABLE IF NOT EXISTS usage_events (
|
||||
id uuid PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
api_key_id uuid REFERENCES platform_api_keys(id) ON DELETE SET NULL,
|
||||
model_id varchar(64) REFERENCES model_catalog(id) ON DELETE SET NULL,
|
||||
event_kind varchar(16) NOT NULL CHECK (event_kind IN ('call', 'task')),
|
||||
status varchar(16) NOT NULL CHECK (status IN ('queued', 'running', 'succeeded', 'failed', 'cancelled')),
|
||||
latency_ms integer,
|
||||
input_units bigint,
|
||||
output_units bigint,
|
||||
cost_amount numeric(18, 6),
|
||||
provider_task_id varchar(160),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS usage_events_user_created_idx ON usage_events(user_id, created_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS usage_events_task_id_idx ON usage_events(provider_task_id);
|
||||
@@ -0,0 +1,20 @@
|
||||
INSERT INTO model_providers (id, code, name, protocol, source_type, base_url, description, status)
|
||||
VALUES
|
||||
('provider-mock', 'moyi-mock', 'MOYI Mock Provider', 'http', 'custom', 'http://mock.local', '用于本地联调的 Mock 供应商。', 'active'),
|
||||
('provider-deepseek', 'deepseek', 'DeepSeek', 'openai-compatible', 'official', 'https://api.deepseek.com', 'DeepSeek 官方兼容接口。', 'active'),
|
||||
('provider-kimi', 'kimi', '月之暗面 Kimi', 'openai-compatible', 'official', 'https://api.moonshot.ai/v1', 'Kimi 官方兼容接口。', 'active'),
|
||||
('provider-zhipu', 'zhipu', '智谱 GLM', 'openai-compatible', 'official', 'https://open.bigmodel.cn/api/paas/v4', '智谱官方兼容接口。', 'active'),
|
||||
('provider-bytedance', 'bytedance', '字节跳动 Seedance', 'http', 'official', 'https://ark.cn-beijing.volces.com/api/v3', '火山方舟异步视频模型。', 'active'),
|
||||
('provider-vast', 'vast-tripo', 'VAST / Tripo', 'http', 'official', 'https://api.tripo3d.ai', 'Tripo 异步 3D 模型。', 'active')
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
|
||||
INSERT INTO model_catalog (id, provider_id, model_key, public_name, upstream_model, model_type, call_mode, description, input_types, output_types, capabilities, status)
|
||||
VALUES
|
||||
('model-mock-text', 'provider-mock', 'moyi-mock-text', 'MOYI Text Pro(Mock)', 'moyi-mock-text-v1', 'multimodal', 'sync', '用于本地验证标准聊天接口的同步模型。', '["text", "image"]', '["text"]', '{"streaming":true,"vision":true}', 'active'),
|
||||
('model-deepseek-v4-flash', 'provider-deepseek', 'deepseek-v4-flash', 'DeepSeek V4 Flash', 'deepseek-v4-flash', 'text', 'sync', 'DeepSeek 高速文本模型。', '["text"]', '["text"]', '{"streaming":true}', 'active'),
|
||||
('model-deepseek-v4-pro', 'provider-deepseek', 'deepseek-v4-pro', 'DeepSeek V4 Pro', 'deepseek-v4-pro', 'text', 'sync', 'DeepSeek 高能力推理模型。', '["text"]', '["text"]', '{"streaming":true,"reasoning":true}', 'active'),
|
||||
('model-kimi-k3', 'provider-kimi', 'kimi-k3', 'Kimi K3', 'kimi-k3', 'multimodal', 'sync', 'Kimi 长上下文多模态模型。', '["text", "image"]', '["text"]', '{"streaming":true,"vision":true}', 'active'),
|
||||
('model-glm-4-5', 'provider-zhipu', 'glm-4.5', 'GLM-4.5', 'glm-4.5', 'multimodal', 'sync', '智谱 GLM 通用多模态模型。', '["text", "image"]', '["text"]', '{"streaming":true,"toolCalling":true}', 'active'),
|
||||
('model-seedance-1-0-pro', 'provider-bytedance', 'seedance-1.0-pro', 'Seedance 1.0 Pro', 'doubao-seedance-1-0-pro-250528', 'video', 'async', '异步文生视频模型。', '["text", "image", "video", "audio"]', '["video"]', '{"taskPolling":true,"webhook":true}', 'active'),
|
||||
('model-mock-3d', 'provider-vast', 'tripo-v3', 'Tripo 3D Generator', 'v3.1-20260211', '3d', 'async', '异步文生 3D 模型。', '["text", "image"]', '["model", "thumbnail"]', '{"taskPolling":true,"webhook":true,"formats":["glb","obj","fbx"]}', 'active')
|
||||
ON CONFLICT (id) DO NOTHING;
|
||||
@@ -25,10 +25,10 @@ Java 服务使用环境变量读取数据库、Redis、OSS 和供应商配置。
|
||||
|
||||
1. Java 工程启动、健康检查和基础配置。
|
||||
2. PostgreSQL Flyway 迁移:用户、邀请码、会话、工作区、供应商、模型、Key、用量、审计。
|
||||
3. Redis 会话与限流:替换内存 session、登录限流和幂等键。
|
||||
4. 认证接口迁移:保持 `/api/auth/*` 返回结构兼容。
|
||||
5. 模型目录和供应商来源迁移:保持 `/api/models`、`/api/admin/providers`、`/api/admin/models` 兼容。
|
||||
6. API Key、模型权限、统一 `/v1/*` 调用和供应商适配迁移。
|
||||
3. 认证接口迁移:保持 `/api/auth/*` 返回结构兼容。
|
||||
4. 模型目录、项目、Key、模型权限和最小 `/v1/*` Mock 接口迁移(当前节点已完成)。
|
||||
5. Redis 会话/限流、并发计数、幂等键和任务队列。
|
||||
6. 同步供应商适配:先接入一个真实文本/多模态模型,再扩展其他厂商。
|
||||
7. 异步任务、队列、OSS 结果文件和 Webhook 迁移。
|
||||
8. 监控、备份、压测、灰度和回滚验证。
|
||||
|
||||
@@ -36,7 +36,7 @@ Java 服务使用环境变量读取数据库、Redis、OSS 和供应商配置。
|
||||
|
||||
### 现在必须提供
|
||||
|
||||
本阶段只需要确认部署边界,不需要把真实密码或 API Key 发到聊天中:
|
||||
本阶段只需要确认部署边界,不需要把真实密码或 API Key 发到聊天中。若使用托管 RDS、Redis、OSS,应用容器只保存连接配置,不在服务器本地运行这些基础设施:
|
||||
|
||||
- 云服务器是否使用现有服务器;操作系统、CPU/内存/磁盘、CPU 架构和 Docker / Compose 版本。
|
||||
- 计划使用的域名、DNS 管理方式、是否已有 Nginx/Caddy、HTTPS 证书和备案状态。
|
||||
@@ -44,6 +44,8 @@ Java 服务使用环境变量读取数据库、Redis、OSS 和供应商配置。
|
||||
- Redis 使用云 Redis 还是 Docker;版本、内网地址、端口、是否 TLS、ACL 用户和网络白名单。
|
||||
- OSS 使用哪家云厂商;Region、Endpoint、Bucket、内网访问 Endpoint、对象公开/私有策略和生命周期规则。
|
||||
|
||||
本地依赖使用 `docker-compose.yml`;云上托管依赖使用 `docker-compose.cloud.yml` 和 `.env.cloud.example`。
|
||||
|
||||
### 开始真实联调前需要提供
|
||||
|
||||
- DeepSeek、Kimi、GLM、Seedance、Tripo/VAST 以及其他中转渠道的测试环境 Key、Base URL、模型 ID 和限额。
|
||||
|
||||
@@ -0,0 +1,73 @@
|
||||
# Java Control Plane 节点一:基础控制面
|
||||
|
||||
## 节点目标
|
||||
|
||||
把前端原型依赖的核心控制面接口迁移到 Java 21 / Spring Boot,并让本地 Docker Compose 可以使用同一套数据库迁移脚本启动:
|
||||
|
||||
- 邀请码注册、账号密码登录、会话 Cookie、退出登录;
|
||||
- 当前用户、管理员用户列表、邀请码管理、审计记录;
|
||||
- 供应商和模型目录读取;
|
||||
- 项目创建与查询;
|
||||
- API Key 创建、明文一次性返回、撤销和模型权限配置;
|
||||
- 最小 OpenAI 兼容 `GET /v1/models` 与 `POST /v1/chat/completions` Mock 接口。
|
||||
|
||||
## 已落地接口
|
||||
|
||||
| 接口 | 权限 | 用途 |
|
||||
| --- | --- | --- |
|
||||
| `POST /api/auth/login` | 公开 | 账号密码登录 |
|
||||
| `POST /api/auth/register` | 公开 | 邀请码注册 |
|
||||
| `GET /api/auth/me` | 登录用户 | 当前会话用户 |
|
||||
| `POST /api/auth/logout` | 登录用户 | 撤销当前会话 |
|
||||
| `GET /api/models` | 登录用户 | 用户端模型广场目录 |
|
||||
| `GET/POST /api/projects` | 登录用户 | 工作区/项目基础管理 |
|
||||
| `GET/POST /api/keys` | 登录用户 | API Key 管理 |
|
||||
| `POST /api/keys/{id}/models` | Key 所属用户 | 修改模型权限 |
|
||||
| `POST /api/keys/{id}/revoke` | Key 所属用户 | 撤销 API Key |
|
||||
| `GET/POST /api/admin/providers` | 管理员 | 供应商目录管理 |
|
||||
| `GET/POST /api/admin/models` | 管理员 | 模型目录管理 |
|
||||
| `GET/POST /api/admin/invitations` | 管理员 | 邀请码管理 |
|
||||
| `GET /api/admin/users` | 管理员 | 用户列表 |
|
||||
| `GET /api/admin/audit` | 管理员 | 审计记录 |
|
||||
| `GET /v1/models` | 平台 API Key | 返回该 Key 有权限的模型 |
|
||||
| `POST /v1/chat/completions` | 平台 API Key | Mock 同步聊天调用 |
|
||||
|
||||
## 数据库迁移
|
||||
|
||||
- `V1__auth.sql`:用户、邀请码、会话、审计;
|
||||
- `V2__catalog_and_keys.sql`:供应商、模型目录、API Key;
|
||||
- `V3__projects.sql`:项目和 Key 项目归属;
|
||||
- `V4__catalog_channels.sql`:供应商凭证元数据、模型来源分组、价格和并发字段;
|
||||
- `V5__usage_events.sql`:用量事件基础表;
|
||||
- `V6__seed_catalog.sql`:本地首批模型目录初始数据,可由管理员继续增删改造。
|
||||
|
||||
Java 版本沿用现有 TypeScript 服务的表命名,便于后续迁移 RDS;供应商密钥当前只返回掩码并预留密文列,真实调用接入前需要补齐 KMS/RAM 或 AES-GCM 密钥管理。
|
||||
|
||||
## 本地启动
|
||||
|
||||
```powershell
|
||||
cd infra/moyi
|
||||
Copy-Item .env.example .env
|
||||
docker compose build
|
||||
docker compose up -d
|
||||
Invoke-WebRequest http://localhost:8080/health
|
||||
```
|
||||
|
||||
默认开发管理员来自 `.env`:`ADMIN_ACCOUNT=adimin`,首次启动时由 Java 服务创建。生产环境必须替换管理员密码、密码 Pepper、数据库密码和 Redis 密码。
|
||||
|
||||
## 验收边界
|
||||
|
||||
本节点完成的是“控制面基础闭环”,不是生产网关完成版:
|
||||
|
||||
- 数据已写入 PostgreSQL,Redis 连接配置已预留,但限流、队列和分布式幂等尚未接入;
|
||||
- `/v1/chat/completions` 目前返回 Java Mock,不会自动调用 DeepSeek、Kimi 或 GLM;
|
||||
- Seedance、Tripo/VAST 的异步任务和 OSS 结果存储尚未迁移到 Java;
|
||||
- 价格、额度扣减、计费账单和并发排队仍沿用下一节点计划;
|
||||
- Docker 镜像需要在具备 Maven 和 Docker daemon 的环境中构建验证。
|
||||
|
||||
## 下一节点
|
||||
|
||||
1. 把模型供应商适配器迁移为 Java HTTP 客户端,先接入一个同步模型;
|
||||
2. 接入 Redis 限流、并发计数、幂等键和异步任务状态;
|
||||
3. 接入 Tripo/Seedance 异步任务与 OSS 私有对象;
|
||||
4. 用 RDS、Redis、OSS 的测试资源完成云上验收,再切换前端的 Java API 地址。
|
||||
@@ -0,0 +1,30 @@
|
||||
# Copy to .env.cloud on the target server. Never commit the real file.
|
||||
MOYI_HTTP_PORT=8080
|
||||
CONTROL_PLANE_CORS_ORIGINS=https://moyi.example.com
|
||||
|
||||
# Alibaba Cloud RDS PostgreSQL private endpoint.
|
||||
MOYI_DB_URL=jdbc:postgresql://rds-private-endpoint:1921/moyi_control
|
||||
MOYI_DB_USERNAME=moyi_app
|
||||
MOYI_DB_PASSWORD=replace-with-a-long-random-password
|
||||
|
||||
# Alibaba Cloud Redis/Tair private endpoint. Use TLS parameters if enabled by the instance.
|
||||
MOYI_REDIS_URL=rediss://:replace-with-redis-password@redis-private-endpoint:6379/0
|
||||
|
||||
SESSION_TTL_HOURS=168
|
||||
AUTH_PASSWORD_PEPPER=replace-with-a-long-random-secret-at-least-32-chars
|
||||
ADMIN_ACCOUNT=adimin
|
||||
ADMIN_PASSWORD=replace-with-a-strong-initial-password
|
||||
ADMIN_DISPLAY_NAME=平台管理员
|
||||
AUTH_SECURE_COOKIE=true
|
||||
|
||||
OSS_ENDPOINT=
|
||||
OSS_REGION=
|
||||
OSS_BUCKET=
|
||||
OSS_ACCESS_KEY_ID=
|
||||
OSS_ACCESS_KEY_SECRET=
|
||||
|
||||
TRIPO_API_KEY=
|
||||
DEEPSEEK_API_KEY=
|
||||
KIMI_API_KEY=
|
||||
ZHIPU_API_KEY=
|
||||
VOLCENGINE_API_KEY=
|
||||
+13
-2
@@ -4,7 +4,7 @@
|
||||
|
||||
## 当前迁移状态
|
||||
|
||||
`control-plane-java/` 当前提供 Java 21 / Spring Boot 的启动和健康检查骨架。现有 TypeScript Control Plane 仍是接口迁移期间的可用实现,Java 服务完成认证、模型目录、Key、路由和任务接口迁移并通过 UAT 前,不要直接把生产流量切到 Java 服务。
|
||||
`control-plane-java/` 当前提供 Java 21 / Spring Boot 的认证、模型目录、项目、Key、权限和最小 OpenAI 兼容接口。真实供应商适配、Redis 队列/限流、异步任务和计费完成并通过 UAT 前,不要直接把生产流量切到 Java 服务。
|
||||
|
||||
## 启动
|
||||
|
||||
@@ -22,7 +22,18 @@ docker compose ps
|
||||
Invoke-WebRequest http://localhost:8080/health
|
||||
```
|
||||
|
||||
前端入口为 `http://localhost:8080`。当前 Java 骨架阶段只保证 `/health` 可用;业务接口仍需完成迁移后才适合用此 Compose 做完整用户验收。
|
||||
前端入口为 `http://localhost:8080`。可先使用邀请码登录、查看模型目录、创建项目和 API Key,并通过 Key 调用 Mock 的 `/v1/chat/completions`;真实供应商和异步任务仍需后续节点接入。
|
||||
|
||||
## 云上托管依赖
|
||||
|
||||
RDS、Redis 和 OSS 不需要部署在轻量应用服务器上。云上使用托管服务时,复制 `.env.cloud.example` 为 `.env.cloud`,填写 RDS/Redis 私网连接信息,然后执行:
|
||||
|
||||
```powershell
|
||||
docker compose --env-file .env.cloud -f docker-compose.cloud.yml build
|
||||
docker compose --env-file .env.cloud -f docker-compose.cloud.yml up -d
|
||||
```
|
||||
|
||||
轻量应用服务器与RDS/Redis通常需要在阿里云控制台配置同地域内网互通和白名单;如果直接使用ECS并放在同一VPC,则不需要这层轻量服务器互通配置。OSS优先填写同地域内网Endpoint。
|
||||
|
||||
## 停止与数据
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
name: moyi
|
||||
|
||||
services:
|
||||
moyi-api:
|
||||
build:
|
||||
context: ../../control-plane-java
|
||||
restart: unless-stopped
|
||||
env_file:
|
||||
- path: .env.cloud
|
||||
required: false
|
||||
environment:
|
||||
CONTROL_PLANE_HOST: 0.0.0.0
|
||||
CONTROL_PLANE_PORT: 8787
|
||||
CONTROL_PLANE_CORS_ORIGINS: ${CONTROL_PLANE_CORS_ORIGINS:-https://moyi.example.com}
|
||||
SPRING_DATASOURCE_URL: ${MOYI_DB_URL:?Set MOYI_DB_URL in infra/moyi/.env.cloud}
|
||||
SPRING_DATASOURCE_USERNAME: ${MOYI_DB_USERNAME:?Set MOYI_DB_USERNAME in infra/moyi/.env.cloud}
|
||||
SPRING_DATASOURCE_PASSWORD: ${MOYI_DB_PASSWORD:?Set MOYI_DB_PASSWORD in infra/moyi/.env.cloud}
|
||||
SPRING_DATA_REDIS_URL: ${MOYI_REDIS_URL:?Set MOYI_REDIS_URL in infra/moyi/.env.cloud}
|
||||
FLYWAY_ENABLED: true
|
||||
AUTH_SECURE_COOKIE: true
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget -q -O - http://127.0.0.1:8787/health | grep -q 'status'"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 12
|
||||
start_period: 20s
|
||||
|
||||
frontend:
|
||||
build:
|
||||
context: ../../prototype
|
||||
args:
|
||||
VITE_CONTROL_PLANE_URL: /
|
||||
VITE_AUTH_MODE: remote
|
||||
restart: unless-stopped
|
||||
ports:
|
||||
- "${MOYI_HTTP_PORT:-8080}:80"
|
||||
depends_on:
|
||||
moyi-api:
|
||||
condition: service_healthy
|
||||
Reference in new issue
Block a user