因此,我在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遵循这些规则?
答案 0 :(得分:1)
itemOperations
和collectionOperations
与GraphQL不相关,而仅与REST相关。
您需要在GraphQL操作中添加访问控制,或者,如果要定位REST 和 GraphQL(尽管没有区别操作),则可以将其添加为attribute
。
此外,access_control
也已弃用。您应该改用security
。
您还可以阅读以下文档:https://api-platform.com/docs/core/graphql/#security