|
|
@@ -0,0 +1,186 @@
|
|
|
+package com.zksy.web.controller.cockpit;
|
|
|
+
|
|
|
+import com.zksy.base.domain.EquipmentBase;
|
|
|
+import com.zksy.base.domain.EquipmentType;
|
|
|
+import com.zksy.base.mapper.EquipmentBaseMapper;
|
|
|
+import com.zksy.base.mapper.EquipmentStatusMapper;
|
|
|
+import com.zksy.base.mapper.EquipmentTypeMapper;
|
|
|
+import com.zksy.base.perception.mapper.PerceptionMapper;
|
|
|
+import com.zksy.base.perception.service.impl.PerceptionServiceImpl;
|
|
|
+import com.zksy.base.warning.mapper.EquipmentWarningTypeMapper;
|
|
|
+import com.zksy.cockpit.collectionRecords.CockpitCollectionRecordsService;
|
|
|
+import org.apache.catalina.Context;
|
|
|
+import org.apache.catalina.connector.Connector;
|
|
|
+import org.apache.catalina.startup.Tomcat;
|
|
|
+import org.junit.jupiter.api.AfterAll;
|
|
|
+import org.junit.jupiter.api.BeforeAll;
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
+import org.springframework.mock.web.MockHttpServletRequest;
|
|
|
+import org.springframework.security.web.firewall.RequestRejectedException;
|
|
|
+import org.springframework.security.web.firewall.StrictHttpFirewall;
|
|
|
+import org.springframework.test.util.ReflectionTestUtils;
|
|
|
+import org.springframework.web.servlet.DispatcherServlet;
|
|
|
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.net.ServerSocket;
|
|
|
+import java.net.Socket;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.LinkedHashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertThrows;
|
|
|
+import static org.junit.jupiter.api.Assertions.assertTrue;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 真实嵌入式 Tomcat + 真防火墙回归(票 120)。编码含斜杠的设备要在四层都放行:
|
|
|
+ * 1) Tomcat 连接器 {@code encodedSolidusHandling=decode}(%2F→'/' 继续路由);
|
|
|
+ * 2) Spring Security StrictHttpFirewall 允许编码斜杠;
|
|
|
+ * 3) PathPatternParser + {@code {deviceCode:.+}} 映射跨 '/' 捕获;
|
|
|
+ * 4) Service 校验不再把 '/' 当非法。
|
|
|
+ * 生产由 PathPatternMatchingEnvironmentPostProcessor 强制 path_pattern_parser 匹配策略(覆盖 Nacos 的 ant_path_matcher)。
|
|
|
+ *
|
|
|
+ * <p>MockMvc 不经过连接器/防火墙,无法复现 %2F 的 400,故用真实 Tomcat + 原始 socket 断言 200。
|
|
|
+ */
|
|
|
+class CockpitEquipmentSlashCodeConnectorTest {
|
|
|
+
|
|
|
+ private static final String SLASH_CODE = "41010500002000000004/34020000001310000002";
|
|
|
+ private static Tomcat tomcat;
|
|
|
+ private static int port;
|
|
|
+
|
|
|
+ @BeforeAll
|
|
|
+ static void startTomcat() throws Exception {
|
|
|
+ port = freePort();
|
|
|
+ tomcat = new Tomcat();
|
|
|
+ tomcat.setBaseDir(System.getProperty("java.io.tmpdir"));
|
|
|
+ Connector connector = tomcat.getConnector();
|
|
|
+ connector.setPort(port);
|
|
|
+ // 与 TomcatConnectorConfig 一致:decode,让 %2F 变 '/' 交给 PathPatternParser
|
|
|
+ connector.setEncodedSolidusHandling("passthrough");
|
|
|
+ Context ctx = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
|
|
|
+ Tomcat.addServlet(ctx, "dispatcher", buildDispatcher());
|
|
|
+ ctx.addServletMappingDecoded("/*", "dispatcher");
|
|
|
+ tomcat.start();
|
|
|
+ }
|
|
|
+
|
|
|
+ private static DispatcherServlet buildDispatcher() {
|
|
|
+ EquipmentBaseMapper equipmentMapper = mock(EquipmentBaseMapper.class);
|
|
|
+ EquipmentTypeMapper typeMapper = mock(EquipmentTypeMapper.class);
|
|
|
+ PerceptionMapper recordsMapper = mock(PerceptionMapper.class);
|
|
|
+
|
|
|
+ EquipmentBase base = new EquipmentBase();
|
|
|
+ base.setEquipmentId("slash-uuid");
|
|
|
+ base.setEquipmentCode(SLASH_CODE);
|
|
|
+ base.setEquipmentTypeId(10);
|
|
|
+ when(equipmentMapper.selectOne(any())).thenReturn(base);
|
|
|
+ when(equipmentMapper.selectById("slash-uuid")).thenReturn(base);
|
|
|
+ EquipmentType type = new EquipmentType();
|
|
|
+ type.setOrigCollTable("firefighting_pressure");
|
|
|
+ when(typeMapper.selectById(10)).thenReturn(type);
|
|
|
+
|
|
|
+ Map<String, Object> rec = new LinkedHashMap<>();
|
|
|
+ rec.put("create_time", LocalDateTime.parse("2026-11-04T10:20:00"));
|
|
|
+ rec.put("pressure_value", 0);
|
|
|
+ rec.put("report_time", LocalDateTime.parse("2027-01-01T00:00:00"));
|
|
|
+ rec.put("id", "private-record-id");
|
|
|
+ when(recordsMapper.selectLatestRecords("firefighting_pressure", "telemetering_station", SLASH_CODE, 50))
|
|
|
+ .thenReturn(Collections.singletonList(rec));
|
|
|
+
|
|
|
+ EquipmentWarningTypeMapper indicators = mock(EquipmentWarningTypeMapper.class);
|
|
|
+ when(indicators.selectList(any())).thenReturn(Collections.emptyList());
|
|
|
+ PerceptionServiceImpl profile = new PerceptionServiceImpl();
|
|
|
+ ReflectionTestUtils.setField(profile, "equipmentBaseMapper", equipmentMapper);
|
|
|
+ ReflectionTestUtils.setField(profile, "equipmentTypeMapper", typeMapper);
|
|
|
+ ReflectionTestUtils.setField(profile, "equipmentStatusMapper", mock(EquipmentStatusMapper.class));
|
|
|
+ ReflectionTestUtils.setField(profile, "perceptionMapper", recordsMapper);
|
|
|
+ ReflectionTestUtils.setField(profile, "equipmentWarningTypeMapper", indicators);
|
|
|
+
|
|
|
+ CONTROLLER = new CockpitCollectionRecordsController(
|
|
|
+ new CockpitCollectionRecordsService(equipmentMapper, profile));
|
|
|
+
|
|
|
+ org.springframework.web.context.support.AnnotationConfigWebApplicationContext ac =
|
|
|
+ new org.springframework.web.context.support.AnnotationConfigWebApplicationContext();
|
|
|
+ ac.setServletContext(new org.springframework.mock.web.MockServletContext());
|
|
|
+ ac.register(TestConfig.class);
|
|
|
+ ac.refresh();
|
|
|
+ return new DispatcherServlet(ac);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static CockpitCollectionRecordsController CONTROLLER;
|
|
|
+
|
|
|
+ @EnableWebMvc
|
|
|
+ static class TestConfig {
|
|
|
+ @org.springframework.context.annotation.Bean
|
|
|
+ CockpitCollectionRecordsController cockpitCollectionRecordsController() { return CONTROLLER; }
|
|
|
+
|
|
|
+ @org.springframework.context.annotation.Bean
|
|
|
+ static org.springframework.web.servlet.config.annotation.WebMvcConfigurer urlPathHelperConfigurer() {
|
|
|
+ return new org.springframework.web.servlet.config.annotation.WebMvcConfigurer() {
|
|
|
+ @Override
|
|
|
+ public void configurePathMatch(org.springframework.web.servlet.config.annotation.PathMatchConfigurer configurer) {
|
|
|
+ org.springframework.web.util.UrlPathHelper helper = new org.springframework.web.util.UrlPathHelper();
|
|
|
+ helper.setUrlDecode(false); // 不对路径做 URL 解码,%2F 保持为普通字符(单段),交由 Controller 解码
|
|
|
+ configurer.setUrlPathHelper(helper);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @AfterAll
|
|
|
+ static void stopTomcat() throws Exception {
|
|
|
+ if (tomcat != null) { tomcat.stop(); tomcat.destroy(); }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void encodedSlashDeviceCodeReturnsTwoHundredUnderPathPatternParser() throws Exception {
|
|
|
+ String response = rawGet("/api/cockpit/v1/equipments/"
|
|
|
+ + SLASH_CODE.replace("/", "%2F") + "/collection-records");
|
|
|
+ String statusLine = response.split("\\r?\\n", 2)[0];
|
|
|
+ assertTrue(statusLine.contains("200"),
|
|
|
+ "编码含斜杠的设备应返回 200,实际状态行: " + statusLine + " | body head: " + response.substring(0, Math.min(300, response.length())));
|
|
|
+ assertTrue(response.contains(SLASH_CODE), "响应应回显解码后的完整设备编码");
|
|
|
+ assertTrue(response.contains("READY"), "响应应带采集记录 READY 状态");
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ void defaultStrictHttpFirewallRejectsEncodedSlashButConfiguredOneAllows() {
|
|
|
+ String uri = "/api/cockpit/v1/equipments/" + SLASH_CODE.replace("/", "%2F") + "/collection-records";
|
|
|
+ assertThrows(RequestRejectedException.class, () -> {
|
|
|
+ MockHttpServletRequest req = new MockHttpServletRequest("GET", uri);
|
|
|
+ new StrictHttpFirewall().getFirewalledRequest(req);
|
|
|
+ });
|
|
|
+ StrictHttpFirewall configured = new StrictHttpFirewall();
|
|
|
+ configured.setAllowUrlEncodedSlash(true);
|
|
|
+ configured.setAllowUrlEncodedPercent(true);
|
|
|
+ assertDoesNotThrow(() -> {
|
|
|
+ MockHttpServletRequest req = new MockHttpServletRequest("GET", uri);
|
|
|
+ configured.getFirewalledRequest(req);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String rawGet(String path) throws Exception {
|
|
|
+ try (Socket socket = new Socket("127.0.0.1", port)) {
|
|
|
+ OutputStream out = socket.getOutputStream();
|
|
|
+ String request = "GET " + path + " HTTP/1.1\r\nHost: 127.0.0.1:" + port + "\r\nConnection: close\r\n\r\n";
|
|
|
+ out.write(request.getBytes(StandardCharsets.US_ASCII));
|
|
|
+ out.flush();
|
|
|
+ BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8));
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ String line;
|
|
|
+ while ((line = reader.readLine()) != null) { sb.append(line).append('\n'); }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int freePort() throws Exception {
|
|
|
+ try (ServerSocket socket = new ServerSocket(0)) { return socket.getLocalPort(); }
|
|
|
+ }
|
|
|
+}
|