API 平台 GraphQL 安全性

API Platform GraphQL security

所以我在我的 Symfony 4 项目中使用 API 平台,并且我读到它支持 graphQl,所以我在 security.yml 中设置了一个访问控制以允许用户访问graphQl API:

- { path: ^/api/graphql, roles: IS_AUTHENTICATED_ANONYMOUSLY }

并且在每个实体中我都有对 itemOperations 和 collectinOperations 的访问控制。示例:

 * @ApiResource(
 *     itemOperations={
 *         "get"={
 *             "access_control"="is_granted('ROLE_ADMIN')"
 *         }
 *     },
 *     collectionOperations={
 *         "get"={
 *             "access_control"="is_granted('ROLE_ADMIN')"
 *         }
 *     }
 * )

但问题是,任何用户都可以通过 graphQL 访问这个实体,因为 graphQL 忽略了这些操作的访问控制。 有没有办法强制 graphQL 遵循这些规则?

itemOperationscollectionOperations 与 GraphQL 无关,仅与 REST 相关。

您需要在 GraphQL 操作中添加访问控制,或者,如果您想针对 REST GraphQL(虽然没有区分操作),您可以将其添加为 attribute.

此外,access_control 已弃用。您应该改用 security

您还可以阅读此文档:https://api-platform.com/docs/core/graphql/#security