浏览代码

租户处理模块

guoenzhou 2 年之前
父节点
当前提交
706e6f3d29

+ 36 - 0
base-modules/service-system/service-system-biz/src/main/java/com/usky/system/controller/web/TestController.java

@@ -0,0 +1,36 @@
+package com.usky.demo.controller.web;
+
+import com.usky.common.core.bean.ApiResult;
+import com.usky.demo.domain.SysUser;
+import com.usky.demo.mapper.SysUserMapper;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+import java.util.List;
+
+/**
+ * 事件管理-火警告警处置
+ *
+ * @author JCB
+ * @since 2022-07-29
+ */
+@RestController
+@RequestMapping("/test")
+public class TestController {
+
+    @Autowired
+    private SysUserMapper sysUserMapper;
+
+
+    @GetMapping("select")
+    public ApiResult<SysUser> select() {
+        SysUser sysUser = sysUserMapper.selectUserByIdOne(1L);
+        return ApiResult.success(sysUser);
+    }
+
+
+
+}
+

+ 18 - 8
usky-common/usky-common-tenant/src/main/java/com/usky/common/tenant/config/UskyTenantAutoConfiguration.java → usky-common/usky-common-tenant/src/main/java/com/usky/common/tenant/config/TenantAutoConfiguration.java

@@ -5,21 +5,20 @@ import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor;
 import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
 import com.usky.common.tenant.core.aop.TenantIgnoreAspect;
-import com.usky.common.tenant.core.db.TenantDatabaseInterceptor;
+import com.usky.common.tenant.core.db.TenantDatabaseHandler;
 import com.usky.common.tenant.core.web.TenantContextWebFilter;
 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
 import org.springframework.boot.context.properties.EnableConfigurationProperties;
 import org.springframework.boot.web.servlet.FilterRegistrationBean;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
-
 import java.util.ArrayList;
 import java.util.List;
 
 @Configuration
-@ConditionalOnProperty(prefix = "tenant", value = "enable", matchIfMissing = true) // 允许使用 yudao.tenant.enable=false 禁用多租户
+@ConditionalOnProperty(prefix = "tenant", value = "enable", matchIfMissing = true) // 允许使用 tenant.enable=treu 禁用多租户
 @EnableConfigurationProperties(TenantProperties.class)
-public class UskyTenantAutoConfiguration {
+public class TenantAutoConfiguration {
 
 
     // ========== AOP ==========
@@ -31,21 +30,32 @@ public class UskyTenantAutoConfiguration {
 
     // ========== DB ==========
 
+    /**
+     * mybatis-plus 多组户配置
+     * @param properties
+     * @param interceptor
+     * @return
+     */
     @Bean
     public TenantLineInnerInterceptor tenantLineInnerInterceptor(TenantProperties properties,MybatisPlusInterceptor interceptor) {
-        TenantLineInnerInterceptor inner = new TenantLineInnerInterceptor(new TenantDatabaseInterceptor(properties));
+        TenantLineInnerInterceptor inner = new TenantLineInnerInterceptor();
+        //多租户插件
+        inner.setTenantLineHandler(new TenantDatabaseHandler(properties));
+
         // 添加到 interceptor 中
         // 需要加在首个,主要是为了在分页插件前面。这个是 MyBatis Plus 的规定
-
         List<InnerInterceptor> inners = new ArrayList<>(interceptor.getInterceptors());
         inners.add(0, inner);
         interceptor.setInterceptors(inners);
-
         return inner;
     }
 
-    // ========== WEB ==========
 
+    /**
+     * 注册Web拦截器解析租户ID
+     * 通过{@link com.usky.common.tenant.core.context.TenantContextHolder} 传递给 {@link TenantDatabaseHandler}
+     * @return
+     */
     @Bean
     public FilterRegistrationBean<TenantContextWebFilter> tenantContextWebFilter() {
         FilterRegistrationBean<TenantContextWebFilter> registrationBean = new FilterRegistrationBean<>();

+ 0 - 1
usky-common/usky-common-tenant/src/main/java/com/usky/common/tenant/core/context/TenantContextHolder.java

@@ -28,7 +28,6 @@ public class TenantContextHolder {
 
     /**
      * 获得租户编号。如果不存在,则抛出 NullPointerException 异常
-     *
      * @return 租户编号
      */
     public static Integer getRequiredTenantId() {

+ 10 - 3
usky-common/usky-common-tenant/src/main/java/com/usky/common/tenant/core/db/TenantDatabaseInterceptor.java → usky-common/usky-common-tenant/src/main/java/com/usky/common/tenant/core/db/TenantDatabaseHandler.java

@@ -14,14 +14,21 @@ import java.util.Set;
 
 /**
  * 基于 MyBatis Plus 多租户的功能,实现 DB 层面的多租户的功能
- *
+ * TenantDatabaseInterceptor类实现了TenantLineHandler接口,并实现了
+ * getTenantId()方法,该方法主要用于设置租户Id的值,在框架去处理SQL语句前去改写SQL语句,
+ * 为SQL语句添加上租户判断条件。租户Id可以从缓存、cookie、token等中获取(根据实际的业务场景来)
+ * getTenantIdColumn()方法,该方法用于设置租户Id的字段名称
+ * ignoreTable(String tableName)方法,该方法用于标记忽略添加租户ID的表
  * @author yq
  */
-public class TenantDatabaseInterceptor implements TenantLineHandler {
+public class TenantDatabaseHandler implements TenantLineHandler {
 
+    /**
+     * 忽略添加租户ID的表
+     */
     private final Set<String> ignoreTables = new HashSet<>();
 
-    public TenantDatabaseInterceptor(TenantProperties properties) {
+    public TenantDatabaseHandler(TenantProperties properties) {
         // 不同 DB 下,大小写的习惯不同,所以需要都添加进去
         if (CollectionUtils.isNotEmpty(properties.getIgnoreTables())){
             properties.getIgnoreTables().forEach(table -> {

+ 7 - 3
usky-common/usky-common-tenant/src/main/java/com/usky/common/tenant/core/web/TenantContextWebFilter.java

@@ -13,14 +13,18 @@ import java.io.IOException;
 
 /**
  * 多租户 Context Web 过滤器
- * 将请求 Header 中的 tenant-id 解析出来,添加到 {@link TenantContextHolder} 中,这样后续的 DB 等操作,可以获得到租户编号。
- *
+ * 将请求 Header 中的 tenant_id 解析出来,添加到 {@link TenantContextHolder} 中,这样后续的 DB 等操作,可以获得到租户编号。
+ * 直接从Header获取tenant_id风险和优势:
+ * 1.放到header的tentant_id有被篡改的风险,考虑到降低组件依赖耦合,该行为应该有其它组件来预防实现,它的主要职责进行横切拼接SQL
  * @author yq
  */
 public class TenantContextWebFilter extends OncePerRequestFilter {
 
+    /**
+     * 租户ID KEY
+     */
+    private final static String HEADER_TENANT_ID = "tenant_id";
 
-    private final static String HEADER_TENANT_ID = "tenant-id";
     @Override
     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
             throws ServletException, IOException {