From 2d7d089fcd334a7463e6b035b7cc168bd38292b6 Mon Sep 17 00:00:00 2001 From: PIG AI Date: Fri, 4 Jul 2025 16:41:06 +0800 Subject: [PATCH] =?UTF-8?q?feat(auth):=20=E4=BC=98=E5=8C=96=E7=A7=9F?= =?UTF-8?q?=E6=88=B7=E8=BF=87=E6=9C=9F=E9=AA=8C=E8=AF=81=E9=80=BB=E8=BE=91?= =?UTF-8?q?=E5=B9=B6=E7=BB=9F=E4=B8=80Redis=20key=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改登录失败/成功处理中的Redis key格式,移除tenantKeyStrResolver依赖 - 简化RedisUtils.execute调用方式,移除冗余的RedisCallback类型声明 - 在SysTenantServiceImpl中增加租户状态和时间有效性过滤 - 重构TenantContextHolderFilter的租户验证逻辑,增加remoteTenantService备用查询 - 调整租户无效时的HTTP状态码为426并优化响应内容类型 --- ...PigxAuthenticationFailureEventHandler.java | 5 ++-- ...PigxAuthenticationSuccessEventHandler.java | 2 +- .../tenant/TenantContextHolderFilter.java | 24 ++++++++++++------- .../service/impl/SysTenantServiceImpl.java | 11 +++++++-- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationFailureEventHandler.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationFailureEventHandler.java index f564f579d..092566fec 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationFailureEventHandler.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationFailureEventHandler.java @@ -36,7 +36,6 @@ import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationEventPublisher; -import org.springframework.data.redis.core.RedisCallback; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -131,11 +130,11 @@ public class PigxAuthenticationFailureEventHandler implements AuthenticationFail * @param username username */ private void recordLoginFailureTimes(String username) { - String key = String.format("%s:%s:%s", tenantKeyStrResolver.key(), CacheConstants.LOGIN_ERROR_TIMES, username); + String key = String.format("%s%s:%s", CacheConstants.GLOBALLY, CacheConstants.LOGIN_ERROR_TIMES, username); Long deltaTimes = ParamResolver.getLong("LOGIN_ERROR_TIMES", 5L); // 使用 RedisUtils 执行原生 Redis 命令进行递增操作 - Long times = RedisUtils.execute((RedisCallback) connection -> + Long times = RedisUtils.execute(connection -> connection.incr(key.getBytes()) ); diff --git a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationSuccessEventHandler.java b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationSuccessEventHandler.java index f354bf77c..918edcce4 100644 --- a/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationSuccessEventHandler.java +++ b/pigx-auth/src/main/java/com/pig4cloud/pigx/auth/support/handler/PigxAuthenticationSuccessEventHandler.java @@ -128,7 +128,7 @@ public class PigxAuthenticationSuccessEventHandler implements AuthenticationSucc * @param map */ private void clearLoginFailureTimes(Map map) { - String key = String.format("%s:%s:%s", tenantKeyStrResolver.key(), CacheConstants.LOGIN_ERROR_TIMES, + String key = String.format("%s%s:%s", CacheConstants.GLOBALLY, CacheConstants.LOGIN_ERROR_TIMES, MapUtil.getStr(map, SecurityConstants.DETAILS_USERNAME)); RedisUtils.delete(key); } 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 6156f0b3f..45bbcee16 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,9 @@ 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.admin.api.feign.RemoteTenantService; import com.pig4cloud.pigx.common.core.constant.CacheConstants; import com.pig4cloud.pigx.common.core.constant.CommonConstants; import com.pig4cloud.pigx.common.core.util.R; @@ -45,6 +45,7 @@ import org.springframework.web.filter.GenericFilterBean; import java.time.LocalDateTime; import java.util.List; +import java.util.Objects; /** * @author lengleng @@ -58,6 +59,8 @@ public class TenantContextHolderFilter extends GenericFilterBean { private final static String UNDEFINED_STR = "undefined"; + private final RemoteTenantService remoteTenantService; + private final CacheManager cacheManager; @Override @@ -105,15 +108,20 @@ public class TenantContextHolderFilter extends GenericFilterBean { return true; } + List tenantList = null; + // 判断租户状态 Cache cache = cacheManager.getCache(CacheConstants.TENANT_DETAILS); - if (cache == null) { - return true; + + if (Objects.nonNull(cache) && Objects.nonNull(cache.get(SimpleKey.EMPTY, List.class))) { + tenantList = cache.get(SimpleKey.EMPTY, List.class); + } else { + R> tenantListR = remoteTenantService.list(); + tenantList = tenantListR.getData(); } - List tenantList = cache.get(SimpleKey.EMPTY, List.class); if (CollUtil.isEmpty(tenantList)) { - return true; + return false; } // 获取当前请求的租户ID @@ -128,9 +136,9 @@ public class TenantContextHolderFilter extends GenericFilterBean { return true; } - // 如果租户不存在或已失效,返回401状态码和错误信息 - response.setStatus(HttpStatus.UNAUTHORIZED.value()); - response.setContentType(ContentType.JSON.getValue()); + // 如果租户不存在或已失效,返回426状态码和错误信息 + response.setStatus(HttpStatus.UPGRADE_REQUIRED.value()); + response.setContentType("application/json;charset=UTF-8"); response.getWriter().print(JSONUtil.toJsonStr(R.failed(StrUtil.format("租户ID为{}的租户不存在或已过期失效", currentTenantId)))); return false; } diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysTenantServiceImpl.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysTenantServiceImpl.java index da9953f78..3c41dcfa4 100755 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysTenantServiceImpl.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/service/impl/SysTenantServiceImpl.java @@ -51,6 +51,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.lang.reflect.Field; +import java.time.LocalDateTime; import java.util.*; /** @@ -313,8 +314,14 @@ public class SysTenantServiceImpl extends ServiceImpl wrapper = new MPJLambdaWrapper() .selectAll(SysTenant.class) .leftJoin(SysTenantUser.class, SysTenantUser::getTenantId, SysTenant::getId) - .eq(SysTenantUser::getUserId, SecurityUtils.getUser().getId()); - return baseMapper.selectJoinList(wrapper); + .eq(SysTenantUser::getUserId, SecurityUtils.getUser().getId()) + .eq(SysTenant::getStatus, TenantStateEnum.NORMAL.getCode()); + + return baseMapper.selectJoinList(wrapper) + .stream() + .filter(tenant -> tenant.getStartTime().isBefore(LocalDateTime.now())) + .filter(tenant -> tenant.getEndTime().isAfter(LocalDateTime.now())) + .toList(); } /**