如何调试(和修复)Symfony2 | 3路由?

时间:2016-01-15 20:04:58

标签: php symfony symfony-routing

我在Symfony 2.8应用程序中app/config/routing.yml定义了此路由:

platform_chat:
    resource: "@PlatformChatBundle/Controller/"
    type:     annotation
    prefix:   /chat

platform_admin:
    resource: "@PlatformAdminBundle/Controller/"
    type:     annotation
    prefix:   /admin

#----> this is part of routing.yml but I forgot to add it
easy_admin_bundle:
    resource: "@PlatformAdminBundle/Controller/AdminController.php"
    type:     annotation
    prefix:   /admin

#FOSUser
fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

您可能已经注意到PlatformAdminBundle是后端,PlatformChatBundle是前端。考虑到这一点,我试着设置并为两者使用一个防火墙,然后在security.interactive_login事件重定向到正确的路径|路径。这就是防火墙的样子:

security:
    ...
    role_hierarchy:
        ROLE_CHATTER:     ROLE_USER
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN
    ...
    firewalls:
        ...
        ignored:
            pattern: ^/(login(_check)?|logout|resetting)$
            security: false
        global:
            pattern: ^/admin/(.*)|^/chat/(.*)
            provider: fos_userbundle
            form_login:
                csrf_provider: security.csrf.token_manager
                login_path: fos_user_security_login
                check_path: fos_user_security_check
                # if true, forward the user to the login form instead of redirecting
                use_forward: true
                # login success redirecting options (read further below)
                always_use_default_target_path: true
                default_target_path: /admin
                target_path_parameter: _target_path
                use_referer: true
                remember_me: true
            logout: ~
            remember_me:
                secret:   '%secret%'
                lifetime: 604800 # 1 week in seconds
                path:     /

    access_control:
        - { path: ^/chat/, role: ROLE_CHATTER }
        - { path: ^/admin/, role: ROLE_ADMIN }

但它无法正常工作,因为当我尝试以任何一个用户身份登录时,我以此错误结束:

  

您必须使用安全防火墙配置中的form_login配置防火墙处理的检查路径。

这让我觉得路由或防火墙没有正确配置。我检查了调试工具栏下的路由,没有匹配,所以它们完成错误。我已经阅读了文档here,但它根本没用,而且我没有得到解决问题的方法。您可以将此帖子作为this one第二部分,但我不想更改旧帖子的主题,也不想更改内容,因为我认为它会有所帮助别人的未来。那么,有什么建议吗?你会调试哪些与路线有关的问题?我的特定问题的任何修复?我真的被困在这里了!

更新

我已根据@xabbuh的建议进行了更改,现在app/config/routing.yml看起来像是:

platform_chat:
    resource: "@PlatformChatBundle/Controller/"
    type:     annotation
    prefix:   /chat
    options:
            expose: true

platform_admin:
    resource: "@PlatformAdminBundle/Controller/"
    type:     annotation
    prefix:   /admin
    options:
        expose: true

#EasyAdminBundle
easy_admin_bundle:
    resource: "@PlatformAdminBundle/Controller/AdminController.php"
    type:     annotation
    prefix:   /admin
    options:
        expose: true

#FOSUser
fos_user:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

#FOSUser Groups
fos_user_group:
    resource: "@FOSUserBundle/Resources/config/routing/group.xml"
    prefix: /group

#FOSJsRouting
fos_js_routing:
    resource: "@FOSJsRoutingBundle/Resources/config/routing/routing.xml"

和``看起来像:

security:
    ...
    firewalls:
        ...
        global:
            pattern: /
            anonymous: true
            provider: fos_userbundle
            form_login:
                csrf_provider: security.csrf.token_manager
                login_path: fos_user_security_login
                check_path: fos_user_security_check
                use_forward: true # if true, forward the user to the login form instead of redirecting
                always_use_default_target_path: true # login success redirecting options (read further below)
                default_target_path: /admin
                target_path_parameter: _target_path
                use_referer: true
                remember_me: true
            logout: ~
            remember_me:
                secret:   '%secret%'
                lifetime: 604800 # 1 week in seconds
                path:     /

    access_control:
        - { path: ^/chat/, role: ROLE_CHATTER }
        - { path: ^/admin/, role: ROLE_ADMIN }

清除缓存后,这是我的尝试和结果:

  • ROL_CHATTER身份登录:我将按照预期http://domain.tld/app_dev.php/chat/获取登录表单并使用有效凭据我收到以下消息:拒绝访问。你是CHATTER 。这是正确的,因为我在security.interactive_login上有一个监听器,这就是我在用户登录时所做的事情。
  • ROL_ADMIN登录:我将按照预期http://domain.tld/app_dev.php/admin/获取登录表单并使用有效凭据我收到以下消息:凭据错误。这是错误的,因为凭据是有效的,至少我应该得到另一条消息(拒绝访问。你是ADMIN ),因为security.interactive_login上的监听器,但正如我所说,这不是正在发生的事情。

与听众相关的信息位于this post。怎么了?

1 个答案:

答案 0 :(得分:3)

您的问题是,用于匹配global防火墙请求的正则表达式为/admin/(.*)|^/chat/(.*),但您的检查路径为/login_check。正如您所看到的那样,您的防火墙不会匹配路径,从而导致您发布的错误消息。

如果我是你,我会简单地将防火墙放在登录相关的东西之前,并将global防火墙的正则表达式更改为/。然后,您只需添加anonymous: true,以便未登录的用户可以访问登录表单。您的访问控制部分仍然会拒绝访问您的受保护区域。