You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

解决Strapi自定义插件中GraphQL的Forbidden access错误

解决Strapi自定义插件集合类型权限配置缺失及GraphQL访问被拒问题

1. 手动注册插件权限项

Strapi不会自动识别自定义插件的内容类型权限,需要在插件中手动注册权限动作:

在插件的server/boot目录下新建01-permissions.js文件,添加以下代码:

module.exports = async ({ strapi }) => {
  // 注册targetcontent的CRUD权限动作
  const targetContentPermissions = [
    {
      action: 'plugin::targetplugin.targetcontent.read',
      section: 'content-type',
      displayName: '读取targetcontent',
      type: 'collection',
      pluginName: 'targetplugin',
      entity: 'targetcontent',
    },
    {
      action: 'plugin::targetplugin.targetcontent.create',
      section: 'content-type',
      displayName: '创建targetcontent',
      type: 'collection',
      pluginName: 'targetplugin',
      entity: 'targetcontent',
    },
    {
      action: 'plugin::targetplugin.targetcontent.update',
      section: 'content-type',
      displayName: '更新targetcontent',
      type: 'collection',
      pluginName: 'targetplugin',
      entity: 'targetcontent',
    },
    {
      action: 'plugin::targetplugin.targetcontent.delete',
      section: 'content-type',
      displayName: '删除targetcontent',
      type: 'collection',
      pluginName: 'targetplugin',
      entity: 'targetcontent',
    },
  ];

  // 批量注册权限
  await strapi.admin.services.permission.actionProvider.registerMany(targetContentPermissions);
};

然后在插件根目录的strapi-server.js中导出boot配置:

module.exports = {
  boot: require('./server/boot'),
  // 保留其他原有配置(如controllers、services、routes等)
};

2. 重启Strapi并配置角色权限

执行以下命令重启服务,确保新的权限配置生效:

npm run build && npm run develop

重启后进入「设置->用户与权限->角色」,就能找到targetplugin分组下的targetcontent权限项,给需要访问的角色(比如Public、Authenticated)勾选对应的权限(如读取权限),保存配置。

3. 验证GraphQL查询

确认你的GraphQL查询格式正确,示例查询如下:

query {
  targetcontents {
    data {
      id
      attributes {
        contentname
        contentdescription
      }
    }
  }
}

执行查询后,应该能正常返回数据库中的数据,不再出现Forbidden access错误。

内容的提问来源于stack exchange,提问作者guerrillacodester

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 18:52:47