refactor(tenant): 优化租户相关代码

This commit is contained in:
2025-07-17 22:15:13 +08:00
parent 6e7d371565
commit 7e9a950694
29 changed files with 333 additions and 151 deletions

View File

@@ -19,10 +19,18 @@ package top.continew.admin.config.satoken;
import cn.dev33.satoken.fun.SaParamFunction;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.extra.servlet.JakartaServletUtil;
import cn.hutool.json.JSONUtil;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.lang.Nullable;
import top.continew.admin.common.context.UserContext;
import top.continew.admin.common.context.UserContextHolder;
import top.continew.starter.extension.tenant.context.TenantContextHolder;
import top.continew.starter.web.model.R;
/**
* Sa-Token 扩展拦截器
@@ -30,6 +38,7 @@ import top.continew.admin.common.context.UserContextHolder;
* @author Charles7c
* @since 2024/10/10 20:25
*/
@Slf4j
public class SaExtensionInterceptor extends SaInterceptor {
public SaExtensionInterceptor(SaParamFunction<Object> auth) {
@@ -41,11 +50,24 @@ public class SaExtensionInterceptor extends SaInterceptor {
HttpServletResponse response,
Object handler) throws Exception {
boolean flag = super.preHandle(request, response, handler);
if (flag && StpUtil.isLogin()) {
UserContextHolder.getContext();
UserContextHolder.getExtraContext();
if (!flag || !StpUtil.isLogin()) {
return flag;
}
return flag;
// 设置上下文
UserContext userContext = UserContextHolder.getContext();
if (userContext == null) {
return true;
}
// 检查用户租户权限
Long userTenantId = userContext.getTenantId();
Long tenantId = TenantContextHolder.getTenantId();
if (!userTenantId.equals(tenantId)) {
JakartaServletUtil.write(response, JSONUtil.toJsonStr(R.fail(String.valueOf(HttpStatus.FORBIDDEN
.value()), "您当前没有访问该租户的权限")), MediaType.APPLICATION_JSON_VALUE);
return false;
}
UserContextHolder.getExtraContext();
return true;
}
@Override
@@ -53,6 +75,7 @@ public class SaExtensionInterceptor extends SaInterceptor {
HttpServletResponse response,
Object handler,
@Nullable Exception e) throws Exception {
// 清除上下文
try {
super.afterCompletion(request, response, handler, e);
} finally {

View File

@@ -57,6 +57,7 @@ import top.continew.starter.core.autoconfigure.application.ApplicationProperties
import top.continew.starter.core.util.TemplateUtils;
import top.continew.starter.core.util.validation.CheckUtils;
import top.continew.starter.core.util.validation.ValidationUtils;
import top.continew.starter.extension.tenant.annotation.TenantIgnore;
import top.continew.starter.log.annotation.Log;
import top.continew.starter.messaging.mail.util.MailUtils;
import top.continew.starter.ratelimiter.annotation.RateLimiter;
@@ -78,6 +79,7 @@ import java.util.concurrent.TimeUnit;
* @since 2022/12/11 14:00
*/
@Tag(name = "验证码 API")
@TenantIgnore
@SaIgnore
@Validated
@RestController

View File

@@ -38,6 +38,7 @@ import top.continew.admin.system.service.*;
import top.continew.starter.core.util.validation.ValidationUtils;
import top.continew.starter.extension.crud.model.query.SortQuery;
import top.continew.starter.extension.crud.model.resp.LabelValueResp;
import top.continew.starter.extension.tenant.annotation.TenantIgnore;
import top.continew.starter.log.annotation.Log;
import java.io.IOException;
@@ -111,6 +112,7 @@ public class CommonController {
return dictItemService.listByDictCode(code);
}
@TenantIgnore
@SaIgnore
@Operation(summary = "查询系统配置参数", description = "查询系统配置参数")
@GetMapping("/dict/option/site")

View File

@@ -35,6 +35,7 @@ import top.continew.admin.system.model.entity.user.UserDO;
import top.continew.admin.system.model.entity.user.UserSocialDO;
import top.continew.starter.cache.redisson.util.RedisUtils;
import top.continew.starter.core.constant.StringConstants;
import top.continew.starter.extension.tenant.annotation.TenantIgnore;
import java.util.List;
import java.util.function.BooleanSupplier;
@@ -78,6 +79,7 @@ public class DemoEnvironmentJob {
/**
* 重置演示环境数据
*/
@TenantIgnore
@JobExecutor(name = "ResetEnvironmentData")
@Transactional(rollbackFor = Exception.class)
public void resetEnvironmentData() {

View File

@@ -33,6 +33,7 @@ import top.continew.admin.system.mapper.NoticeMapper;
import top.continew.admin.system.model.entity.NoticeDO;
import top.continew.admin.system.service.NoticeService;
import top.continew.starter.core.constant.PropertiesConstants;
import top.continew.starter.extension.tenant.annotation.TenantIgnore;
import java.time.LocalDateTime;
import java.util.List;
@@ -55,6 +56,7 @@ public class NoticePublishJob {
@ConditionalOnProperty(prefix = "snail-job", name = PropertiesConstants.ENABLED, havingValue = "false")
public static class Scheduler {
@TenantIgnore
@Scheduled(cron = "0 * * * * ?")
@Transactional(rollbackFor = Exception.class)
public void publishNoticeWithSchedule() {
@@ -71,6 +73,7 @@ public class NoticePublishJob {
@ConditionalOnEnabledScheduleJob
public static class ScheduleJob {
@TenantIgnore
@JobExecutor(name = "NoticePublishJob")
@Transactional(rollbackFor = Exception.class)
public void publishNoticeWithScheduleJob() {