Parcourir la source

feat(auth): 支持租户编码的权限缓存机制

- 在UserAuthService中添加tenantCode参数以支持租户隔离
- 修改角色权限缓存逻辑,使用租户编码作为前缀- 更新Redis缓存键名生成规则,确保租户间数据隔离
- 在WebGatewayImpl中传递租户编码以初始化权限缓存
- 添加SA_USER_TENANT_CODE常量用于存储租户编码信息
chejianzheng il y a 3 semaines
Parent
commit
1bf9c14303

+ 1 - 0
portal-service-common/src/main/java/com/hfln/portal/common/constant/UserConstants.java

@@ -14,6 +14,7 @@ public interface UserConstants {
     String SA_USER_TYPE = "user_type";
     String SA_USER_NAME = "user_name";
     String SA_USER_TENANT_ID = "user_tenant_id";
+    String SA_USER_TENANT_CODE = "user_tenant_code";
     String SA_USER_ACCOUNT = "user_account";
 
     // 用户默认角色code

+ 7 - 5
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/config/UserAuthService.java

@@ -15,6 +15,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Component;
 import org.springframework.util.CollectionUtils;
+import org.springframework.util.StringUtils;
 
 import java.util.ArrayList;
 import java.util.List;
@@ -46,23 +47,24 @@ public class UserAuthService {
     @Autowired
     private TblSystemMenuService tblSystemMenuService;
 
-    public void cacheRolesAndPermissions() {
+    public void cacheRolesAndPermissions(String tenantCode) {
 
         List<String> roleCodeList = this.getRoleCodeList(StpUtil.getLoginId(), StpUtil.getLoginType());
         if (!CollectionUtils.isEmpty(roleCodeList)) {
 
-            StpUtil.getSession().set(SaSession.ROLE_LIST, roleCodeList);
+            List<String> newRoleCodeList = new ArrayList<>();
             for (String roleCode : roleCodeList) {
                 if (!redisService.hasKey(RedisCacheConstant.ROLE_PERM_KEY_PRE + roleCode)) {
-                    // 这里从更改角色权限的地方修改最好
-                    // redisService.deleteObject(RedisCacheConstant.ROLE_PERM_KEY_PRE+roleCode);
+                    String newRoleCode = StringUtils.hasText(tenantCode) ? tenantCode + "_" + roleCode : roleCode;
+                    newRoleCodeList.add(newRoleCode);
                     List<String> permCodeList = this.getPermCodeList(roleCode);
                     if (!CollectionUtils.isEmpty(permCodeList)) {
                         // 永久期限
-                        redisService.setForever(RedisCacheConstant.ROLE_PERM_KEY_PRE + roleCode, JSONArray.toJSONString(permCodeList));
+                        redisService.setForever(RedisCacheConstant.ROLE_PERM_KEY_PRE + newRoleCode, JSONArray.toJSONString(permCodeList));
                     }
                 }
             }
+            StpUtil.getSession().set(SaSession.ROLE_LIST, newRoleCodeList);
         }
     }
 

+ 1 - 1
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/gateway/impl/WebGatewayImpl.java

@@ -343,7 +343,7 @@ public class WebGatewayImpl implements WebGateway {
 
 
         // 当前用户登录后,需要将当前用户的权限保存到redis,用于网关校验权限
-        userAuthService.cacheRolesAndPermissions();
+        userAuthService.cacheRolesAndPermissions(tenant == null ? null : tenant.getTenantCode());
 
         SaTokenInfo tokenInfo = StpUtil.getTokenInfo();
         AdminLoginRes res = CopyUtils.copy(tokenInfo, AdminLoginRes.class);