From 718a587c1905a761591b02cd7f54fe3448aa70e3 Mon Sep 17 00:00:00 2001 From: PIG AI Date: Fri, 6 Jun 2025 09:27:47 +0800 Subject: [PATCH] =?UTF-8?q?feat(tenant):=20=E5=A2=9E=E5=BC=BA=E7=A7=9F?= =?UTF-8?q?=E6=88=B7=E7=8A=B6=E6=80=81=E6=A3=80=E6=9F=A5=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增对租户有效期的检查,包括startTime和endTime验证 - 优化无效租户的响应方式,返回401状态码和JSON格式错误信息 - 使用JSONUtil和ContentType处理错误响应输出 - 添加@SneakyThrows注解处理潜在异常 --- .../tenant/TenantContextHolderFilter.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/tenant/TenantContextHolderFilter.java b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/tenant/TenantContextHolderFilter.java index cb9196f54..6156f0b3f 100644 --- a/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/tenant/TenantContextHolderFilter.java +++ b/pigx-common/pigx-common-data/src/main/java/com/pig4cloud/pigx/common/data/tenant/TenantContextHolderFilter.java @@ -20,9 +20,12 @@ package com.pig4cloud.pigx.common.data.tenant; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.util.NumberUtil; import cn.hutool.core.util.StrUtil; +import cn.hutool.http.ContentType; +import cn.hutool.json.JSONUtil; import com.pig4cloud.pigx.admin.api.entity.SysTenant; import com.pig4cloud.pigx.common.core.constant.CacheConstants; import com.pig4cloud.pigx.common.core.constant.CommonConstants; +import com.pig4cloud.pigx.common.core.util.R; import jakarta.servlet.FilterChain; import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletResponse; @@ -40,6 +43,7 @@ import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; import org.springframework.web.filter.GenericFilterBean; +import java.time.LocalDateTime; import java.util.List; /** @@ -94,6 +98,7 @@ public class TenantContextHolderFilter extends GenericFilterBean { * @param response 响应 * @return boolean */ + @SneakyThrows private boolean checkTenantStatus(HttpServletRequest request, HttpServletResponse response) { // 如果是获取租户列表请求跳过 if (StrUtil.containsAnyIgnoreCase(request.getRequestURI(), "/tenant/list")) { @@ -111,12 +116,22 @@ public class TenantContextHolderFilter extends GenericFilterBean { return true; } - boolean exist = tenantList.stream().anyMatch(tenant -> NumberUtil.equals(tenant.getId(), TenantContextHolder.getTenantId())); + // 获取当前请求的租户ID + Long currentTenantId = TenantContextHolder.getTenantId(); + // 检查租户是否存在且在有效期内 + boolean exist = tenantList.stream() + .filter(tenant -> NumberUtil.equals(tenant.getId(), currentTenantId)) // 匹配租户ID + .filter(tenant -> tenant.getStartTime().isBefore(LocalDateTime.now())) // 检查开始时间:租户已生效 + .anyMatch(tenant -> tenant.getEndTime().isAfter(LocalDateTime.now())); + if (exist) { return true; } - response.setStatus(HttpStatus.UPGRADE_REQUIRED.value()); + // 如果租户不存在或已失效,返回401状态码和错误信息 + response.setStatus(HttpStatus.UNAUTHORIZED.value()); + response.setContentType(ContentType.JSON.getValue()); + response.getWriter().print(JSONUtil.toJsonStr(R.failed(StrUtil.format("租户ID为{}的租户不存在或已过期失效", currentTenantId)))); return false; }