猿问

如何使用 Symfony 5 通过端口限制对某些路线的访问?

我们的 Web 服务器正在侦听 80 和 8080 端口,我希望特定路由只能通过端口 8080 使用,但拒绝尝试使用端口 80 访问该路由的所有用户。


我的routes.yaml


testing-logging:

  path: /testing/logging

  controller: Test\Infrastructure\API\HTTP\Technical\LoggingController::handle

  methods: [GET]


healthcheck:

  path: /healthcheck

  controller: Test\API\HTTP\Technical\HealthcheckController::handle

  methods: [GET]

当然,还有更多的路线,但它们就像这些路线一样。


它是一个微服务,因此没有任何用户。


我想通过自定义端口限制对某些路由的访问。当然,其他路由必须像以前一样使用标准端口。


我试图使用安全性:


security:

    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers

    providers:

        in_memory: { memory: null }

    firewalls:

        dev:

            security: true

            anonymous: ~

            methods: [POST]

        main:

            anonymous: lazy

    access_control:

        - { path: ^/healthcheck, roles: IS_AUTHENTICATED_ANONYMOUSLY, port: 8080 }


慕桂英3389331
浏览 67回答 1
1回答

30秒到达战场

只需为端口 80 添加另一个规则。access_controlaccess_control:         - { path: ^/healthcheck, roles: IS_AUTHENTICATED_ANONYMOUSLY, port: 8080 }         - { path: ^/healthcheck, roles: ROLE_ADMIN, port: 80 }由于您没有任何身份验证机制,因此没有用户将拥有 。因此,任何尝试在端口 80 上访问的用户都将被拒绝其访问。ROLE_ADMIN^/healthcheck
随时随地看视频慕课网APP
我要回答