我正在尝试在我的第一个测试应用程序中设置用户和安全管理,但我已经有点迷失了,不知道什么是做什么的。
到目前为止我的设置:Symfony 2.5、SonataUserBundle(以及 FOSUserBundle)
在我的 app/config/config.yml
中,我有以下设置,我认为这些设置与管理站点安全性相关(大部分取自各个 bundle 的设置说明)我包括):
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_manager
和fos_user.user_provider.username
) - FOS\UserBundle\Model\UserInterface 的编码器
主
防火墙的模式(^/
与.*
)
这些确实定义相同吗?可以安全地假设在所有这些情况下,只有 security.yml
中定义的设置适用吗?
最佳实践
通常应如何在 security.yml
和 config.yml
(以及其他潜在位置)之间划分与安全相关的定义?
最佳答案
正如 Cerad 在评论中提到的,两个文件中都有相同的 security:
部分。
查看app/config/config.yml
文件的开头:
imports:
- { resource: security.yml }
这意味着当 Symfony2 解析 config.yml
文件时,security.yml
文件将被导入。因此,您可以仅保留 app/config/security.yml
文件中的 security:
部分来定义安全配置。
这是默认配置,请在官方 GitHub 存储库中查看这些文件:
-
app/config/config.yml
(没有安全:
部分) -
app/config/security.yml
(包含security:
部分)
关于security - config.yml 和 security.yml 中的安全设置如何关联?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25217901/