From 5ebbb13a511518338d2f25eaeddef7c3cd2dd091 Mon Sep 17 00:00:00 2001 From: zhangjf Date: Tue, 17 Feb 2026 16:12:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DAPI=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=B8=AD=E5=8F=91=E7=8E=B0=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 问题修复 ### 1. 上下文拦截器 - ContextInterceptor: 从HTTP Header提取租户ID和用户ID到ThreadLocal - WebMvcConfig: 注册拦截器到Spring MVC ### 2. 数据库配置 - fund-cust/application.yml: 修复MySQL密码默认值 - fund-proj/application.yml: 修复MySQL密码默认值 ### 3. Gateway配置 - application.yml: 删除空的Sentinel datasource配置 - SentinelRuleConfig: 删除重复的sentinelGatewayFilter Bean ### 4. 数据库表 - requirement.sql: 修复主键列名为id,与BaseEntity保持一致 ### 5. MyBatis-Plus依赖 - fund-cust/pom.xml: 使用mybatis-plus-spring-boot3-starter - fund-proj/pom.xml: 使用mybatis-plus-spring-boot3-starter --- doc/requirement.sql | 4 +- .../common/config/WebMvcConfig.java | 25 +++++++++ .../common/web/ContextInterceptor.java | 53 +++++++++++++++++++ fund-cust/src/main/resources/application.yml | 2 +- .../gateway/config/SentinelRuleConfig.java | 14 ----- .../src/main/resources/application.yml | 8 --- fund-proj/src/main/resources/application.yml | 2 +- 7 files changed, 82 insertions(+), 26 deletions(-) create mode 100644 fund-common/src/main/java/com/fundplatform/common/config/WebMvcConfig.java create mode 100644 fund-common/src/main/java/com/fundplatform/common/web/ContextInterceptor.java diff --git a/doc/requirement.sql b/doc/requirement.sql index 8a3fd2b..5147621 100644 --- a/doc/requirement.sql +++ b/doc/requirement.sql @@ -1,6 +1,6 @@ -- 需求工单表 CREATE TABLE IF NOT EXISTS `requirement` ( - `requirement_id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键,需求ID', + `id` BIGINT NOT NULL AUTO_INCREMENT COMMENT '主键,需求ID', `tenant_id` BIGINT NOT NULL COMMENT '租户ID', `requirement_code` VARCHAR(50) NOT NULL COMMENT '需求编号', `requirement_name` VARCHAR(200) NOT NULL COMMENT '需求名称', @@ -25,7 +25,7 @@ CREATE TABLE IF NOT EXISTS `requirement` ( `updated_by` BIGINT COMMENT '更新人ID', `updated_time` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', `deleted` TINYINT DEFAULT 0 COMMENT '逻辑删除:0-未删除,1-已删除', - PRIMARY KEY (`requirement_id`), + PRIMARY KEY (`id`), UNIQUE KEY `uk_tenant_code` (`tenant_id`, `requirement_code`), INDEX `idx_tenant` (`tenant_id`), INDEX `idx_project` (`project_id`), diff --git a/fund-common/src/main/java/com/fundplatform/common/config/WebMvcConfig.java b/fund-common/src/main/java/com/fundplatform/common/config/WebMvcConfig.java new file mode 100644 index 0000000..3020c67 --- /dev/null +++ b/fund-common/src/main/java/com/fundplatform/common/config/WebMvcConfig.java @@ -0,0 +1,25 @@ +package com.fundplatform.common.config; + +import com.fundplatform.common.web.ContextInterceptor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +/** + * Web MVC配置 + * 注册拦截器 + */ +@Configuration +public class WebMvcConfig implements WebMvcConfigurer { + + @Autowired + private ContextInterceptor contextInterceptor; + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(contextInterceptor) + .addPathPatterns("/api/**") + .excludePathPatterns("/actuator/**", "/error"); + } +} diff --git a/fund-common/src/main/java/com/fundplatform/common/web/ContextInterceptor.java b/fund-common/src/main/java/com/fundplatform/common/web/ContextInterceptor.java new file mode 100644 index 0000000..784bbf7 --- /dev/null +++ b/fund-common/src/main/java/com/fundplatform/common/web/ContextInterceptor.java @@ -0,0 +1,53 @@ +package com.fundplatform.common.web; + +import com.fundplatform.common.context.TenantContextHolder; +import com.fundplatform.common.context.UserContextHolder; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +/** + * 租户和用户上下文拦截器 + * 从HTTP Header中提取租户ID和用户ID,设置到ThreadLocal中 + */ +@Component +public class ContextInterceptor implements HandlerInterceptor { + + public static final String HEADER_TENANT_ID = "X-Tenant-Id"; + public static final String HEADER_USER_ID = "X-User-Id"; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { + // 提取租户ID + String tenantIdStr = request.getHeader(HEADER_TENANT_ID); + if (tenantIdStr != null && !tenantIdStr.isEmpty()) { + try { + Long tenantId = Long.parseLong(tenantIdStr); + TenantContextHolder.setTenantId(tenantId); + } catch (NumberFormatException e) { + // 忽略无效的租户ID + } + } + + // 提取用户ID + String userIdStr = request.getHeader(HEADER_USER_ID); + if (userIdStr != null && !userIdStr.isEmpty()) { + try { + Long userId = Long.parseLong(userIdStr); + UserContextHolder.setUserId(userId); + } catch (NumberFormatException e) { + // 忽略无效的用户ID + } + } + + return true; + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) { + // 清理ThreadLocal,防止内存泄漏 + TenantContextHolder.clear(); + UserContextHolder.clear(); + } +} diff --git a/fund-cust/src/main/resources/application.yml b/fund-cust/src/main/resources/application.yml index ddd3b3e..3db834f 100644 --- a/fund-cust/src/main/resources/application.yml +++ b/fund-cust/src/main/resources/application.yml @@ -16,7 +16,7 @@ spring: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/fund_cust?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai username: root - password: ${DB_PASSWORD:123456} + password: ${DB_PASSWORD:zjf@123456} hikari: maximum-pool-size: 10 minimum-idle: 5 diff --git a/fund-gateway/src/main/java/com/fundplatform/gateway/config/SentinelRuleConfig.java b/fund-gateway/src/main/java/com/fundplatform/gateway/config/SentinelRuleConfig.java index dbc4b8e..34758fa 100644 --- a/fund-gateway/src/main/java/com/fundplatform/gateway/config/SentinelRuleConfig.java +++ b/fund-gateway/src/main/java/com/fundplatform/gateway/config/SentinelRuleConfig.java @@ -2,13 +2,8 @@ package com.fundplatform.gateway.config; import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule; import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager; -import com.alibaba.csp.sentinel.adapter.gateway.sc.SentinelGatewayFilter; import jakarta.annotation.PostConstruct; -import org.springframework.cloud.gateway.filter.GlobalFilter; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import org.springframework.core.Ordered; -import org.springframework.core.annotation.Order; import java.util.HashSet; import java.util.Set; @@ -54,13 +49,4 @@ public class SentinelRuleConfig { // 加载规则 GatewayRuleManager.loadRules(rules); } - - /** - * Sentinel Gateway 过滤器 - */ - @Bean - @Order(Ordered.HIGHEST_PRECEDENCE) - public GlobalFilter sentinelGatewayFilter() { - return new SentinelGatewayFilter(); - } } diff --git a/fund-gateway/src/main/resources/application.yml b/fund-gateway/src/main/resources/application.yml index aaea386..8fbe6ec 100644 --- a/fund-gateway/src/main/resources/application.yml +++ b/fund-gateway/src/main/resources/application.yml @@ -23,14 +23,6 @@ spring: dashboard: localhost:8080 # Sentinel Dashboard地址(可选) port: 8719 # Sentinel客户端端口 eager: true # 服务启动时立即初始化 - datasource: - # 从Nacos读取规则(可选) - # ds1: - # nacos: - # server-addr: localhost:8848 - # data-id: sentinel-gateway-rules - # group-id: DEFAULT_GROUP - # rule-type: gw-flow gateway: # 默认限流配置 diff --git a/fund-proj/src/main/resources/application.yml b/fund-proj/src/main/resources/application.yml index 00c2da0..7090d56 100644 --- a/fund-proj/src/main/resources/application.yml +++ b/fund-proj/src/main/resources/application.yml @@ -16,7 +16,7 @@ spring: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/fund_proj?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai username: root - password: ${DB_PASSWORD:123456} + password: ${DB_PASSWORD:zjf@123456} hikari: maximum-pool-size: 10 minimum-idle: 5