From 99346c7c0c87d30f7833b94aadccb1d0cc80edbb Mon Sep 17 00:00:00 2001 From: lorne <1991wangliang@gmail.com> Date: Thu, 3 Dec 2026 08:51:50 +0800 Subject: [PATCH] =?UTF-8?q?fix(#301):=20=E8=BA=AB=E4=BB=BD=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=E8=AF=B7=E6=B1=82=E5=A4=B4=E4=BC=A0=E9=80=92=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E6=B1=A1=E6=9F=93=20SearchRequest=20?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 身份原先前端以 query 参数 operatorIdentity 注入所有 /api/ 请求,而引擎的 /api/query/workflow/list 用 SearchRequest 接收请求,会把 query 参数当作实体 过滤字段解析,直接抛 Field operatorIdentity not found in class hierarchy。 - OperatorIdentity 新增 X-Operator-Identity / X-Identity-Scope / X-Lock-Identity 请求头常量与 pick()(请求头优先、区分「未携带」与「显式空串」) - FlowRecordController、FlowRecordQueryController 身份解析改为请求头优先, 保留 query 读取以兼容旧调用方 - 查询身份范围(identityScope / lockIdentity)同步支持请求头 - 更新 operator-identity 文档与 curl 示例 - 新增 OperatorIdentityHeaderResolveTest 覆盖优先级与非法值拒绝 Closes #301 --- docs/Integration/operator-identity.md | 27 ++++++-- .../flow/operator/OperatorIdentity.java | 36 ++++++++++ .../OperatorIdentityHeaderResolveTest.java | 69 +++++++++++++++++++ .../api/controller/FlowRecordController.java | 19 +++-- .../controller/FlowRecordQueryController.java | 31 +++++++-- 5 files changed, 163 insertions(+), 19 deletions(-) create mode 100644 flow-engine-framework/src/test/java/com/codingapi/flow/operator/OperatorIdentityHeaderResolveTest.java diff --git a/docs/Integration/operator-identity.md b/docs/Integration/operator-identity.md index 409b957c..b978d9d3 100644 --- a/docs/Integration/operator-identity.md +++ b/docs/Integration/operator-identity.md @@ -100,21 +100,36 @@ public IFlowOperator get(long id, String identity) { ### 3.3 请求声明身份 -REST 接口支持 `operatorIdentity` 参数(与 `operatorId` 对称):未传时取当前登录操作者的身份;若同时用 `operatorId` -覆盖了人员,身份归默认身份(避免出现「人员与身份错配」的副本)。 +REST 接口通过**请求头** `X-Operator-Identity` 声明当前身份(issue #301):未传时取当前登录操作者的身份; +若同时用 `operatorId` 覆盖了人员,身份归默认身份(避免出现「人员与身份错配」的副本)。 +显式选择「默认身份」时传空值(`X-Operator-Identity:`),与「未携带」区分。 + +> ⚠️ **不要用 query 参数 `operatorIdentity` 传身份**:引擎的部分查询接口(如 `/api/query/workflow/list`) +> 用 `SearchRequest` 接收请求,会把所有 query 参数当作实体过滤字段解析,多一个 `operatorIdentity` 就会抛 +> `Field operatorIdentity not found in class hierarchy`,页面直接 500。 +> 为兼容旧调用方,服务端仍保留 query 读取(非 `SearchRequest` 接口可用),但请求头优先。 ```bash # 以 department1 的身份发起(身份随记录落库:create_operator_identity) -curl -X POST 'http://host/api/cmd/record/create?operatorIdentity=department1' \ +curl -X POST 'http://host/api/cmd/record/create' \ + -H 'X-Operator-Identity: department1' \ -H 'Content-Type: application/json' -d '{...}' -# 查看待办:默认跨身份,operatorIdentity 只是「本次操作身份」上下文,不影响返回范围 -curl 'http://host/api/query/record/todo?operatorId=1&operatorIdentity=department1' +# 查看待办:默认跨身份,X-Operator-Identity 只是「本次操作身份」上下文,不影响返回范围 +curl 'http://host/api/query/record/todo?operatorId=1' -H 'X-Operator-Identity: department1' # 只想看「落在 department1 下」的待办时,显式锁定身份(见 4.3) -curl 'http://host/api/query/record/todo?operatorId=1&operatorIdentity=department1&identityScope=LOCKED' +curl 'http://host/api/query/record/todo?operatorId=1' \ + -H 'X-Operator-Identity: department1' \ + -H 'X-Identity-Scope: LOCKED' ``` +| 请求头 | 取值 | 说明 | +|---|---|---| +| `X-Operator-Identity` | 身份标识 / 空串 | 当前操作/查看的身份,空串表示显式默认身份 | +| `X-Identity-Scope` | `ALL` / `LOCKED` | 查询范围:默认跨身份,`LOCKED` 只看当前身份 | +| `X-Lock-Identity` | `true` / `false` | `X-Identity-Scope` 的等价简写 | + 编程式 API 中对应各请求对象的 `operatorIdentity` 字段: | 请求对象 | 字段 | diff --git a/flow-engine-framework/src/main/java/com/codingapi/flow/operator/OperatorIdentity.java b/flow-engine-framework/src/main/java/com/codingapi/flow/operator/OperatorIdentity.java index afe2c645..06ff49f4 100644 --- a/flow-engine-framework/src/main/java/com/codingapi/flow/operator/OperatorIdentity.java +++ b/flow-engine-framework/src/main/java/com/codingapi/flow/operator/OperatorIdentity.java @@ -36,9 +36,45 @@ public final class OperatorIdentity { */ public static final String KEY_SEPARATOR = "#"; + /** + * 当前身份的请求头名称(issue #301)。 + * + *
身份是请求上下文,不应放在 query 中:引擎的查询接口用 {@code SearchRequest} 接收请求, + * 会把所有 query 参数当作实体过滤字段解析,多一个 {@code operatorIdentity} 就会抛 + * {@code Field operatorIdentity not found in class hierarchy}。 + * + *
为兼容旧前端与 curl 直连,服务端仍保留 query 读取,但请求头优先。 + */ + public static final String HEADER_OPERATOR_IDENTITY = "X-Operator-Identity"; + + /** + * 查询身份范围的请求头名称,取值 {@code ALL} / {@code LOCKED}(issue #301) + */ + public static final String HEADER_IDENTITY_SCOPE = "X-Identity-Scope"; + + /** + * 是否锁定身份查询的请求头名称,取值 {@code true} / {@code false}(issue #301) + */ + public static final String HEADER_LOCK_IDENTITY = "X-Lock-Identity"; + private OperatorIdentity() { } + /** + * 选取身份原始值:请求头优先,其次请求参数(issue #301)。 + * + * @param headerValue 请求头中的原始值,未携带时为 {@code null} + * @param parameterValue 请求参数中的原始值 + * @param parameterPresent 请求参数是否显式出现(区分「未传」与「传了空串」) + * @return 原始值;两者都未携带时返回 {@code null} + */ + public static String pick(String headerValue, String parameterValue, boolean parameterPresent) { + if (headerValue != null) { + return headerValue; + } + return parameterPresent ? parameterValue : null; + } + /** * 归一化身份标识:null 转为空串,其余原样返回。 * diff --git a/flow-engine-framework/src/test/java/com/codingapi/flow/operator/OperatorIdentityHeaderResolveTest.java b/flow-engine-framework/src/test/java/com/codingapi/flow/operator/OperatorIdentityHeaderResolveTest.java new file mode 100644 index 00000000..a568bc70 --- /dev/null +++ b/flow-engine-framework/src/test/java/com/codingapi/flow/operator/OperatorIdentityHeaderResolveTest.java @@ -0,0 +1,69 @@ +package com.codingapi.flow.operator; + +import com.codingapi.flow.exception.FlowValidationException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * 身份取值优先级测试(issue #301)。 + * + *
背景:身份原先只从 query 参数读取,而引擎查询接口用 {@code SearchRequest} 接收请求, + * 会把 query 参数当成实体过滤字段,导致 + * {@code Field operatorIdentity not found in class hierarchy}。 + * 改为请求头优先,同时保留 query 兼容。 + */ +class OperatorIdentityHeaderResolveTest { + + @Test + void should_prefer_header_over_parameter() { + // given:请求头与 query 同时携带,且取值不同 + String header = "department1"; + String parameter = "department2"; + + // when + String raw = OperatorIdentity.pick(header, parameter, true); + + // then:请求头优先 + assertEquals("department1", OperatorIdentity.validate(raw)); + } + + @Test + void should_fallback_to_parameter_when_header_absent() { + // given:仅 query 携带(旧前端 / curl 直连) + // when + String raw = OperatorIdentity.pick(null, "department1", true); + + // then + assertEquals("department1", OperatorIdentity.validate(raw)); + } + + @Test + void should_return_null_when_neither_present() { + // given / when:两者都未携带,交由调用方回退到登录用户身份 + String raw = OperatorIdentity.pick(null, null, false); + + // then + assertNull(raw); + } + + @Test + void should_treat_empty_header_as_default_identity() { + // given:显式选择「默认身份」时请求头为空串,需要与「未携带」区分 + // when + String raw = OperatorIdentity.pick("", "department1", true); + + // then:空串表示默认身份,不能回退到 query + assertEquals(OperatorIdentity.DEFAULT, OperatorIdentity.validate(raw)); + } + + @Test + void should_reject_invalid_identity_from_header() { + // given:请求头携带非法身份(包含合并键分隔符) + // when / then:入口即拒绝,避免污染落库与缓存键 + String raw = OperatorIdentity.pick("department#1", null, false); + assertThrows(FlowValidationException.class, () -> OperatorIdentity.validate(raw)); + } +} diff --git a/flow-engine-starter-api/src/main/java/com/codingapi/flow/api/controller/FlowRecordController.java b/flow-engine-starter-api/src/main/java/com/codingapi/flow/api/controller/FlowRecordController.java index d54cd745..2455dc2a 100644 --- a/flow-engine-starter-api/src/main/java/com/codingapi/flow/api/controller/FlowRecordController.java +++ b/flow-engine-starter-api/src/main/java/com/codingapi/flow/api/controller/FlowRecordController.java @@ -55,18 +55,25 @@ private long loadCurrentOperatorId() { } /** - * 当前操作人的身份标识(issue #288)。 + * 当前操作人的身份标识(issue #288 / #301)。 * - *
与 {@link #loadCurrentOperatorId()} 对称:优先取请求参数 {@code operatorIdentity}, - * 其次取当前登录操作者自身的身份。身份是会话级信息,因此统一在这里解析后注入各请求对象, - * 避免每个接口各自解析导致行为不一致。 + *
与 {@link #loadCurrentOperatorId()} 对称:优先取请求头 {@code X-Operator-Identity}, + * 其次取请求参数 {@code operatorIdentity}(兼容旧前端与 curl 直连),最后取当前登录操作者自身的身份。 + * 身份是会话级信息,因此统一在这里解析后注入各请求对象,避免每个接口各自解析导致行为不一致。 + * + *
之所以以请求头优先:查询接口用 {@code SearchRequest} 接收请求,会把 query 参数当作实体 + * 过滤字段解析,身份放在 query 会让列表接口直接 500(issue #301)。 */ private String loadCurrentOperatorIdentity() { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); HttpServletRequest request = attributes.getRequest(); - if (request.getParameterMap().containsKey("operatorIdentity")) { + String raw = OperatorIdentity.pick( + request.getHeader(OperatorIdentity.HEADER_OPERATOR_IDENTITY), + request.getParameter("operatorIdentity"), + request.getParameterMap().containsKey("operatorIdentity")); + if (raw != null) { // 入口即校验:长度 / 分隔符 / 控制字符不合法时直接拒绝,避免拖到落库阶段才失败 - return OperatorIdentity.validate(request.getParameter("operatorIdentity")); + return OperatorIdentity.validate(raw); } // 覆盖了 operatorId(联调、无登录态场景)时不再继承登录用户的身份, // 否则会得到「目标人员 + 当前登录人身份」的错配组合(issue #288 评审修复) diff --git a/flow-engine-starter-query/src/main/java/com/codingapi/flow/query/controller/FlowRecordQueryController.java b/flow-engine-starter-query/src/main/java/com/codingapi/flow/query/controller/FlowRecordQueryController.java index 7093e1b5..56269bb0 100644 --- a/flow-engine-starter-query/src/main/java/com/codingapi/flow/query/controller/FlowRecordQueryController.java +++ b/flow-engine-starter-query/src/main/java/com/codingapi/flow/query/controller/FlowRecordQueryController.java @@ -55,10 +55,13 @@ private long loadCurrentOperatorId(){ } /** - * 当前操作人身份标识(issue #288 / #297)。 + * 当前操作人身份标识(issue #288 / #297 / #301)。 * - *
优先取请求参数 {@code operatorIdentity}(宿主声明「我以哪个身份操作/查看」), - * 其次取当前登录操作者自身的身份,都为空时表示默认身份。 + *
优先取请求头 {@code X-Operator-Identity},其次取请求参数 {@code operatorIdentity} + * (兼容旧前端与 curl 直连),最后取当前登录操作者自身的身份,都为空时表示默认身份。 + * + *
以请求头优先的原因:本控制器的列表接口用 {@code SearchRequest} 接收请求,会把 query + * 参数当作实体过滤字段解析,身份放在 query 会让待办/已办/抄送/列表接口直接 500(issue #301)。 * *
该值只作为身份上下文使用:它不会隐式改变查询范围——查询是否按身份过滤由 * {@code identityScope} 显式控制,因此宿主始终携带它也不会导致待办「消失」。 @@ -66,9 +69,13 @@ private long loadCurrentOperatorId(){ private String loadCurrentOperatorIdentity(){ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); HttpServletRequest request = attributes.getRequest(); - if(request.getParameterMap().containsKey("operatorIdentity")) { + String raw = OperatorIdentity.pick( + request.getHeader(OperatorIdentity.HEADER_OPERATOR_IDENTITY), + request.getParameter("operatorIdentity"), + request.getParameterMap().containsKey("operatorIdentity")); + if(raw != null) { // 入口即校验,与命令侧口径一致(issue #288 评审修复) - return OperatorIdentity.validate(request.getParameter("operatorIdentity")); + return OperatorIdentity.validate(raw); } // 覆盖了 operatorId(联调、无登录态场景)时不再继承登录用户的身份,避免人员与身份错配 if(StringUtils.hasText(request.getParameter("operatorId"))) { @@ -95,13 +102,23 @@ private String loadCurrentOperatorIdentity(){ * *
注意:{@code operatorIdentity} 只表示「本次操作/查看的身份上下文」, * 不再隐式决定查询范围。宿主仍然可以一直携带它,不会影响返回结果。 + * + *
参数优先取请求头({@code X-Identity-Scope} / {@code X-Lock-Identity}), + * 避免被 {@code SearchRequest} 当成实体字段解析(issue #301)。 */ private boolean isIdentityLocked() { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); HttpServletRequest request = attributes.getRequest(); - String scope = request.getParameter(IDENTITY_SCOPE_PARAM); + String scope = OperatorIdentity.pick( + request.getHeader(OperatorIdentity.HEADER_IDENTITY_SCOPE), + request.getParameter(IDENTITY_SCOPE_PARAM), + request.getParameterMap().containsKey(IDENTITY_SCOPE_PARAM)); if (!StringUtils.hasText(scope)) { - return "true".equalsIgnoreCase(request.getParameter(LOCK_IDENTITY_PARAM)); + String locked = OperatorIdentity.pick( + request.getHeader(OperatorIdentity.HEADER_LOCK_IDENTITY), + request.getParameter(LOCK_IDENTITY_PARAM), + request.getParameterMap().containsKey(LOCK_IDENTITY_PARAM)); + return "true".equalsIgnoreCase(locked); } String normalized = scope.trim().toUpperCase(); if (IDENTITY_SCOPE_ALL.equals(normalized)) {