我正在尝试在我的第一个测试应用程序中设置用户和安全管理,但我对什么做了什么有点迷茫。

到目前为止我的设置: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 防火墙的模式( ^/.* )

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

    最佳实践

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

    最佳答案

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

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

    imports:
        - { resource: security.yml }
    

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

    这是默认配置,在官方GitHub存储库中查看这些文件:
  • app/config/config.yml (无 security: 部分)
  • app/config/security.yml (包含 security: 部分)
  • 关于security - config.yml 和 security.yml 中的安全设置如何关联?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25217901/

    10-13 08:52