AutoSyncPermission implements ApplicationListener<AuthorizeDefinitionInitializedEvent> { @Override public void onApplicationEvent(AuthorizeDefinitionInitializedEvent event) { List<AuthorizeDefinition> definitions = event.getAllDefinition(); Map<String, List<AuthorizeDefinition>> grouping = new HashMap<>(); for (AuthorizeDefinition definition : definitions) { for (String permissionId : definition.getPermissions()) { grouping.computeIfAbsent(permissionId, id -> new ArrayList<>()) .add(definition); } } Map<String, PermissionEntity> waitToSyncPermissions = new HashMap<>(); for (Map.Entry<String, List<AuthorizeDefinition>> permissionDefinition : grouping.entrySet()) { String permissionId = permissionDefinition.getKey(); List<AuthorizeDefinition> allDefinition = permissionDefinition.getValue(); if (allDefinition.isEmpty()) { return; } AuthorizeDefinition tmp = allDefinition.get(0); List<String> actionDescription = allDefinition.stream() .map(AuthorizeDefinition::getActionDescription) .flatMap(Stream::of) .collect(Collectors.toList()); List<String> actions = allDefinition .stream() .map(AuthorizeDefinition::getActions) .flatMap(Collection::stream) .collect(Collectors.toList()); Set<ActionEntity> actionEntities = new HashSet<>(actions.size()); if (!actions.isEmpty()) { for (int i = 0; i < actions.size(); i++) { String action = actions.get(i); String desc = actionDescription.size() > i ? actionDescription.get(i) : actionDescMapping.getOrDefault(actions.get(i), action); ActionEntity actionEntity = new ActionEntity(); actionEntity.setAction(action); actionEntity.setDescribe(desc); actionEntities.add(actionEntity); } } PermissionEntity entity = entityFactory.newInstance(PermissionEntity.class); if (tmp instanceof AopAuthorizeDefinition) { AopAuthorizeDefinition aopAuthorizeDefinition = ((AopAuthorizeDefinition) tmp); Class type = aopAuthorizeDefinition.getTargetClass(); Class genType = entityFactory.getInstanceType(ClassUtils.getGenericType(type)); List<OptionalField> optionalFields = new ArrayList<>(); entity.setOptionalFields(optionalFields); if (genType != Object.class) { List<Field> fields = new ArrayList<>(); ReflectionUtils.doWithFields(genType, fields::add, field -> (field.getModifiers() & Modifier.STATIC) == 0); for (Field field : fields) { if ("id".equals(field.getName())) { continue; } ApiModelProperty property = field.getAnnotation(ApiModelProperty.class); OptionalField optionalField = new OptionalField(); optionalField.setName(field.getName()); if (null != property) { if (property.hidden()) { continue; } optionalField.setDescribe(property.value()); } optionalFields.add(optionalField); } } } entity.setId(permissionId); entity.setName(tmp.getPermissionDescription().length > 0 ? tmp.getPermissionDescription()[0] : permissionId); entity.setActions(new ArrayList<>(actionEntities)); entity.setType("default"); entity.setStatus(DataStatus.STATUS_ENABLED); waitToSyncPermissions.putIfAbsent(entity.getId(), entity); } Map<String, PermissionEntity> oldCache = permissionService .select() .stream() .collect(Collectors.toMap(PermissionEntity::getId, Function.identity())); waitToSyncPermissions.forEach((permissionId, permission) -> { log.info("try sync permission[{}].{}", permissionId, permission.getActions()); PermissionEntity oldPermission = oldCache.get(permissionId); if (oldPermission == null) { permissionService.insert(permission); } else { Set<ActionEntity> oldAction = new HashSet<>(); if (oldPermission.getActions() != null) { oldAction.addAll(oldPermission.getActions()); } Map<String, ActionEntity> actionCache = oldAction .stream() .collect(Collectors.toMap(ActionEntity::getAction, Function.identity())); boolean permissionChanged = false; for (ActionEntity actionEntity : permission.getActions()) { if (actionCache.get(actionEntity.getAction()) == null) { oldAction.add(actionEntity); permissionChanged = true; } } if (permissionChanged) { oldPermission.setActions(new ArrayList<>(oldAction)); permissionService.updateByPk(oldPermission.getId(), oldPermission); } actionCache.clear(); } }); oldCache.clear(); waitToSyncPermissions.clear(); definitions.clear(); grouping.clear(); }  @Autowired void setPermissionService(PermissionService permissionService); @Autowired void setEntityFactory(EntityFactory entityFactory); @Override void onApplicationEvent(AuthorizeDefinitionInitializedEvent event);  }
@Test public void test() throws NoSuchMethodException { AopMethodAuthorizeDefinitionParser parser = new DefaultAopMethodAuthorizeDefinitionParser(); List<AuthorizeDefinition> definition = Arrays.stream(TestController.class.getMethods()) .map(method -> parser.parse(TestController.class, method)) .filter(Objects::nonNull) .collect(Collectors.toList()); autoSyncPermission.onApplicationEvent(new AuthorizeDefinitionInitializedEvent(definition)); Assert.assertTrue(!newPermissionEntity.isEmpty()); PermissionEntity permissionEntity = newPermissionEntity.get(0); Assert.assertEquals(permissionEntity.getId(), "test"); Assert.assertEquals(permissionEntity.getName(), "测试权限"); Assert.assertTrue(!permissionEntity.getActions().isEmpty()); Assert.assertEquals(permissionEntity.getOptionalFields().size(), 3); }