租户改为从header获取

This commit is contained in:
zy 2026-03-18 09:39:36 +08:00
parent c624f5dd40
commit 3029e6724e
7 changed files with 151 additions and 18 deletions

View File

@ -5,16 +5,17 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils; import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.config.TenantContext; import org.jeecg.common.config.TenantContext;
import org.jeecg.common.constant.CommonConstants;
import org.jeecg.common.constant.PollutionConstants; import org.jeecg.common.constant.PollutionConstants;
import org.jeecg.common.constant.enums.PollutionEnum; import org.jeecg.common.constant.enums.PollutionEnum;
import org.jeecg.common.dto.CommonDTO; import org.jeecg.common.dto.CommonDTO;
import org.jeecg.common.entity.*; import org.jeecg.common.entity.*;
import org.jeecg.common.tenant.TenantContextHolder;
import org.jeecg.common.vo.VOHisResult; import org.jeecg.common.vo.VOHisResult;
import org.jeecg.modules.appmana.service.ISurvHisdataAirService; import org.jeecg.modules.appmana.service.ISurvHisdataAirService;
import org.jeecg.modules.appmana.service.ISurvHisdataLivestockwaterService; import org.jeecg.modules.appmana.service.ISurvHisdataLivestockwaterService;
import org.jeecg.modules.appmana.service.ISurvHisdataOrientwaterService; import org.jeecg.modules.appmana.service.ISurvHisdataOrientwaterService;
import org.jeecg.modules.appmana.service.ISurvHisdataSoilService; import org.jeecg.modules.appmana.service.ISurvHisdataSoilService;
import org.jeecg.modules.appmana.utils.HttpServletRequestUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -39,7 +40,7 @@ public class CommonServiceImpl {
@Autowired @Autowired
@Lazy @Lazy
private SurvConfigServiceImpl configService; private SurvConfigServiceImpl configService;
private static String defaultTenant = "1000";
/** /**
* 获取监测设备的数据 * 获取监测设备的数据
*/ */
@ -163,28 +164,51 @@ public class CommonServiceImpl {
return configService.getCacheConfig(tenantId, configType); 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){ public String getTenantId(String authId){
String tenantId = ""; String tenantId = "";
if(StringUtils.isNotBlank(authId)){ if(StringUtils.isNotBlank(authId)){
tenantId = authId; tenantId = authId;
}else{ }else{
String headerTenantId = TenantContext.getTenant(); tenantId = CommonConstants.defaultTenant;
log.error("==============tenantCheck==========="+headerTenantId);
if(StringUtils.isNotBlank(headerTenantId)){
tenantId = headerTenantId;
}else{
tenantId = defaultTenant;
}
} }
TenantContext.setTenant(tenantId);//设置当前租户 TenantContext.setTenant(tenantId);//设置当前租户
return tenantId; return tenantId;
} }
public void processTenant(CommonDTO commonDTO) { public void processTenant(CommonDTO commonDTO) {
if(commonDTO!=null){ String tenantId = HttpServletRequestUtil.getRequestInfo(CommonConstants.HEADER_TENANT_ID);
if(StringUtils.isNotBlank(tenantId)){
getTenantId(commonDTO.getAuthId()); getTenantId(commonDTO.getAuthId());
}else{ }else{
TenantContext.setTenant(defaultTenant);//设置当前租户 TenantContext.setTenant(CommonConstants.defaultTenant);//设置当前租户
} }
} }

View File

@ -149,4 +149,16 @@ public class HttpServletRequestUtil {
}); });
return parametersList; return parametersList;
} }
/**
* 获取某个头信息
*
* @return
*/
public static String getRequestInfo(String headerName) {
HttpServletRequest request = getRequest();
String headerInfo = request.getHeader(headerName);
return headerInfo;
}
} }

View File

@ -593,8 +593,4 @@ public class SurvStationInfoController {
return R.ok(job); return R.ok(job);
} }
} }

View File

@ -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;
}
}

View File

@ -1,15 +1,27 @@
package org.jeecg.system.applet.util; package org.jeecg.system.applet.util;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.constant.CommonConstants;
public class TenantUtil { 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){ public static String getCurTenant(String tenantId){
String tenant = ""; String tenant = "";
if(StringUtils.isNotBlank(tenantId) && !"0".equals(tenantId)){ String curTenant = HeaderUtils.getHeader(CommonConstants.HEADER_TENANT_ID);
tenant = tenantId; if(StringUtils.isNotBlank(curTenant) && !"0".equals(curTenant)){
tenant = curTenant;
}else{ }else{
tenant = "1000";//默认汾西 tenant = CommonConstants.defaultTenant;//默认汾西
} }
return tenant; return tenant;
} }

View File

@ -490,4 +490,7 @@ public interface CommonConstant {
* 是叶子节点 * 是叶子节点
*/ */
Integer IS_LEAF = 1; Integer IS_LEAF = 1;
} }

View File

@ -154,4 +154,12 @@ public interface CommonConstants {
String USER_TYPE_S = "-1"; String USER_TYPE_S = "-1";
String USER_TYPE_1 = "1"; String USER_TYPE_1 = "1";
String USER_TYPE_2 = "2"; String USER_TYPE_2 = "2";
/**
* 默认租户
*/
String defaultTenant = "1000";
/** 租户请求头 更名为X-Tenant-Id */
String HEADER_TENANT_ID = "X-Tenant-Id";
} }