From 3029e6724e0db3cbf5660e5dbe591097a8005a66 Mon Sep 17 00:00:00 2001 From: zy <82248909@qq.com> Date: Wed, 18 Mar 2026 09:39:36 +0800 Subject: [PATCH] =?UTF-8?q?=E7=A7=9F=E6=88=B7=E6=94=B9=E4=B8=BA=E4=BB=8Ehe?= =?UTF-8?q?ader=E8=8E=B7=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/CommonServiceImpl.java | 46 ++++++++--- .../appmana/utils/HttpServletRequestUtil.java | 12 +++ .../controller/SurvStationInfoController.java | 4 - .../jeecg/system/applet/util/HeaderUtils.java | 78 +++++++++++++++++++ .../jeecg/system/applet/util/TenantUtil.java | 18 ++++- .../jeecg/common/constant/CommonConstant.java | 3 + .../common/constant/CommonConstants.java | 8 ++ 7 files changed, 151 insertions(+), 18 deletions(-) create mode 100644 zh-module-applet/zh-applet-api/src/main/java/org/jeecg/system/applet/util/HeaderUtils.java diff --git a/zh-module-applet/zh-applet-admin/src/main/java/org/jeecg/modules/appmana/service/impl/CommonServiceImpl.java b/zh-module-applet/zh-applet-admin/src/main/java/org/jeecg/modules/appmana/service/impl/CommonServiceImpl.java index c2c4b87..b2b242e 100644 --- a/zh-module-applet/zh-applet-admin/src/main/java/org/jeecg/modules/appmana/service/impl/CommonServiceImpl.java +++ b/zh-module-applet/zh-applet-admin/src/main/java/org/jeecg/modules/appmana/service/impl/CommonServiceImpl.java @@ -5,16 +5,17 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.lang3.StringUtils; import org.jeecg.common.config.TenantContext; +import org.jeecg.common.constant.CommonConstants; import org.jeecg.common.constant.PollutionConstants; import org.jeecg.common.constant.enums.PollutionEnum; import org.jeecg.common.dto.CommonDTO; import org.jeecg.common.entity.*; -import org.jeecg.common.tenant.TenantContextHolder; import org.jeecg.common.vo.VOHisResult; import org.jeecg.modules.appmana.service.ISurvHisdataAirService; import org.jeecg.modules.appmana.service.ISurvHisdataLivestockwaterService; import org.jeecg.modules.appmana.service.ISurvHisdataOrientwaterService; import org.jeecg.modules.appmana.service.ISurvHisdataSoilService; +import org.jeecg.modules.appmana.utils.HttpServletRequestUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Lazy; import org.springframework.stereotype.Service; @@ -39,7 +40,7 @@ public class CommonServiceImpl { @Autowired @Lazy private SurvConfigServiceImpl configService; - private static String defaultTenant = "1000"; + /** * 获取监测设备的数据 */ @@ -163,28 +164,51 @@ public class CommonServiceImpl { return configService.getCacheConfig(tenantId, configType); } +// public String getTenantId(String authId){ +// String tenantId = ""; +// if(StringUtils.isNotBlank(authId)){ +// tenantId = authId; +// }else{ +// String headerTenantId = TenantContext.getTenant(); +// log.error("==============tenantCheck==========="+headerTenantId); +// if(StringUtils.isNotBlank(headerTenantId)){ +// tenantId = headerTenantId; +// }else{ +// tenantId = defaultTenant; +// } +// } +// TenantContext.setTenant(tenantId);//设置当前租户 +// return tenantId; +// } +// +// public void processTenant(CommonDTO commonDTO) { +// if(commonDTO!=null){ +// getTenantId(commonDTO.getAuthId()); +// }else{ +// TenantContext.setTenant(defaultTenant);//设置当前租户 +// } +// +// } + + public String getTenantId(String authId){ String tenantId = ""; if(StringUtils.isNotBlank(authId)){ tenantId = authId; }else{ - String headerTenantId = TenantContext.getTenant(); - log.error("==============tenantCheck==========="+headerTenantId); - if(StringUtils.isNotBlank(headerTenantId)){ - tenantId = headerTenantId; - }else{ - tenantId = defaultTenant; - } + tenantId = CommonConstants.defaultTenant; } TenantContext.setTenant(tenantId);//设置当前租户 return tenantId; } public void processTenant(CommonDTO commonDTO) { - if(commonDTO!=null){ + String tenantId = HttpServletRequestUtil.getRequestInfo(CommonConstants.HEADER_TENANT_ID); + + if(StringUtils.isNotBlank(tenantId)){ getTenantId(commonDTO.getAuthId()); }else{ - TenantContext.setTenant(defaultTenant);//设置当前租户 + TenantContext.setTenant(CommonConstants.defaultTenant);//设置当前租户 } } diff --git a/zh-module-applet/zh-applet-admin/src/main/java/org/jeecg/modules/appmana/utils/HttpServletRequestUtil.java b/zh-module-applet/zh-applet-admin/src/main/java/org/jeecg/modules/appmana/utils/HttpServletRequestUtil.java index 657f11e..a391225 100644 --- a/zh-module-applet/zh-applet-admin/src/main/java/org/jeecg/modules/appmana/utils/HttpServletRequestUtil.java +++ b/zh-module-applet/zh-applet-admin/src/main/java/org/jeecg/modules/appmana/utils/HttpServletRequestUtil.java @@ -149,4 +149,16 @@ public class HttpServletRequestUtil { }); return parametersList; } + + /** + * 获取某个头信息 + * + * @return + */ + public static String getRequestInfo(String headerName) { + HttpServletRequest request = getRequest(); + String headerInfo = request.getHeader(headerName); + return headerInfo; + + } } \ No newline at end of file diff --git a/zh-module-applet/zh-applet-api/src/main/java/org/jeecg/system/applet/controller/SurvStationInfoController.java b/zh-module-applet/zh-applet-api/src/main/java/org/jeecg/system/applet/controller/SurvStationInfoController.java index e3cea6e..181d3e4 100644 --- a/zh-module-applet/zh-applet-api/src/main/java/org/jeecg/system/applet/controller/SurvStationInfoController.java +++ b/zh-module-applet/zh-applet-api/src/main/java/org/jeecg/system/applet/controller/SurvStationInfoController.java @@ -593,8 +593,4 @@ public class SurvStationInfoController { return R.ok(job); } - - - - } diff --git a/zh-module-applet/zh-applet-api/src/main/java/org/jeecg/system/applet/util/HeaderUtils.java b/zh-module-applet/zh-applet-api/src/main/java/org/jeecg/system/applet/util/HeaderUtils.java new file mode 100644 index 0000000..e0f130c --- /dev/null +++ b/zh-module-applet/zh-applet-api/src/main/java/org/jeecg/system/applet/util/HeaderUtils.java @@ -0,0 +1,78 @@ +package org.jeecg.system.applet.util; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; + +import javax.servlet.http.HttpServletRequest; + +@Slf4j +public class HeaderUtils { + + /** + * 获取当前请求的 Header 值 + */ + public static String getHeader(String headerName) { + // 获取当前请求的属性 + ServletRequestAttributes attributes = (ServletRequestAttributes) + RequestContextHolder.getRequestAttributes(); + + if (attributes == null) { + return null; + } + + // 获取 HttpServletRequest + HttpServletRequest request = attributes.getRequest(); + return request.getHeader(headerName); + } + + /** + * 获取 User-Agent + */ + public static String getUserAgent() { + return getHeader("User-Agent"); + } + + /** + * 获取 Authorization Token + */ + public static String getToken() { + String auth = getHeader("Authorization"); + if (auth != null && auth.startsWith("Bearer ")) { + return auth.substring(7); + } + return auth; + } + + /** + * 获取客户端 IP + */ + public static String getClientIp() { + ServletRequestAttributes attributes = (ServletRequestAttributes) + RequestContextHolder.getRequestAttributes(); + + if (attributes == null) { + return null; + } + + HttpServletRequest request = attributes.getRequest(); + String ip = request.getHeader("X-Forwarded-For"); + + if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("Proxy-Client-IP"); + } + if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { + ip = request.getHeader("WL-Proxy-Client-IP"); + } + if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { + ip = request.getRemoteAddr(); + } + + // 多个代理的情况,取第一个 + if (ip != null && ip.contains(",")) { + ip = ip.split(",")[0].trim(); + } + + return ip; + } +} diff --git a/zh-module-applet/zh-applet-api/src/main/java/org/jeecg/system/applet/util/TenantUtil.java b/zh-module-applet/zh-applet-api/src/main/java/org/jeecg/system/applet/util/TenantUtil.java index ae9def7..da5e4a6 100644 --- a/zh-module-applet/zh-applet-api/src/main/java/org/jeecg/system/applet/util/TenantUtil.java +++ b/zh-module-applet/zh-applet-api/src/main/java/org/jeecg/system/applet/util/TenantUtil.java @@ -1,15 +1,27 @@ package org.jeecg.system.applet.util; import org.apache.commons.lang3.StringUtils; +import org.jeecg.common.constant.CommonConstants; public class TenantUtil { +// public static String getCurTenant(String tenantId){ +// String tenant = ""; +// if(StringUtils.isNotBlank(tenantId) && !"0".equals(tenantId)){ +// tenant = tenantId; +// }else{ +// tenant = "1000";//默认汾西 +// } +// return tenant; +// } + public static String getCurTenant(String tenantId){ String tenant = ""; - if(StringUtils.isNotBlank(tenantId) && !"0".equals(tenantId)){ - tenant = tenantId; + String curTenant = HeaderUtils.getHeader(CommonConstants.HEADER_TENANT_ID); + if(StringUtils.isNotBlank(curTenant) && !"0".equals(curTenant)){ + tenant = curTenant; }else{ - tenant = "1000";//默认汾西 + tenant = CommonConstants.defaultTenant;//默认汾西 } return tenant; } diff --git a/zh-module-applet/zh-applet-common/src/main/java/org/jeecg/common/constant/CommonConstant.java b/zh-module-applet/zh-applet-common/src/main/java/org/jeecg/common/constant/CommonConstant.java index f263753..3a5e246 100644 --- a/zh-module-applet/zh-applet-common/src/main/java/org/jeecg/common/constant/CommonConstant.java +++ b/zh-module-applet/zh-applet-common/src/main/java/org/jeecg/common/constant/CommonConstant.java @@ -490,4 +490,7 @@ public interface CommonConstant { * 是叶子节点 */ Integer IS_LEAF = 1; + + + } diff --git a/zh-module-applet/zh-applet-common/src/main/java/org/jeecg/common/constant/CommonConstants.java b/zh-module-applet/zh-applet-common/src/main/java/org/jeecg/common/constant/CommonConstants.java index b0af8f2..b6bddf3 100644 --- a/zh-module-applet/zh-applet-common/src/main/java/org/jeecg/common/constant/CommonConstants.java +++ b/zh-module-applet/zh-applet-common/src/main/java/org/jeecg/common/constant/CommonConstants.java @@ -154,4 +154,12 @@ public interface CommonConstants { String USER_TYPE_S = "-1"; String USER_TYPE_1 = "1"; String USER_TYPE_2 = "2"; + + /** + * 默认租户 + */ + String defaultTenant = "1000"; + + /** 租户请求头 更名为:X-Tenant-Id */ + String HEADER_TENANT_ID = "X-Tenant-Id"; }