api-platform.com - 如何在单一资源的 api-platform 中使用私有和公共路由
问题描述
我会尝试在资源中使用不同的路由并设置多个防火墙:
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
api_login:
pattern: ^/api/public/authentication
anonymous: true
provider: app_user_provider
stateless: true
json_login:
check_path: /api/public/authentication_token
username_path: email
password_path: password
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
# guard:
# authenticators:
# - lexik_jwt_authentication.jwt_token_authenticator
api_private:
pattern: ^/api/private
stateless: true
provider: app_user_provider
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
access_control:
- { path: ^/docs, roles: IS_AUTHENTICATED_ANONYMOUSLY } # Allows accessing the Swagger UI
- { path: ^/api/public/, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/private/, roles: IS_AUTHENTICATED_FULLY }
并希望在我的用户资源中使用不同的 route_prefixes。就我而言,我不想让 collectionOperation.get 与公共防火墙相匹配。尝试为此资源全局设置/private前缀并希望在 collectionOperation.get 中覆盖:
/**
* @ApiResource(
* routePrefix="/private",
* collectionOperations={
* "get"={
* "path"="/api/public/users",
* "security"="is_granted('IS_AUTHENTICATED_ANONYMOUSLY')"
* },
* }
* )
* @ORM\Entity(repositoryClass=UserRepository::class)
*/
但是当我使用path时,前缀不会被覆盖:
/api/private/api/public/users
我应该不使用“route_prefix”并为每个操作定义一个自定义路径吗?
解决方案
完整的资源路径是串联api_platform.prefix
( config/routes/api_platform.yaml
)、routePrefix和path
.
我认为更灵活的是在防火墙级别打开所有 API 资源并通过安全注释关闭一些资源/操作。
推荐阅读
- datetime - 在 Flutter 中为 DateTime 转换似乎无法正常工作
- pyqt - 从 QtDesigner 设计的 UI 中隐藏底部边框
- sql - Hive - 如何从列表类型的表中读取列
- python - python打印来自两个不同字典的值
- python - 获取 PySpark 列中列表列表中第一个元素的最大值
- wpf - WPF:从后面的代码更改属性时,未应用内置数据错误模板(红色边框)
- python - python 3通过套接字发送文件
- python - Noob here - 当我在屏幕上移动鼠标时,我的精灵更新的 pygame 动画
- python-3.x - 从多个列表中识别相似的数字
- python-3.x - 如何使用其键从 Google 数据库中删除 NDB 对象,甚至不编译