소스 검색

feat(auth):优化角色权限缓存逻辑以支持租户隔离

- 修改TblRoleServiceImpl中查询条件,支持tenantId为null时查询
- 调整UserAuthService中cacheRolesAndPermissions方法参数类型
- 更新getPermCodeList方法签名并传入tenantId参数
- 在WebGatewayImpl中调用cacheRolesAndPermissions时传入tenant对象
chejianzheng 1 주 전
부모
커밋
c1af8ff0d7

+ 10 - 11
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/config/UserAuthService.java

@@ -6,16 +6,15 @@ import cn.hfln.framework.redis.util.RedisUtil;
 import com.alibaba.fastjson2.JSONArray;
 import com.hfln.portal.common.constant.UserConstants;
 import com.hfln.portal.common.constant.redis.RedisCacheConstant;
-import com.hfln.portal.infrastructure.po.TblRole;
-import com.hfln.portal.infrastructure.po.TblRoleMenuMap;
-import com.hfln.portal.infrastructure.po.TblSystemMenu;
-import com.hfln.portal.infrastructure.po.TblUserRole;
-import com.hfln.portal.infrastructure.service.*;
+import com.hfln.portal.infrastructure.po.*;
+import com.hfln.portal.infrastructure.service.TblRoleMenuMapService;
+import com.hfln.portal.infrastructure.service.TblRoleService;
+import com.hfln.portal.infrastructure.service.TblSystemMenuService;
+import com.hfln.portal.infrastructure.service.UserRoleService;
 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;
@@ -47,17 +46,17 @@ public class UserAuthService {
     @Autowired
     private TblSystemMenuService tblSystemMenuService;
 
-    public void cacheRolesAndPermissions(String tenantCode) {
+    public void cacheRolesAndPermissions(TblTenant tenant) {
 
         List<String> roleCodeList = this.getRoleCodeList(StpUtil.getLoginId(), StpUtil.getLoginType());
         if (!CollectionUtils.isEmpty(roleCodeList)) {
 
             List<String> newRoleCodeList = new ArrayList<>();
             for (String roleCode : roleCodeList) {
-                String newRoleCode = StringUtils.hasText(tenantCode) ? tenantCode + "_" + roleCode : roleCode;
+                String newRoleCode = tenant != null  ? tenant.getTenantCode() + "_" + roleCode : roleCode;
                 newRoleCodeList.add(newRoleCode);
                 if (!redisService.hasKey(RedisCacheConstant.ROLE_PERM_KEY_PRE + newRoleCode)) {
-                    List<String> permCodeList = this.getPermCodeList(roleCode);
+                    List<String> permCodeList = this.getPermCodeList(roleCode, tenant == null ? null : tenant.getTenantId());
                     if (!CollectionUtils.isEmpty(permCodeList)) {
                         // 永久期限
                         redisService.setForever(RedisCacheConstant.ROLE_PERM_KEY_PRE + newRoleCode, JSONArray.toJSONString(permCodeList));
@@ -94,11 +93,11 @@ public class UserAuthService {
     /**
      * 返回当前角色所拥有的权限码集合
      */
-    public List<String> getPermCodeList(String roleCode) {
+    public List<String> getPermCodeList(String roleCode, Long tenantId) {
 
         List<String> list = new ArrayList<>();
 
-        TblRole role = tblRoleService.findAvailableByCode(roleCode);
+        TblRole role = tblRoleService.queryRole(tenantId, roleCode);
         if (role == null) {
             log.info("角色配置异常, userIds :{}", role);
             return list;

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

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

+ 4 - 0
portal-service-infrastructure/src/main/java/com/hfln/portal/infrastructure/service/impl/TblRoleServiceImpl.java

@@ -38,6 +38,8 @@ public class TblRoleServiceImpl extends ServiceImpl<TblRoleMapper, TblRole> impl
         LambdaQueryWrapper<TblRole> queryWrapper = new LambdaQueryWrapper<>();
         if (tenantId != null) {
             queryWrapper.eq(TblRole::getTenantId, tenantId);
+        } else {
+            queryWrapper.isNull(TblRole::getTenantId);
         }
         queryWrapper.eq(TblRole::getRoleCode, roleCode);
         queryWrapper.eq(TblRole::getIsDeleted, BasePO.DeleteFlag.NOT_DELETED);
@@ -49,6 +51,8 @@ public class TblRoleServiceImpl extends ServiceImpl<TblRoleMapper, TblRole> impl
         LambdaQueryWrapper<TblRole> queryWrapper = new LambdaQueryWrapper<>();
         if (tenantId != null) {
             queryWrapper.eq(TblRole::getTenantId, tenantId);
+        } else {
+            queryWrapper.isNull(TblRole::getTenantId);
         }
 
         queryWrapper.and(qw -> qw.eq(TblRole::getRoleCode, roleCode).or().eq(TblRole::getRoleName, roleName));