mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-08 04:39:00 +08:00
feat(auth): 优化租户过期验证逻辑并统一Redis key格式
- 修改登录失败/成功处理中的Redis key格式,移除tenantKeyStrResolver依赖 - 简化RedisUtils.execute调用方式,移除冗余的RedisCallback类型声明 - 在SysTenantServiceImpl中增加租户状态和时间有效性过滤 - 重构TenantContextHolderFilter的租户验证逻辑,增加remoteTenantService备用查询 - 调整租户无效时的HTTP状态码为426并优化响应内容类型
This commit is contained in:
1 parent
00efa97eef
commit
2d7d089fcd
4 files changed
+28
-14
No files matched your search
+2
-3
@@ -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<Long>) connection ->
|
||||
Long times = RedisUtils.execute(connection ->
|
||||
connection.incr(key.getBytes())
|
||||
);
|
||||
|
||||
|
||||
+1
-1
@@ -128,7 +128,7 @@ public class PigxAuthenticationSuccessEventHandler implements AuthenticationSucc
|
||||
* @param map
|
||||
*/
|
||||
private void clearLoginFailureTimes(Map<String, Object> 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);
|
||||
}
|
||||
|
||||
+16
-8
@@ -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<SysTenant> 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<List<SysTenant>> tenantListR = remoteTenantService.list();
|
||||
tenantList = tenantListR.getData();
|
||||
}
|
||||
|
||||
List<SysTenant> 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;
|
||||
}
|
||||
|
||||
+9
-2
@@ -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<SysTenantMapper, SysTenant
|
||||
MPJLambdaWrapper<SysTenant> wrapper = new MPJLambdaWrapper<SysTenant>()
|
||||
.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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in new issue
Block a user