微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在单个资源的api平台中使用私有和公共路由

如何解决如何在单个资源的api平台中使用私有和公共路由

我会尝试在资源中使用不同的路由并设置多个防火墙:

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.prefixconfig/routes/api_platform.yaml),routePrefixpath

我认为更灵活的方法是在防火墙级别打开所有API资源,并通过security annotations关闭一些资源/操作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。