|
@@ -1,5 +1,6 @@
|
|
|
package com.ruoyi.common.utils;
|
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.ruoyi.common.constant.HttpStatus;
|
|
|
import com.ruoyi.common.core.domain.model.LoginUser;
|
|
|
import com.ruoyi.common.exception.ServiceException;
|
|
@@ -16,6 +17,7 @@ import java.util.regex.Pattern;
|
|
|
* @author ruoyi
|
|
|
*/
|
|
|
public class SecurityUtils {
|
|
|
+ private static final ThreadLocal<String> HOLDER_TENANT_ID = new ThreadLocal<>();
|
|
|
|
|
|
|
|
|
* 用户ID
|
|
@@ -55,6 +57,10 @@ public class SecurityUtils {
|
|
|
**/
|
|
|
public static String getTenantId() {
|
|
|
try {
|
|
|
+ String tenantId = HOLDER_TENANT_ID.get();
|
|
|
+ if(ObjectUtil.isNotEmpty(tenantId)){
|
|
|
+ return tenantId;
|
|
|
+ }
|
|
|
return getLoginUser().getUser().getTenantId();
|
|
|
} catch (Exception e) {
|
|
|
throw new ServiceException("获取租户id异常", HttpStatus.UNAUTHORIZED);
|
|
@@ -62,6 +68,20 @@ public class SecurityUtils {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ * 临时设置租户Id(只在当前线程有效)
|
|
|
+ */
|
|
|
+ public static void setTenantId(String tenantId) {
|
|
|
+ HOLDER_TENANT_ID.set(tenantId);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ * 清除临时设置的租户Id
|
|
|
+ */
|
|
|
+ public static void clearTenantId() {
|
|
|
+ HOLDER_TENANT_ID.remove();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
* 获取用户
|
|
|
**/
|
|
|
public static LoginUser getLoginUser() {
|