首页 > 解决方案 > 如何在单一资源的 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.com

解决方案


完整的资源路径是串联api_platform.prefix( config/routes/api_platform.yaml)、routePrefixpath.

我认为更灵活的是在防火墙级别打开所有 API 资源并通过安全注释关闭一些资源/操作。


推荐阅读