From 6aff54a3a54f5eab024e897893c5e13ff9c0ba44 Mon Sep 17 00:00:00 2001 From: gaoqr <13665037151@163.com> Date: Fri, 28 Jun 2024 14:34:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9D=83=E9=99=90=E9=97=AE=E9=A2=98=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=EF=BC=9A=201=E3=80=81=E7=B3=BB=E7=BB=9F/=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E7=AE=A1=E7=90=86=E4=B8=AD=E7=9A=84=E6=A0=87=E7=AD=BE?= =?UTF-8?q?=E8=AE=BE=E8=AE=A1=E6=9D=83=E9=99=90=E4=B8=B2=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=EF=BC=9B=202=E3=80=81=E8=A7=92=E8=89=B2=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=E6=8E=88=E6=9D=83=E6=B8=85=E7=A9=BAMENU=5FROLE=5FID=5FLIST:*:?= =?UTF-8?q?=E8=8F=9C=E5=8D=95id=EF=BC=8C=E8=A7=92=E8=89=B2=E5=88=86?= =?UTF-8?q?=E9=85=8D=E7=94=A8=E6=88=B7=E6=B8=85=E7=A9=BAUSER=5FROLE=5FID?= =?UTF-8?q?=5FLIST:*:=E7=94=A8=E6=88=B7id=E7=BC=93=E5=AD=98=EF=BC=8C?= =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=86=85=E7=BD=AE=E8=A7=92=E8=89=B2=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=E6=9D=83=E9=99=90=E5=86=85=E5=AE=B9=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E7=AB=8B=E5=8D=B3=E7=94=9F=E6=95=88=EF=BC=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/label/LabelController.java | 8 ++-- .../LabelTemplateController.java | 2 +- .../permission/PermissionServiceImpl.java | 44 ++++++++++++++----- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/label/LabelController.java b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/label/LabelController.java index 297209cb6..5a0dcb680 100644 --- a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/label/LabelController.java +++ b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/label/LabelController.java @@ -37,7 +37,7 @@ public class LabelController { @PostMapping("/create") @Operation(summary = "创建标签") - //@PreAuthorize("@ss.hasPermission('system:label:create')") + @PreAuthorize("@ss.hasPermission('sysTagDesignManager:create')") @OperateLog(enable = false) public CommonResult createLabel(@Valid @RequestBody LabelSaveReqVO createReqVO) { return success(labelService.createLabel(createReqVO)); @@ -45,7 +45,7 @@ public class LabelController { @PutMapping("/update") @Operation(summary = "更新标签") - //@PreAuthorize("@ss.hasPermission('system:label:update')") + @PreAuthorize("@ss.hasPermission('sysTagDesignManager:update')") @OperateLog(enable = false) public CommonResult updateLabel(@Valid @RequestBody LabelSaveReqVO updateReqVO) { labelService.updateLabel(updateReqVO); @@ -55,7 +55,7 @@ public class LabelController { @DeleteMapping("/delete") @Operation(summary = "删除标签") @Parameter(name = "id", description = "编号", required = true) - //@PreAuthorize("@ss.hasPermission('system:label:delete')") + @PreAuthorize("@ss.hasPermission('sysTagDesignManager:delete')") public CommonResult deleteLabel(@RequestParam("id") Long id) { labelService.deleteLabel(id); return success(true); @@ -90,7 +90,7 @@ public class LabelController { @GetMapping("/group-list") @Operation(summary = "获得分组标签列表") - //@PreAuthorize("@ss.hasPermission('system:label:query')") + @PreAuthorize("@ss.hasAnyPermissions('system:label-template:query','sysTagDesignManager:query')") @OperateLog(enable = false) public CommonResult>> getGroupList(@RequestParam(value = "organId", required = false) Long organId) { return success(labelService.getGroupList(organId)); diff --git a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/labeltemplate/LabelTemplateController.java b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/labeltemplate/LabelTemplateController.java index b70693f5a..1e95e771e 100644 --- a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/labeltemplate/LabelTemplateController.java +++ b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/controller/admin/labeltemplate/LabelTemplateController.java @@ -87,7 +87,7 @@ public class LabelTemplateController { @GetMapping("/getDefault") @Operation(summary = "获得默认标签模板") @Parameter(name = "type", description = "标签类型", required = true, example = "1024") - @PreAuthorize("@ss.hasPermission('system:label-template:query')") + @PreAuthorize("@ss.hasAnyPermissions('system:label-template:query','sysTagDesignManager:query')") @OperateLog(enable = false) public CommonResult getDefaultLabelTemplate(@RequestParam("type") String type) { return success(labelTemplateService.getDefaultLabelTemplate(type)); diff --git a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/permission/PermissionServiceImpl.java b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/permission/PermissionServiceImpl.java index a4a7ce9e2..3fcfe7e05 100644 --- a/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/permission/PermissionServiceImpl.java +++ b/cf-module-system/cf-module-system-biz/src/main/java/com/cf/imes/module/system/service/permission/PermissionServiceImpl.java @@ -39,7 +39,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; import org.springframework.cache.annotation.Caching; +import org.springframework.data.redis.core.Cursor; import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.core.ScanOptions; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -205,14 +207,15 @@ public class PermissionServiceImpl implements PermissionService { // 角色菜单权限发生修改,通知刷新本地缓存 if (CollectionUtil.isNotEmpty(createMenuIds) || CollectionUtil.isNotEmpty(deleteMenuIds)) { CompletableFuture.runAsync(() -> { - // 当前操作人和所操作的目标菜单机构不同时,清空MENU_ROLE_ID_LIST:目标机构id:菜单id的缓存 + // 当前操作人和所操作的目标菜单机构不同时,清空MENU_ROLE_ID_LIST:*:菜单id的缓存 if (ObjectUtil.notEqual(finalOrganId, currentOrganId)) { // 获取新增和删除菜单id的并集 Collection delMenuIds = CollUtil.union(createMenuIds, deleteMenuIds); - // 构建批量删除的key集合 - Set delKeys = delMenuIds.stream().map(menuId -> String.format(String.valueOf(new StringBuffer(RedisKeyConstants.MENU_ROLE_ID_LIST).append(":%s:%s")), finalOrganId, menuId)).collect(Collectors.toSet()); - Long batchDelNum = redisTemplate.delete(delKeys); - log.info("[assignRoleMenu] 批量删除redis[MENU_ROLE_ID_LIST]数量:{}", batchDelNum); + delMenuIds.forEach(delMenuId -> { + String patternKey = String.format(String.valueOf(new StringBuffer(RedisKeyConstants.MENU_ROLE_ID_LIST).append(":*:%s")), delMenuId); + Long batchDelNum = scanAndDelKeys(patternKey); + log.info("[assignRoleMenu] 批量删除redis[{}]数量:{}", patternKey, batchDelNum); + }); } redisTemplate.convertAndSend(RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, ""); }).exceptionally(e -> { @@ -222,6 +225,26 @@ public class PermissionServiceImpl implements PermissionService { } } + /** + * scan所有匹配的redis key并删除 + * + * @param keyPattern 模糊匹配的表达式 + */ + private Long scanAndDelKeys(String keyPattern) { + // 根据keyPattern scan匹配的redis key + List delKeys = new ArrayList<>(); + Cursor cursor = redisTemplate.scan(ScanOptions.scanOptions().match(keyPattern).count(200).build()); + while (cursor.hasNext()) { + delKeys.add(cursor.next()); + } + cursor.close(); + // 删除匹配到的key + if (CollectionUtil.isNotEmpty(delKeys)) { + redisTemplate.delete(delKeys); + } + return 0L; + } + @Override @Transactional(rollbackFor = Exception.class) @Caching(evict = { @@ -362,14 +385,15 @@ public class PermissionServiceImpl implements PermissionService { // 角色菜单权限发生修改,通知刷新本地缓存 if (CollectionUtil.isNotEmpty(createRoleUserIds) || CollectionUtil.isNotEmpty(deleteRoleUserIds)) { CompletableFuture.runAsync(() -> { - // 当前操作人和所操作的目标菜单机构不同时,清空USER_ROLE_ID_LIST:目标机构id:用户id的缓存 + // 当前操作人和所操作的目标菜单机构不同时,清空USER_ROLE_ID_LIST:*:用户id的缓存 if (ObjectUtil.notEqual(finalOrganId, currentOrganId)) { // 获取新增和删除用户id的并集 Collection delRoleUserIds = CollUtil.union(createRoleUserIds, deleteRoleUserIds); - // 构建批量删除的key集合 - Set delKeys = delRoleUserIds.stream().map(userId -> String.format(String.valueOf(new StringBuffer(RedisKeyConstants.USER_ROLE_ID_LIST).append(":%s:%s")), finalOrganId, userId)).collect(Collectors.toSet()); - Long batchDelNum = redisTemplate.delete(delKeys); - log.info("[batchAssignRoleUsers] 批量删除redis[USER_ROLE_ID_LIST]数量:{}", batchDelNum); + delRoleUserIds.forEach(delRoleUserId -> { + String patternKey = String.format(String.valueOf(new StringBuffer(RedisKeyConstants.USER_ROLE_ID_LIST).append(":*:%s")), delRoleUserId); + Long batchDelNum = scanAndDelKeys(patternKey); + log.info("[batchAssignRoleUsers] 批量删除redis[{}]数量:{}", patternKey, batchDelNum); + }); } redisTemplate.convertAndSend(RedisRefreshChannelTopicConstants.PERMISSION_REFRESH, ""); }).exceptionally(e -> {