feat(gateway): Modeler source 转发+超管门(中文 procDefId 编码硬验收)
阶段1 T1.6 代码部分:流程 Modeler 存量源 XML 经网关超管门载入。
- FlowableClient.procDefSource(procDefId):转发 GET /api/flow/process-definitions/{id}/source,
procDefId 作为 uri 变量由 RestClient 严格编码(中文 percent、冒号→%3A),绝不字符串拼 URL(M-2)。
走既有 execute() 重试/降级封装(连接级重试→503,4xx/5xx 透传)。
- ProcessModelerController GET /api/workflow/process-definitions/{procDefId}/source:
超管门(EngineContextBinder.isSystemAdmin,非超管 403 {status:1,msg:"仅系统管理员可用"}),
过门后透传下游返回体。flowable-service 裸 8089 无安全栈,超管门唯一落点在网关。
- EngineContextBinder.isSystemAdmin 由 private 提升为 public static + null 安全,供网关复用;
判定口径不变(ADMIN 大小写不敏感去补白 / account 大小写敏感=admin / 含 admin 字样不误判)。
测试(mvn -pl platform-flow-gateway,platform-auth -am test 全绿,既有测试零修改):
- FlowableClientTest +2:中文 procDefId「总包合同交底:3:abc123」uri 变量严格编码(断言 %3A 且无裸中文)
/ 下游 4xx 透传 FlowRemoteException 不重试。共 11。
- ProcessModelerControllerTest 4:超管 200 透传 / 非超管 403 不触发下游 / 无身份 403 / 引擎不可达上抛。
- EngineContextBinderAdminTest 8:ADMIN 大小写·补白 / account=admin / Admin 变体不提权 / 含 admin 普通用户 / null。
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -226,6 +226,24 @@ public class FlowableClient {
|
||||
.body(Map.class));
|
||||
}
|
||||
|
||||
/**
|
||||
* 按流程定义 id 取净化源 XML(供 bpmn-js Modeler 在线编辑载入)。对应 flowable-service
|
||||
* {@code GET /api/flow/process-definitions/{procDefId}/source},返回体
|
||||
* {@code {procDefId, found, bpmnXml(净化后), idMap(原id→净化id), procDefKey, version}}。
|
||||
*
|
||||
* <p><b>中文 procDefId 编码</b>:真实 procDefId 形如 {@code 总包合同交底:3:<uuid>}(中文 key + 冒号)。
|
||||
* procDefId 作为 uri 变量传入({@code .uri(template, procDefId)}),由 {@code RestClient} 严格编码
|
||||
* (中文 percent 编码、冒号 → {@code %3A});<b>绝不字符串拼 URL</b>(拼接会把冒号当分隔符裸传,
|
||||
* 且中文可能编码不一致)。
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> procDefSource(String procDefId) {
|
||||
return execute("流程源", () -> restClient.get()
|
||||
.uri("/api/flow/process-definitions/{id}/source", procDefId)
|
||||
.retrieve()
|
||||
.body(Map.class));
|
||||
}
|
||||
|
||||
/** 按发起人查询历史流程实例。对应 flowable-service {@code GET /api/flow/process/started}。 */
|
||||
public List<ProcessInstanceDto> started(String startedBy, int firstResult, int maxResults) {
|
||||
return execute("发起流程列表", () -> restClient.get()
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.eip4.platform.flowgateway.web;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.eip4.platform.auth.security.AuthPrincipal;
|
||||
import com.eip4.platform.auth.security.EngineContextBinder;
|
||||
import com.eip4.platform.flowgateway.client.FlowableClient;
|
||||
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
|
||||
/**
|
||||
* 在线流程 Modeler 网关门面:把 flowable-service 的流程定义/草稿能力经<b>超管门</b>暴露给设计器前端。
|
||||
*
|
||||
* <p>flowable-service 裸 8089 无 Spring Security → 流程编辑/部署面的鉴权唯一落点在本网关层。
|
||||
* 本控制器所有端点先过超管门({@link EngineContextBinder#isSystemAdmin},与全站 {@code requireAdmin}
|
||||
* 逐口径对齐),非超管返回 403,过门后转发 {@link FlowableClient},返回体原样透传。
|
||||
*
|
||||
* <p>阶段 1(T1.6):只读载入存量流程源 XML。草稿 CRUD / 发布门在后续阶段接入本控制器。
|
||||
*/
|
||||
@Tag(name = "流程 Modeler 网关", description = "流程设计器:超管门 + flowable-service 转发(存量源载入/草稿/发布)")
|
||||
@RestController
|
||||
@RequestMapping("/api/workflow")
|
||||
public class ProcessModelerController {
|
||||
|
||||
private final FlowableClient flowableClient;
|
||||
|
||||
public ProcessModelerController(FlowableClient flowableClient) {
|
||||
this.flowableClient = flowableClient;
|
||||
}
|
||||
|
||||
@Operation(summary = "按流程定义 id 取净化源 XML(超管)",
|
||||
description = "超管门后转发 flowable-service /api/flow/process-definitions/{procDefId}/source,"
|
||||
+ "返回 {procDefId, found, bpmnXml(净化后), idMap, procDefKey, version} 供 bpmn-js Modeler 载入。"
|
||||
+ "非超管 403。中文 procDefId 由 FlowableClient 以 uri 变量严格编码转发。")
|
||||
@GetMapping("/process-definitions/{procDefId}/source")
|
||||
public ResponseEntity<Map<String, Object>> source(@AuthenticationPrincipal AuthPrincipal principal,
|
||||
@PathVariable String procDefId) {
|
||||
if (!EngineContextBinder.isSystemAdmin(principal)) {
|
||||
return forbidden();
|
||||
}
|
||||
return ResponseEntity.ok(flowableClient.procDefSource(procDefId));
|
||||
}
|
||||
|
||||
/** 超管门未过:403 + {status:1,msg} 风格对齐既有网关端点。 */
|
||||
private static ResponseEntity<Map<String, Object>> forbidden() {
|
||||
Map<String, Object> body = new LinkedHashMap<>();
|
||||
body.put("status", 1);
|
||||
body.put("msg", "仅系统管理员可用");
|
||||
return ResponseEntity.status(HttpStatus.FORBIDDEN).body(body);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,11 @@ package com.eip4.platform.flowgateway;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.hamcrest.Matchers.allOf;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.endsWith;
|
||||
import static org.hamcrest.Matchers.not;
|
||||
import static org.hamcrest.Matchers.startsWith;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.jsonPath;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
|
||||
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
|
||||
@@ -199,4 +204,51 @@ class FlowableClientTest {
|
||||
assertThat(result).containsEntry("businessKey", "BK-1");
|
||||
server.verify();
|
||||
}
|
||||
|
||||
// ---- 流程源(Modeler 载入):中文 procDefId 编码硬验收(T1.4 评审 M-2) ----
|
||||
|
||||
/**
|
||||
* 真实 procDefId 形如 {@code 总包合同交底:3:<uuid>}(中文 key + 冒号进 path segment)。
|
||||
* procDefId 必须作为 uri 变量被严格编码转发——冒号编码为 {@code %3A}、中文 percent 编码,绝无裸传。
|
||||
* 关键判别:字符串拼接会把冒号当 path 分隔符保留裸 {@code ':'};只有 uri 变量展开才会编码成 {@code %3A}。
|
||||
*/
|
||||
@Test
|
||||
void 取流程源_中文procDefId作为uri变量严格编码不裸传() {
|
||||
FlowGatewayProperties p = props();
|
||||
RestClient.Builder builder = RestClient.builder().baseUrl(p.getBaseUrl());
|
||||
MockRestServiceServer server = MockRestServiceServer.bindTo(builder).build();
|
||||
String procDefId = "总包合同交底:3:abc123";
|
||||
server.expect(ExpectedCount.once(), requestTo(allOf(
|
||||
startsWith("http://localhost:8089/api/flow/process-definitions/"),
|
||||
endsWith("/source"),
|
||||
containsString("%3A3%3Aabc123"), // 两个冒号均编码为 %3A(证明走 uri 变量非拼接)
|
||||
not(containsString("总包合同交底"))))) // 中文未裸传(已 percent 编码)
|
||||
.andExpect(method(HttpMethod.GET))
|
||||
.andRespond(withSuccess("{\"found\":true,\"procDefKey\":\"总包合同交底\",\"version\":3}",
|
||||
MediaType.APPLICATION_JSON));
|
||||
FlowableClient client = new FlowableClient(builder.build(), p);
|
||||
|
||||
Map<String, Object> resp = client.procDefSource(procDefId);
|
||||
|
||||
assertThat(resp).containsEntry("procDefKey", "总包合同交底");
|
||||
assertThat(resp).containsEntry("found", true);
|
||||
server.verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
void 取流程源_下游4xx透传FlowRemote且不重试() {
|
||||
FlowGatewayProperties p = props();
|
||||
RestClient.Builder builder = RestClient.builder().baseUrl(p.getBaseUrl());
|
||||
MockRestServiceServer server = MockRestServiceServer.bindTo(builder).build();
|
||||
server.expect(ExpectedCount.once(), // 业务错不重试
|
||||
requestTo("http://localhost:8089/api/flow/process-definitions/X/source"))
|
||||
.andExpect(method(HttpMethod.GET))
|
||||
.andRespond(withStatus(HttpStatus.BAD_REQUEST).body("bad procDefId"));
|
||||
FlowableClient client = new FlowableClient(builder.build(), p);
|
||||
|
||||
assertThatThrownBy(() -> client.procDefSource("X"))
|
||||
.isInstanceOf(FlowRemoteException.class)
|
||||
.satisfies(e -> assertThat(((FlowRemoteException) e).getStatus().value()).isEqualTo(400));
|
||||
server.verify();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package com.eip4.platform.flowgateway;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
import com.eip4.platform.auth.security.AuthPrincipal;
|
||||
import com.eip4.platform.flowgateway.client.FlowServiceUnavailableException;
|
||||
import com.eip4.platform.flowgateway.client.FlowableClient;
|
||||
import com.eip4.platform.flowgateway.web.ProcessModelerController;
|
||||
|
||||
/**
|
||||
* 流程 Modeler 网关转发单测:超管门 + source 透传 + 引擎不可达上抛(由既有 handler 映射 503)。
|
||||
* 中文 procDefId 全链编码在 {@link FlowableClientTest} 覆盖(客户端层),此处覆盖门控与透传语义。
|
||||
*/
|
||||
class ProcessModelerControllerTest {
|
||||
|
||||
private final FlowableClient flowableClient = mock(FlowableClient.class);
|
||||
private final ProcessModelerController controller = new ProcessModelerController(flowableClient);
|
||||
|
||||
/** 真实形态:中文 key + 冒号分隔的 procDefId。 */
|
||||
private static final String CN_PROCDEF = "总包合同交底:3:abc123";
|
||||
|
||||
private static AuthPrincipal admin() {
|
||||
return new AuthPrincipal("ADMIN", "admin", "管理员", null, null, List.of());
|
||||
}
|
||||
|
||||
/** 含 "admin" 字样但非超管的普通用户(account=user_admin ≠ admin)。 */
|
||||
private static AuthPrincipal normalUser() {
|
||||
return new AuthPrincipal("U1", "user_admin", "张三", "D1", "O1", List.of("R1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void 超管_透传下游净化源XML返回200() {
|
||||
Map<String, Object> downstream = Map.of("found", true, "procDefKey", "总包合同交底",
|
||||
"bpmnXml", "<definitions/>", "idMap", Map.of("总包合同交底", "n1a2b3"), "version", 3);
|
||||
when(flowableClient.procDefSource(CN_PROCDEF)).thenReturn(downstream);
|
||||
|
||||
ResponseEntity<Map<String, Object>> resp = controller.source(admin(), CN_PROCDEF);
|
||||
|
||||
assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(resp.getBody()).isEqualTo(downstream);
|
||||
verify(flowableClient).procDefSource(CN_PROCDEF);
|
||||
}
|
||||
|
||||
@Test
|
||||
void 非超管_403且不触发下游调用() {
|
||||
ResponseEntity<Map<String, Object>> resp = controller.source(normalUser(), CN_PROCDEF);
|
||||
|
||||
assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
|
||||
assertThat(resp.getBody()).containsEntry("status", 1).containsEntry("msg", "仅系统管理员可用");
|
||||
verify(flowableClient, never()).procDefSource(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 无身份_403且不触发下游调用() {
|
||||
ResponseEntity<Map<String, Object>> resp = controller.source(null, CN_PROCDEF);
|
||||
|
||||
assertThat(resp.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
|
||||
assertThat(resp.getBody()).containsEntry("status", 1);
|
||||
verify(flowableClient, never()).procDefSource(anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void 引擎不可达_异常上抛由既有handler映射503() {
|
||||
when(flowableClient.procDefSource(CN_PROCDEF))
|
||||
.thenThrow(new FlowServiceUnavailableException("flowable-service 不可达", null));
|
||||
|
||||
// 控制器不吞异常:FlowServiceUnavailableException 上抛 → FlowGatewayExceptionHandler.onUnavailable 映射 503
|
||||
assertThatThrownBy(() -> controller.source(admin(), CN_PROCDEF))
|
||||
.isInstanceOf(FlowServiceUnavailableException.class);
|
||||
verify(flowableClient).procDefSource(CN_PROCDEF);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user