【问题标题】:How do security settings in config.yml and security.yml relate?config.yml 和 security.yml 中的安全设置如何关联?
【发布时间】:2014-08-09 11:08:07
【问题描述】:

我正在尝试在我的第一个测试应用程序中设置用户和安全管理,但我有点不知所措。

到目前为止我的设置:Symfony 2.5、SonataUserBundle(以及它的 FOSUserBundle)

在我的app/config/config.yml 中,我有以下设置,这些设置与管理站点安全性相关(大部分来自我所包含的各种捆绑包的设置说明):

imports:
    - { resource: security.yml }

[...]

fos_user:
    firewall_name:  main

[...]

security:
    # FOSUserBundle config
    # cf. https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/doc/index.md#step-4-configure-your-applications-securityyml
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512
    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN
    providers:
        fos_userbundle:
            id: fos_user.user_provider.username
    firewalls:
        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
            logout:       true
            anonymous:    true
    # end of FOSUserBundle config

    access_control:
        # URL of FOSUserBundle which need to be available to anonymous users
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Admin login page needs to be access without credential
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Secured part of the site
        # This config requires being logged for the whole site and having the admin role for the admin part.
        # Change these rules to adapt them to your needs
        - { path: ^/admin/, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
        - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

我的app/config/security.yml 如下所示:

security:

    # added with Sonata User Bundle
    encoders:
        FOS\UserBundle\Model\UserInterface: sha512
    # end

    providers:
        in_memory:
            memory: ~
        # added with Sonata User Bundle
        fos_userbundle:
            id: fos_user.user_manager
        # end

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        # added with Sonata User Bundle
        # -> custom firewall for the admin area of the URL
        admin:
            pattern:            /admin(.*)
            context:            user
            form_login:
                provider:       fos_userbundle
                login_path:     /admin/login
                use_forward:    false
                check_path:     /admin/login_check
                failure_path:   null
            logout:
                path:           /admin/logout
            anonymous:          true

        # -> end custom configuration

        # default login area for standard users

        # This firewall is used to handle the public login area
        # This part is handled by the FOS User Bundle
        main:
            pattern:             /(.*)
            context:             user
            form_login:
                provider:       fos_userbundle
                login_path:     /login
                use_forward:    false
                check_path:     /login_check
                failure_path:   null
            logout:             true
            anonymous:          true
        # end

        default:
            anonymous: ~

    # Sonata
    acl:
        connection: default

    role_hierarchy:
        ROLE_ADMIN:       [ROLE_USER, ROLE_SONATA_ADMIN]
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
        SONATA:
            - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT  # if you are using acl then this line must be commented

这是我的问题:

配置的优先级

根据我迄今为止对 Symfony 的“模式”的理解,security.yml 中的任何内容都会首先加载,因此将优先于我的config.yml 中相同参数的任何新定义。 正确吗?

重复定义

在我看来,以下定义了两次,一次在security.yml,一次在config.yml

  • FOSUserBundle 的提供者(不同的值,fos_user.user_managerfos_user.user_provider.username
  • FOS\UserBundle\Model\UserInterface 的编码器
  • main 防火墙的模式 (^/vs. .*)

这些确实定义相同吗?是否可以假设在所有这些情况下,只有security.yml 中定义的那些设置适用?

最佳做法

一般应如何在security.ymlconfig.yml(以及其他潜在位置)之间划分与安全相关的定义?

【问题讨论】:

  • 您不应该在两个不同的文件中有相同的部分(即安全性:)。从 config.yml 中删除 security: 部分。我怀疑您在配置 fos_user 部分时可能已将其放在那里,并且可能误解了说明。
  • 好的。我相信在 Symfony 文档的某些部分中看到了一些东西,暗示在这两个地方都有配置是可以的。但是这部分的通知似乎证实了你的建议:symfony.com/doc/current/book/…
  • 所以我猜这个页面会误导用户:symfony.com/doc/2.8/testing/http_authentication.html 它表明我们可以覆盖 config_test.yml 中的安全性:(

标签: security symfony


【解决方案1】:

正如 Cerad 在评论中提到的,您在两个文件中都有相同的部分 security:

查看app/config/config.yml文件的开头:

imports:
    - { resource: security.yml }

这意味着security.yml文件将在Symfony2解析config.yml文件时被导入。因此,您可以只保留app/config/security.yml 文件中的security: 部分以定义安全配置。

这是默认配置,请在官方 GitHub 存储库中查看这些文件:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-16
    • 2016-10-30
    • 1970-01-01
    • 2017-06-23
    • 2011-11-28
    • 2016-10-23
    相关资源
    最近更新 更多