处理租户隔离-tenant_id
This commit is contained in:
@@ -39,6 +39,7 @@ public class HisBaseEntity implements Serializable {
|
||||
private Date updateTime;
|
||||
|
||||
/** 租户ID */
|
||||
@TableField(fill = FieldFill.INSERT)
|
||||
private Integer tenantId;
|
||||
|
||||
/** 删除标识 */
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.core.common.core.domain.model;
|
||||
import java.util.Collection;
|
||||
import java.util.Set;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.security.core.GrantedAuthority;
|
||||
import org.springframework.security.core.userdetails.UserDetails;
|
||||
|
||||
@@ -14,6 +15,7 @@ import com.core.common.core.domain.entity.SysUser;
|
||||
*
|
||||
* @author system
|
||||
*/
|
||||
@Data
|
||||
public class LoginUser implements UserDetails {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -67,6 +69,9 @@ public class LoginUser implements UserDetails {
|
||||
*/
|
||||
private Set<String> permissions;
|
||||
|
||||
/** 租户ID */
|
||||
private Integer tenantId;
|
||||
|
||||
/**
|
||||
* 用户信息
|
||||
*/
|
||||
|
||||
@@ -1,14 +1,22 @@
|
||||
package com.core.framework.config;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
|
||||
import com.core.common.utils.SecurityUtils;
|
||||
import net.sf.jsqlparser.expression.Expression;
|
||||
import net.sf.jsqlparser.expression.LongValue;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.DbType;
|
||||
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.BlockAttackInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
|
||||
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@EnableTransactionManagement(proxyTargetClass = true)
|
||||
@Configuration
|
||||
@@ -22,6 +30,8 @@ public class MybatisPlusConfig {
|
||||
interceptor.addInnerInterceptor(optimisticLockerInnerInterceptor());
|
||||
// 阻断插件
|
||||
interceptor.addInnerInterceptor(blockAttackInnerInterceptor());
|
||||
// 多租户插件
|
||||
interceptor.addInnerInterceptor(tenantLineInnerInterceptor());
|
||||
return interceptor;
|
||||
}
|
||||
|
||||
@@ -50,4 +60,93 @@ public class MybatisPlusConfig {
|
||||
public BlockAttackInnerInterceptor blockAttackInnerInterceptor() {
|
||||
return new BlockAttackInnerInterceptor();
|
||||
}
|
||||
|
||||
/**
|
||||
* 多租户插件
|
||||
*/
|
||||
public TenantLineInnerInterceptor tenantLineInnerInterceptor() {
|
||||
TenantLineInnerInterceptor tenantInterceptor = new TenantLineInnerInterceptor();
|
||||
tenantInterceptor.setTenantLineHandler(new TenantLineHandler() {
|
||||
@Override
|
||||
public Expression getTenantId() {
|
||||
// 返回当前租户 ID
|
||||
return new LongValue(getCurrentTenantId());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTenantIdColumn() {
|
||||
// 返回租户 ID 的字段名
|
||||
return "tenant_id";
|
||||
}
|
||||
|
||||
// 配置需要租户隔离的表名集合
|
||||
private static final Set<String> TENANT_TABLES = new HashSet<>(Arrays.asList(
|
||||
"adm_account",
|
||||
"adm_charge_item",
|
||||
"adm_charge_item_def_app",
|
||||
"adm_charge_item_definition",
|
||||
"adm_device",
|
||||
"adm_device_definition",
|
||||
"adm_encounter",
|
||||
"adm_encounter_diagnosis",
|
||||
"adm_encounter_location",
|
||||
"adm_encounter_participant",
|
||||
"adm_encounter_reason",
|
||||
"adm_healthcare_service",
|
||||
"adm_invoice",
|
||||
"adm_location",
|
||||
"adm_organization",
|
||||
"adm_organization_location",
|
||||
"adm_patient",
|
||||
"adm_patient_identifier",
|
||||
"adm_practitioner",
|
||||
"adm_practitioner_role",
|
||||
"adm_supplier",
|
||||
"cli_condition",
|
||||
"cli_condition_definition",
|
||||
"cli_procedure",
|
||||
"cli_procedure_performer",
|
||||
"doc_emr",
|
||||
"doc_emr_template",
|
||||
"fin_claim",
|
||||
"fin_claim_response",
|
||||
"fin_contract",
|
||||
"fin_payment_notice",
|
||||
"fin_payment_rec_detail",
|
||||
"fin_payment_reconciliation",
|
||||
"med_medication",
|
||||
"med_medication_definition",
|
||||
"med_medication_dispense",
|
||||
"med_medication_request",
|
||||
"wor_activity_definition",
|
||||
"wor_device_dispense",
|
||||
"wor_device_request",
|
||||
"wor_inventory_item",
|
||||
"wor_service_request",
|
||||
"wor_service_request_detail",
|
||||
"wor_supply_delivery",
|
||||
"wor_supply_request"
|
||||
));
|
||||
|
||||
@Override
|
||||
public boolean ignoreTable(String tableName) {
|
||||
// 忽略不需要租户隔离的表
|
||||
// return false; // 默认所有表都开启租户隔离
|
||||
// 如果表名不在集合中,则忽略租户隔离
|
||||
return !TENANT_TABLES.contains(tableName);
|
||||
}
|
||||
});
|
||||
return tenantInterceptor;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前租户 ID
|
||||
*/
|
||||
private Integer getCurrentTenantId() {
|
||||
// 获取当前登录用户的租户 ID
|
||||
if (SecurityUtils.getAuthentication() != null) {
|
||||
return SecurityUtils.getLoginUser().getTenantId();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ public class MybastisColumnsHandler implements MetaObjectHandler {
|
||||
} catch (Exception ignored) {
|
||||
}
|
||||
this.strictInsertFill(metaObject, "createBy", String.class, username);
|
||||
this.strictInsertFill(metaObject, "tenantId", Integer.class, getCurrentTenantId());
|
||||
}
|
||||
|
||||
// 设置数据修改update时候的,字段自动赋值规则
|
||||
@@ -41,4 +42,15 @@ public class MybastisColumnsHandler implements MetaObjectHandler {
|
||||
}
|
||||
this.strictUpdateFill(metaObject, "updateBy", String.class, username);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前租户 ID
|
||||
*/
|
||||
private Integer getCurrentTenantId() {
|
||||
// 获取当前登录用户的租户 ID
|
||||
if (SecurityUtils.getAuthentication() != null) {
|
||||
return SecurityUtils.getLoginUser().getTenantId();
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ public class SysLoginService {
|
||||
*/
|
||||
public String login(String username, String password, String code, String uuid) {
|
||||
// 验证码校验
|
||||
validateCaptcha(username, code, uuid);
|
||||
validateCaptcha(username, code, uuid);
|
||||
// 登录前置校验
|
||||
loginPreCheck(username, password);
|
||||
// 用户验证
|
||||
@@ -87,6 +87,11 @@ public class SysLoginService {
|
||||
AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.LOGIN_SUCCESS,
|
||||
MessageUtils.message("user.login.success")));
|
||||
LoginUser loginUser = (LoginUser)authentication.getPrincipal();
|
||||
|
||||
// -----start-----登录时set租户id,正常应该从请求头获取,这行代码只是测试使用
|
||||
loginUser.setTenantId(1);
|
||||
// -----end-----登录时set租户id,正常应该从请求头获取,这行代码只是测试使用
|
||||
|
||||
recordLoginInfo(loginUser.getUserId());
|
||||
// 生成token
|
||||
return tokenService.createToken(loginUser);
|
||||
|
||||
Reference in New Issue
Block a user