security.yml有access_control条目,每个条目都是以下的组合:
我的理解是,Symfony只匹配每个请求一个,但这也是执行HTTPS的最高级别的地方。
那么,对于每个惟一的URL模式/角色需求,这是否意味着一个重复的规则?
发布于 2016-09-22 18:37:36
您是对的,对于每个传入请求,Symfony将根据URI、客户端的IP地址、传入的“主机名”!和请求方法来决定使用哪个。
但是为了你的幸福,有一种方法可以避免重复规则,实现同样的目标。
“特殊parameters.yml文件”
它定义了每个服务器上通常更改的值。
首先,在本地开发机器中,在app/config/parameters.yml文件中定义一个新参数,让我们命名为app/config/parameters.yml
# in your local development machine (development)
parameters:
database_driver: pdo_mysql
# ...
requires_channel: http # <- Wow! I can use this var anywhere into my configuration :)在生产服务器中做相同的设置,但是设置https
# in your deployment server (production)
# app/config/parameters.yml
parameters:
database_driver: pdo_mysql
# ...
requires_channel: https # <- I need enforce https redirection now in production server现在,您可以在每个条目中只定义一个规则,并使用新的%requires_channel%参数:
# app/config/security.yml
security:
# ...
access_control:
- { path: ^/admin, role: ROLE_ADMIN, requires_channel: '%requires_channel%' }
# ...%requires_channel%:的值取决于运行应用程序的主机。
请注意,host属性不再重要,因为它以前需要区分这两种规则。
这个解决方案可以通过使用环境配置文件(config_dev.yml和config_prod.yml)来完成,但是您可能也需要在localhost (http)中测试prod环境的应用程序。因此,上述解决方案对您来说应该足够了;)
希望我能帮上忙!
https://stackoverflow.com/questions/39644788
复制相似问题