feat:支持租户动态切换

master
xiwa 2024-01-07 18:56:32 +08:00
parent 0475f00826
commit b047655808
4 changed files with 15 additions and 46 deletions

View File

@ -82,49 +82,4 @@ public class SwaggerConfig {
}
};
}
// /**
// * 解决springboot升级到2.6.x之后knife4j报错
// *
// * @param webEndpointsSupplier the web endpoints supplier
// * @param servletEndpointsSupplier the servlet endpoints supplier
// * @param controllerEndpointsSupplier the controller endpoints supplier
// * @param endpointMediaTypes the endpoint media types
// * @param corsProperties the cors properties
// * @param webEndpointProperties the web endpoint properties
// * @param environment the environment
// * @return the web mvc endpoint handler mapping
// */
// @Bean
// public WebMvcEndpointHandlerMapping webEndpointServletHandlerMapping(
// WebEndpointsSupplier webEndpointsSupplier, ServletEndpointsSupplier servletEndpointsSupplier,
// ControllerEndpointsSupplier controllerEndpointsSupplier, EndpointMediaTypes endpointMediaTypes,
// CorsEndpointProperties corsProperties, WebEndpointProperties webEndpointProperties,
// Environment environment) {
// List<ExposableEndpoint<?>> allEndpoints = new ArrayList<>();
// Collection<ExposableWebEndpoint> webEndpoints = webEndpointsSupplier.getEndpoints();
// allEndpoints.addAll(webEndpoints);
// allEndpoints.addAll(servletEndpointsSupplier.getEndpoints());
// allEndpoints.addAll(controllerEndpointsSupplier.getEndpoints());
// String basePath = webEndpointProperties.getBasePath();
// EndpointMapping endpointMapping = new EndpointMapping(basePath);
// boolean shouldRegisterLinksMapping = shouldRegisterLinksMapping(webEndpointProperties,
// environment, basePath);
// return new WebMvcEndpointHandlerMapping(endpointMapping, webEndpoints, endpointMediaTypes,
// corsProperties.toCorsConfiguration(), new EndpointLinksResolver(allEndpoints, basePath),
// shouldRegisterLinksMapping, null);
// }
//
// /**
// * shouldRegisterLinksMapping
// * @param webEndpointProperties webEndpointProperties
// * @param environment environment
// * @param basePath /
// * @return boolean
// */
// private boolean shouldRegisterLinksMapping(WebEndpointProperties webEndpointProperties,
// Environment environment, String basePath) {
// return webEndpointProperties.getDiscovery().isEnabled() && (StringUtils.hasText(basePath)
// || ManagementPortType.get(environment).equals(ManagementPortType.DIFFERENT));
// }
}

View File

@ -2,6 +2,8 @@ package cc.iotkit.common.tenant.aspect;
import cc.iotkit.common.satoken.utils.LoginHelper;
import cc.iotkit.common.tenant.helper.TenantHelper;
import cc.iotkit.common.utils.StringUtils;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
@ -25,6 +27,10 @@ public class TenantFilterAspect {
public void afterOpenSession(Object session) {
if (session instanceof Session) {
String tenantId = LoginHelper.getTenantId();
String dynamic = TenantHelper.getDynamic();
if (StringUtils.isNotBlank(dynamic)) {
tenantId = dynamic;
}
if (tenantId != null && !tenantId.equals("000000")) {
org.hibernate.Filter filter = ((Session) session).enableFilter("tenantFilter");
filter.setParameter("tenantId", tenantId);

View File

@ -2,5 +2,7 @@ package cc.iotkit.common.tenant.dao;
public interface TenantAware {
void setTenantId(String tenantId);
}

View File

@ -3,6 +3,8 @@ package cc.iotkit.common.tenant.listener;
import cc.iotkit.common.satoken.utils.LoginHelper;
import cc.iotkit.common.tenant.dao.TenantAware;
import cc.iotkit.common.tenant.helper.TenantHelper;
import cc.iotkit.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import javax.persistence.PrePersist;
@ -23,7 +25,11 @@ public class TenantListener {
@PreRemove
@PrePersist
public void setTenant(TenantAware entity) {
final String tenantId = LoginHelper.getTenantId();
String tenantId = LoginHelper.getTenantId();
String dynamic = TenantHelper.getDynamic();
if (StringUtils.isNotBlank(dynamic)) {
tenantId = dynamic;
}
if (!"000000".equals(tenantId) && tenantId != null) {
entity.setTenantId(tenantId);
}