feat: 统一开放接口网关微服务初始化工程 (OAuth2.0 + MCP + OpenAPI + Webhook)
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
package com.iot.gateway.admin;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.iot.gateway.common.BizException;
|
||||
import com.iot.gateway.common.Result;
|
||||
import com.iot.gateway.entity.OpenClient;
|
||||
import com.iot.gateway.mapper.OpenClientMapper;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
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.PutMapping;
|
||||
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;
|
||||
|
||||
@Slf4j
|
||||
@Tag(name = "开放平台管理 - 三方应用凭据管理")
|
||||
@RestController
|
||||
@RequestMapping("/api/open/client")
|
||||
public class OpenClientAdminController {
|
||||
|
||||
@Autowired
|
||||
private OpenClientMapper clientMapper;
|
||||
|
||||
private static final SecureRandom RANDOM = new SecureRandom();
|
||||
|
||||
@Operation(summary = "三方应用凭据分页列表")
|
||||
@GetMapping("/page")
|
||||
public Result<Page<OpenClient>> page(
|
||||
@RequestParam(defaultValue = "1") Integer pageNum,
|
||||
@RequestParam(defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(required = false) Long tenantId,
|
||||
@RequestParam(required = false) String keyword) {
|
||||
|
||||
Page<OpenClient> page = new Page<>(pageNum, pageSize);
|
||||
LambdaQueryWrapper<OpenClient> qw = new LambdaQueryWrapper<>();
|
||||
if (tenantId != null) {
|
||||
qw.eq(OpenClient::getTenantId, tenantId);
|
||||
}
|
||||
if (StringUtils.hasText(keyword)) {
|
||||
qw.and(w -> w.like(OpenClient::getClientName, keyword)
|
||||
.or().like(OpenClient::getClientId, keyword));
|
||||
}
|
||||
qw.orderByDesc(OpenClient::getCreateTime);
|
||||
return Result.success(clientMapper.selectPage(page, qw));
|
||||
}
|
||||
|
||||
@Operation(summary = "获取凭据详情")
|
||||
@GetMapping("/{id}")
|
||||
public Result<OpenClient> getById(@PathVariable Long id) {
|
||||
OpenClient client = clientMapper.selectById(id);
|
||||
if (client == null) throw new BizException("客户端凭据不存在");
|
||||
return Result.success(client);
|
||||
}
|
||||
|
||||
@Operation(summary = "新建三方应用凭据 (自动生成 ClientId 与 Secret)")
|
||||
@PostMapping
|
||||
public Result<OpenClient> create(@RequestBody OpenClient req) {
|
||||
req.setId(null);
|
||||
req.setClientId("client_" + UUID.randomUUID().toString().replace("-", "").substring(0, 12));
|
||||
req.setClientSecret("sec_" + UUID.randomUUID().toString().replace("-", "") + Long.toHexString(RANDOM.nextLong()));
|
||||
if (req.getTenantId() == null) req.setTenantId(1L);
|
||||
if (!StringUtils.hasText(req.getScopes())) req.setScopes("device.read");
|
||||
if (req.getStatus() == null) req.setStatus(1);
|
||||
if (req.getRateLimit() == null) req.setRateLimit(60);
|
||||
|
||||
clientMapper.insert(req);
|
||||
log.info("新建开放应用凭据: id={}, clientId={}", req.getId(), req.getClientId());
|
||||
return Result.success(req);
|
||||
}
|
||||
|
||||
@Operation(summary = "修改三方应用凭据")
|
||||
@PutMapping("/{id}")
|
||||
public Result<Void> update(@PathVariable Long id, @RequestBody OpenClient req) {
|
||||
OpenClient client = clientMapper.selectById(id);
|
||||
if (client == null) throw new BizException("客户端凭据不存在");
|
||||
|
||||
client.setClientName(req.getClientName());
|
||||
if (req.getScopes() != null) client.setScopes(req.getScopes());
|
||||
client.setIpWhitelist(req.getIpWhitelist());
|
||||
if (req.getRateLimit() != null) client.setRateLimit(req.getRateLimit());
|
||||
if (req.getStatus() != null) client.setStatus(req.getStatus());
|
||||
client.setExpireTime(req.getExpireTime());
|
||||
client.setRemark(req.getRemark());
|
||||
|
||||
clientMapper.updateById(client);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "删除三方应用凭据")
|
||||
@DeleteMapping("/{id}")
|
||||
public Result<Void> delete(@PathVariable Long id) {
|
||||
clientMapper.deleteById(id);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "重置客户端密钥 (Secret)")
|
||||
@PostMapping("/{id}/reset-secret")
|
||||
public Result<Map<String, String>> resetSecret(@PathVariable Long id) {
|
||||
OpenClient client = clientMapper.selectById(id);
|
||||
if (client == null) throw new BizException("客户端凭据不存在");
|
||||
|
||||
String newSecret = "sec_" + UUID.randomUUID().toString().replace("-", "") + Long.toHexString(RANDOM.nextLong());
|
||||
client.setClientSecret(newSecret);
|
||||
clientMapper.updateById(client);
|
||||
return Result.success(Map.of("clientSecret", newSecret));
|
||||
}
|
||||
|
||||
@Operation(summary = "更新客户端状态")
|
||||
@PutMapping("/{id}/status")
|
||||
public Result<Void> updateStatus(@PathVariable Long id, @RequestParam Integer status) {
|
||||
OpenClient client = clientMapper.selectById(id);
|
||||
if (client == null) throw new BizException("客户端凭据不存在");
|
||||
client.setStatus(status);
|
||||
clientMapper.updateById(client);
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.iot.gateway.admin;
|
||||
|
||||
import com.iot.gateway.common.Result;
|
||||
import com.iot.gateway.mcp.McpToolRegistry;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@Tag(name = "网关运行状态与能力目录")
|
||||
@RestController
|
||||
@RequestMapping("/open/gateway")
|
||||
public class OpenGatewayInfoController {
|
||||
|
||||
@Autowired
|
||||
private McpToolRegistry toolRegistry;
|
||||
|
||||
@Operation(summary = "网关健康检查探针")
|
||||
@GetMapping("/health")
|
||||
public Result<Map<String, Object>> health() {
|
||||
return Result.success(Map.of(
|
||||
"status", "ONLINE",
|
||||
"service", "iot-open-gateway",
|
||||
"timestamp", System.currentTimeMillis()
|
||||
));
|
||||
}
|
||||
|
||||
@Operation(summary = "拉取可用能力目录 (MCP Tools 与 OpenAPI)")
|
||||
@GetMapping("/catalog")
|
||||
public Result<Map<String, Object>> catalog() {
|
||||
List<Map<String, Object>> tools = toolRegistry.getToolDefinitions();
|
||||
List<String> apis = List.of(
|
||||
"GET /open/v1/devices (租户设备台账与状态)",
|
||||
"GET /open/v1/devices/{id} (单设备详情与点位定义)",
|
||||
"GET /open/v1/telemetry/latest (设备最新实时工况召测)",
|
||||
"GET /open/v1/telemetry/history (时序历史遥测点位拉取)",
|
||||
"POST /open/v1/commands (设备控制指令反写下发)",
|
||||
"GET /open/v1/alarms (故障报警检索与确认)"
|
||||
);
|
||||
|
||||
return Result.success(Map.of(
|
||||
"status", "ONLINE",
|
||||
"mcpEndpoint", "/mcp/sse",
|
||||
"tokenEndpoint", "/oauth2/token",
|
||||
"toolCount", tools.size(),
|
||||
"tools", tools,
|
||||
"openApis", apis
|
||||
));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package com.iot.gateway.admin;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.iot.gateway.common.BizException;
|
||||
import com.iot.gateway.common.Result;
|
||||
import com.iot.gateway.entity.OpenWebhook;
|
||||
import com.iot.gateway.mapper.OpenWebhookMapper;
|
||||
import com.iot.gateway.webhook.WebhookDispatchService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import java.util.Map;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
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.PutMapping;
|
||||
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;
|
||||
|
||||
@Slf4j
|
||||
@Tag(name = "开放平台管理 - Webhook 通道管理")
|
||||
@RestController
|
||||
@RequestMapping("/api/open/webhook")
|
||||
public class OpenWebhookAdminController {
|
||||
|
||||
@Autowired
|
||||
private OpenWebhookMapper webhookMapper;
|
||||
|
||||
@Autowired
|
||||
private WebhookDispatchService dispatchService;
|
||||
|
||||
@Operation(summary = "Webhook 通道分页列表")
|
||||
@GetMapping("/page")
|
||||
public Result<Page<OpenWebhook>> page(
|
||||
@RequestParam(defaultValue = "1") Integer pageNum,
|
||||
@RequestParam(defaultValue = "10") Integer pageSize,
|
||||
@RequestParam(required = false) Long tenantId,
|
||||
@RequestParam(required = false) String keyword) {
|
||||
|
||||
Page<OpenWebhook> page = new Page<>(pageNum, pageSize);
|
||||
LambdaQueryWrapper<OpenWebhook> qw = new LambdaQueryWrapper<>();
|
||||
if (tenantId != null) {
|
||||
qw.eq(OpenWebhook::getTenantId, tenantId);
|
||||
}
|
||||
if (StringUtils.hasText(keyword)) {
|
||||
qw.and(w -> w.like(OpenWebhook::getChannelName, keyword)
|
||||
.or().like(OpenWebhook::getWebhookUrl, keyword));
|
||||
}
|
||||
qw.orderByDesc(OpenWebhook::getCreateTime);
|
||||
return Result.success(webhookMapper.selectPage(page, qw));
|
||||
}
|
||||
|
||||
@Operation(summary = "新建 Webhook 通道")
|
||||
@PostMapping
|
||||
public Result<OpenWebhook> create(@RequestBody OpenWebhook req) {
|
||||
req.setId(null);
|
||||
if (req.getTenantId() == null) req.setTenantId(1L);
|
||||
if (!StringUtils.hasText(req.getEvents())) req.setEvents("device.alarm");
|
||||
if (req.getStatus() == null) req.setStatus(1);
|
||||
|
||||
webhookMapper.insert(req);
|
||||
return Result.success(req);
|
||||
}
|
||||
|
||||
@Operation(summary = "修改 Webhook 通道")
|
||||
@PutMapping("/{id}")
|
||||
public Result<Void> update(@PathVariable Long id, @RequestBody OpenWebhook req) {
|
||||
OpenWebhook item = webhookMapper.selectById(id);
|
||||
if (item == null) throw new BizException("通道不存在");
|
||||
|
||||
item.setChannelName(req.getChannelName());
|
||||
item.setChannelType(req.getChannelType());
|
||||
item.setWebhookUrl(req.getWebhookUrl());
|
||||
item.setSecret(req.getSecret());
|
||||
if (req.getEvents() != null) item.setEvents(req.getEvents());
|
||||
if (req.getStatus() != null) item.setStatus(req.getStatus());
|
||||
item.setRemark(req.getRemark());
|
||||
|
||||
webhookMapper.updateById(item);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "删除 Webhook 通道")
|
||||
@DeleteMapping("/{id}")
|
||||
public Result<Void> delete(@PathVariable Long id) {
|
||||
webhookMapper.deleteById(id);
|
||||
return Result.success();
|
||||
}
|
||||
|
||||
@Operation(summary = "测试 Webhook 推送连通性")
|
||||
@PostMapping("/{id}/test")
|
||||
public Result<Map<String, Object>> testSend(@PathVariable Long id, @RequestBody(required = false) Map<String, String> body) {
|
||||
OpenWebhook item = webhookMapper.selectById(id);
|
||||
if (item == null) throw new BizException("通道不存在");
|
||||
|
||||
String title = body != null && body.containsKey("title") ? body.get("title") : "【IoT平台测试】告警通知";
|
||||
String content = body != null && body.containsKey("content") ? body.get("content") : "这是一条来自开放平台的连通性测试消息。";
|
||||
|
||||
boolean ok = dispatchService.sendDirect(item.getChannelType(), item.getWebhookUrl(), title, content);
|
||||
return Result.success(Map.of("success", ok, "message", ok ? "测试消息已成功送达" : "发送失败,请检查 URL 与网络配置"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user