如何在symfony中按环境保护应用程序? [英] how to secure an application by environment in symfony?
问题描述
我做了
apps / frontend / config / security.yml
dev:
default:
is_secure: false
prod:
default:
is_secure: true
但它不起作用,我错过了什么吗?
but it is not working, am i missing something ?
推荐答案
正如史蒂夫所说,不能在每个环境中配置is_secure。
As steve says, is_secure can't be configured on a per environment basis.
我的猜测是您试图用密码保护您的整个开发环境?我建议您使用.htaccess / .htpasswd保护或类似方法以这种方式保护网站。
My guess is that you are trying to password protect your entire dev environment? I'd suggest that you use .htaccess/.htpasswd protection or equivalent to protect a site in this way.
如果您不能或出于任何原因想要这样做在symfony中,您可以通过创建自定义sfSecurityConfigHandler.class.php
If you can't or for whatever reason want to do it in symfony, you could make symfony accept configuration in this way by creating a custom sfSecurityConfigHandler.class.php
Config处理程序中的一种名为getConfiguration的方法,使symfony接受配置。
Config handlers have a method in them called getConfiguration - this is in charge of getting the values set in the various yml files and creating an array of the final values after all over-rides etc have been applied.
sfSecurityConfigHander.class.php具有负责获取各种yml文件中设置的值并在应用所有替代等之后创建最终值的数组的功能。这样的getConfiguration:
sfSecurityConfigHander.class.php has a getConfiguration like this:
static public function getConfiguration(array $configFiles)
{
$config = self::flattenConfiguration(self::parseYamls($configFiles));
// change all of the keys to lowercase
$config = array_change_key_case($config);
return $config;
}
其中有一个取决于环境的配置,例如sfDatabaseConfigHandler.class.php具有像这样的人:
whilst a configuration that depends on environment, such as sfDatabaseConfigHandler.class.php has one like this:
static public function getConfiguration(array $configFiles)
{
$config = self::replaceConstants(self::flattenConfigurationWithEnvironment(self::parseYamls($configFiles)));
foreach ($config as $name => $dbConfig)
{
if (isset($dbConfig['file']))
{
$config[$name]['file'] = self::replacePath($dbConfig['file']);
}
}
return $config;
}
此处的主要区别是对self :: flattenConfigurationWithEnvironment的使用: flattenConfiguration。我认为,如果使用以下方法扩展sfSecurityConfigHandler:
The key difference here is the use of self::flattenConfigurationWithEnvironment over self::flattenConfiguration. I think if you extend sfSecurityConfigHandler with:
class mySecurityConfigHandler extends sfSecurityConfigHandler {
static public function getConfiguration(array $configFiles)
{
$config = self::flattenConfigurationWithEnvironment(self::parseYamls($configFiles));
// change all of the keys to lowercase
$config = array_change_key_case($config);
return $config;
}
}
,然后在配置中创建config_handlers.yml文件告诉symfony使用此类:
and then create a config_handlers.yml file in your config telling symfony to use this class:
modules/*/config/security.yml:
class: sfSecurityConfigHandler
file: %sf_lib_dir%/path/to/mySecurityConfigHandler
您应该能够使用根据问题配置yml。
You should then be able to use the yml as per the question to configure security per environment.
这篇关于如何在symfony中按环境保护应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!