问题描述
我会尝试在资源中使用不同的路由并设置多个防火墙:
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资源,并通过security annotations关闭一些资源/操作。