task) {
+ boolean acquired;
+ try {
+ acquired = semaphore.tryAcquire(acquireTimeoutMs, TimeUnit.MILLISECONDS);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new ReportBusyException(maxConcurrent);
+ }
+ if (!acquired) {
+ throw new ReportBusyException(maxConcurrent);
+ }
+ try {
+ return task.get();
+ } finally {
+ semaphore.release();
+ }
+ }
+}
diff --git a/platform-report/src/main/java/com/eip4/platform/report/ReportBusyException.java b/platform-report/src/main/java/com/eip4/platform/report/ReportBusyException.java
new file mode 100644
index 0000000..dca83e6
--- /dev/null
+++ b/platform-report/src/main/java/com/eip4/platform/report/ReportBusyException.java
@@ -0,0 +1,19 @@
+package com.eip4.platform.report;
+
+/**
+ * 渲染并发已达上限({@code eip4.report.max-concurrent-renders}),在等待超时内未取得许可。
+ * 控制器据此返回 429 Too Many Requests,避免 N 个并发大报表线性叠加堆占用导致 OOM。
+ */
+public class ReportBusyException extends RuntimeException {
+
+ private final int maxConcurrent;
+
+ public ReportBusyException(int maxConcurrent) {
+ super("报表渲染并发已达上限 " + maxConcurrent + ",请稍后重试");
+ this.maxConcurrent = maxConcurrent;
+ }
+
+ public int getMaxConcurrent() {
+ return maxConcurrent;
+ }
+}
diff --git a/platform-report/src/main/java/com/eip4/platform/report/ReportController.java b/platform-report/src/main/java/com/eip4/platform/report/ReportController.java
index 8fcda6a..f6a4154 100644
--- a/platform-report/src/main/java/com/eip4/platform/report/ReportController.java
+++ b/platform-report/src/main/java/com/eip4/platform/report/ReportController.java
@@ -1,6 +1,7 @@
package com.eip4.platform.report;
-import java.io.InputStream;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
@@ -8,11 +9,13 @@ import java.nio.file.Path;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
+import java.util.Properties;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
@@ -23,26 +26,27 @@ import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.server.ResponseStatusException;
+import com.dsasp.server.common.user.service.LoggedInUser;
+import com.dsasp.server.common.util.UserSession;
+import com.eip4.platform.auth.security.SecurityProfiles;
+
import jakarta.servlet.http.HttpServletRequest;
/**
- * 报表渲染端点。
+ * 报表渲染端点(含安全收口)。
*
* {@code GET /api/report/render/{templateId}?format=html|pdf|xls&<查询参数...>}
- * → 把 {@code templateId} 解析为 JOR 模板 .xml → {@link ReportService#render} → 返回
- * 渲染产物(HTML/PDF/XLS)。除 {@code format}/{@code tpl} 外的全部查询参数原样作为报表参数
- * 透传(报表 SQL 里的 {@code ${PARAM}} 据此绑定)。
+ * → 把 {@code templateId} 解析为 JOR 模板 → {@link ReportService#render} → 返回渲染产物。
*
- *
模板解析(v1):
+ *
安全门(P0/P1,见各 {@code ReportXxxGuard}):
*
- * - {@code ?tpl=<绝对路径>} —— 验证/演示用,直接指定 .xml 绝对路径(仅 dev/local 便利)。
- * - 否则把 {@code templateId} 当作相对 {@code eip4.report.templates-dir} 的路径解析,
- * 默认基目录指向 EIP3 report 根。Phase 3 改为查 {@code sys_report_template} 表。
+ * - 对象级授权 {@link #enforceReportAccess}:已认证非超管用户只能渲染其可见菜单暴露的报表(防 IDOR);
+ * - SQL 注入门 {@link ReportParamGuard}:只放行模板引用过的参数名 + 值校验 + 剥离身份保留位;
+ * - 可信身份注入:{@code ${CURRENTUSER}} 等由服务端从 JWT 上下文注入,杜绝客户端伪造越权;
+ * - {@code ?tpl} 守卫 {@link ReportTplGuard}:仅旁路 profile 允许,且限定在 templates-dir 内、仅 .xml;
+ * - XXE/并发/字节上限/超时在 {@link ReportService}/{@link JatoolsConfig} 侧。
*
- *
- * 注:不与既有 {@code /api/report/menu-tree}(platform-metadata)冲突 —— 该控制器只占
- * {@code /api/report/render/**}。安全:这是 {@code /api/**} 端点,在 dev/local/ci 宽松链下放行,
- * 严格 profile 下走 {@code authenticated()},沿用既有安全模型,无需改 SecurityConfig。
+ * 错误响应只回通用文案(底层 SQL/路径/异常细节仅写服务端日志,杜绝信息泄露)。
*/
@RestController
public class ReportController {
@@ -51,14 +55,21 @@ public class ReportController {
private final ReportService reportService;
private final ReportTemplateRepository templateRepository;
+ private final Environment environment;
@Value("${eip4.report.templates-dir:/Users/zhengwr/workspace/EIP-3.0}")
private String templatesDir;
+ /** 是否要求报表对象级菜单授权(默认开)。关闭后已认证非超管用户可渲染任意报表(仅排障用)。 */
+ @Value("${eip4.report.require-menu-grant:true}")
+ private boolean requireMenuGrant;
+
public ReportController(ReportService reportService,
- ReportTemplateRepository templateRepository) {
+ ReportTemplateRepository templateRepository,
+ Environment environment) {
this.reportService = reportService;
this.templateRepository = templateRepository;
+ this.environment = environment;
}
@GetMapping("/api/report/render/{templateId}")
@@ -66,33 +77,42 @@ public class ReportController {
@RequestParam(value = "format", required = false, defaultValue = "html") String format,
@RequestParam(value = "tpl", required = false) String tplPath,
HttpServletRequest request) {
- Map params = collectReportParams(request);
- ReportService.Format fmt = ReportService.Format.parse(format);
+ ReportService.Format fmt;
+ try {
+ fmt = ReportService.Format.parse(format);
+ } catch (IllegalArgumentException e) {
+ throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "不支持的报表格式");
+ }
+ enforceReportAccess(templateId);
byte[] body;
try {
- if (tplPath != null && !tplPath.isBlank()) {
- // dev/演示覆盖:直接渲染指定 .xml 文件(可为 JOR 文档或 view_template 外壳)。
- Path xml = resolveTemplate(templateId, tplPath);
- try (InputStream is = Files.newInputStream(xml)) {
- body = reportService.render(is, params, format);
- }
- } else {
- // 标准路径:按 templateId 从 view_template(type=200) 取 JOR 文档并渲染。
- String jorXml = templateRepository.loadJorXml(templateId);
- body = reportService.render(jorXml, params, format);
- }
+ byte[] xmlBytes = loadTemplateBytes(templateId, tplPath);
+ Map params = buildReportParams(request, xmlBytes);
+ body = reportService.render(new ByteArrayInputStream(xmlBytes), params, format);
} catch (ReportTemplateRepository.ReportTemplateNotFoundException e) {
log.warn("报表模板未找到 templateId={} : {}", templateId, e.getMessage());
- throw new ResponseStatusException(HttpStatus.NOT_FOUND, e.getMessage(), e);
+ throw new ResponseStatusException(HttpStatus.NOT_FOUND, "报表模板未找到");
+ } catch (ReportBusyException e) {
+ log.warn("报表渲染并发已满 templateId={} : {}", templateId, e.getMessage());
+ throw new ResponseStatusException(HttpStatus.TOO_MANY_REQUESTS, "报表渲染繁忙,请稍后重试");
+ } catch (ReportSizeExceededException e) {
+ log.warn("报表产物超限 templateId={} : {}", templateId, e.getMessage());
+ throw new ResponseStatusException(HttpStatus.PAYLOAD_TOO_LARGE, "报表数据过大,请缩小查询范围");
+ } catch (SecurityException e) {
+ log.warn("报表请求被安全门拒绝 templateId={} tpl={} : {}", templateId, tplPath, e.getMessage());
+ throw new ResponseStatusException(HttpStatus.FORBIDDEN, "请求被拒绝");
+ } catch (IllegalArgumentException e) {
+ log.warn("报表请求参数非法 templateId={} tpl={} : {}", templateId, tplPath, e.getMessage());
+ throw new ResponseStatusException(HttpStatus.BAD_REQUEST, "请求参数非法");
} catch (ReportService.ReportRenderException e) {
log.warn("报表渲染失败 templateId={} tpl={} : {}", templateId, tplPath, e.getMessage());
- throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, e.getMessage(), e);
+ throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "报表渲染失败");
} catch (ResponseStatusException e) {
throw e;
} catch (Exception e) {
- throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR,
- "读取/渲染报表模板失败: " + e.getMessage(), e);
+ log.warn("报表处理失败 templateId={} : {}", templateId, e.toString());
+ throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "报表处理失败");
}
ResponseEntity.BodyBuilder rb = ResponseEntity.ok()
@@ -113,28 +133,124 @@ public class ReportController {
* → 返回 amis CRUD 单行信封 {@code {status:0,msg:"",data:{items:[{__url}],total:1}}},
* {@code __url} = {@code /api/report/render/{id}?format=html&<回显的查询参数>}。
*
- * 报表页是 amis {@code crud}:查询条件放在 {@code crud.filter}(实测唯一能让 picker
- * 正常加载选项的上下文;放在独立 form 里 picker 弹窗「暂无数据」)。crud 的单个 html 列把
- * {@code __url} 渲成 {@code