【新增】【优化】新建租户时,自动创建对应的管理员账号、角色等基础信息

This commit is contained in:
YunaiV
2022-02-20 23:59:23 +08:00
parent 6b6d676a6b
commit 2598c033a9
34 changed files with 425 additions and 64 deletions

View File

@ -2,7 +2,9 @@ package cn.iocoder.yudao.framework.tenant.config;
import cn.iocoder.yudao.framework.common.enums.WebFilterOrderEnum;
import cn.iocoder.yudao.framework.tenant.core.security.TenantSecurityWebFilter;
import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService;
import cn.iocoder.yudao.framework.web.config.WebProperties;
import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
@ -22,9 +24,12 @@ public class YudaoTenantSecurityAutoConfiguration {
@Bean
public FilterRegistrationBean<TenantSecurityWebFilter> tenantSecurityWebFilter(TenantProperties tenantProperties,
WebProperties webProperties) {
WebProperties webProperties,
GlobalExceptionHandler globalExceptionHandler,
TenantFrameworkService tenantFrameworkService) {
FilterRegistrationBean<TenantSecurityWebFilter> registrationBean = new FilterRegistrationBean<>();
registrationBean.setFilter(new TenantSecurityWebFilter(tenantProperties, webProperties));
registrationBean.setFilter(new TenantSecurityWebFilter(tenantProperties, webProperties,
globalExceptionHandler, tenantFrameworkService));
registrationBean.setOrder(WebFilterOrderEnum.TENANT_SECURITY_FILTER);
return registrationBean;
}

View File

@ -11,11 +11,6 @@ public class TenantContextHolder {
private static final ThreadLocal<Long> TENANT_ID = new TransmittableThreadLocal<>();
/**
* 租户编号 - 空
*/
private static final Long TENANT_ID_NULL = 0L;
/**
* 获得租户编号。
*
@ -38,15 +33,6 @@ public class TenantContextHolder {
return tenantId;
}
/**
* 在一些前端场景下,可能无法请求带上租户。例如说,<img /> 方式获取图片等
* 此时,暂时的解决方案,是在该接口的 Controller 方法上,调用该方法
* TODO 芋艿:思考有没更合适的方案,目标是去掉该方法
*/
public static void setNullTenantId() {
TENANT_ID.set(TENANT_ID_NULL);
}
public static void setTenantId(Long tenantId) {
TENANT_ID.set(tenantId);
}

View File

@ -8,8 +8,10 @@ import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.framework.tenant.config.TenantProperties;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService;
import cn.iocoder.yudao.framework.web.config.WebProperties;
import cn.iocoder.yudao.framework.web.core.filter.ApiRequestFilter;
import cn.iocoder.yudao.framework.web.core.handler.GlobalExceptionHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.AntPathMatcher;
@ -24,6 +26,7 @@ import java.util.Objects;
* 多租户 Security Web 过滤器
* 1. 如果是登陆的用户,校验是否有权限访问该租户,避免越权问题。
* 2. 如果请求未带租户的编号,检查是否是忽略的 URL否则也不允许访问。
* 3. 校验租户是合法,例如说被禁用、到期
*
* 校验用户访问的租户,是否是其所在的租户,
*
@ -33,13 +36,21 @@ import java.util.Objects;
public class TenantSecurityWebFilter extends ApiRequestFilter {
private final TenantProperties tenantProperties;
private final AntPathMatcher pathMatcher;
private final GlobalExceptionHandler globalExceptionHandler;
private final TenantFrameworkService tenantFrameworkService;
public TenantSecurityWebFilter(TenantProperties tenantProperties,
WebProperties webProperties) {
WebProperties webProperties,
GlobalExceptionHandler globalExceptionHandler,
TenantFrameworkService tenantFrameworkService) {
super(webProperties);
this.tenantProperties = tenantProperties;
this.pathMatcher = new AntPathMatcher();
this.globalExceptionHandler = globalExceptionHandler;
this.tenantFrameworkService = tenantFrameworkService;
}
@Override
@ -72,6 +83,17 @@ public class TenantSecurityWebFilter extends ApiRequestFilter {
return;
}
// 3. 校验租户是合法,例如说被禁用、到期
if (tenantId != null) {
try {
tenantFrameworkService.validTenant(tenantId);
} catch (Throwable ex) {
CommonResult<?> result = globalExceptionHandler.allExceptionHandler(request, ex);
ServletUtils.writeJSON(response, result);
return;
}
}
// 继续过滤
chain.doFilter(request, response);
}

View File

@ -16,4 +16,11 @@ public interface TenantFrameworkService {
*/
List<Long> getTenantIds();
/**
* 校验租户是否合法
*
* @param id 租户编号
*/
void validTenant(Long id);
}

View File

@ -0,0 +1,30 @@
package cn.iocoder.yudao.framework.tenant.core.util;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
/**
* 多租户 Util
*
* @author 芋道源码
*/
public class TenantUtils {
/**
* 使用指定租户,执行对应的逻辑
*
* @param tenantId 租户编号
* @param runnable 逻辑
*/
public static void execute(Long tenantId, Runnable runnable) {
Long oldTenantId = TenantContextHolder.getTenantId();
try {
TenantContextHolder.setTenantId(tenantId);
// 执行逻辑
runnable.run();
} finally {
TenantContextHolder.setTenantId(oldTenantId);
}
}
}