w0rma / sf-ssl-requirement-extra-plugin
symfony 1.x 的 SSL 插件。
Requires
- composer/installers: *
- lexpress/symfony1: 1.5.*
This package is not auto-updated.
Last update: 2024-09-29 02:34:10 UTC
README
概览
sfSslRequirementExtra
是一个 symfony 插件,为您的应用程序提供 SSL 集成。该插件需要 https://github.com/LExpress/symfony1,这是 symfony1 的一个分支。请注意,您应该已经配置了一个带有 SSL 的 web 服务器。
它提供了 3 个新的安全设置(在模块的 security.yml
中指定):require_ssl
、allow_ssl
和 generate_ssl
,允许您在 URL 匹配期间以及 URL 生成期间完全配置 SSL 要求。更多解释请参阅。
该插件还通过混合设计模式添加了 3 个新的 sfAction
方法:->sslRequired()
、->sslAllowed()
和 ->sslGenerate()
。
安装
-
使用 composer 安装插件
$ composer require w0rma/sf-ssl-requirement-extra-plugin
-
在
config/ProjectConfiguration.class.php
中启用 sfSslRequirementExtraPlugin(如果未自动启用)[php] public function setup() { $this->enablePlugins( array('sfSslRequirementExtraPlugin')); }
-
在您的
filters.yml
中激活过滤器(用于 URL 匹配期间的 SSL 要求)[yml] [...] sfSslRequirementExtra: class: sfSslRequirementFilter cache: ~ execution: ~
-
在您的
factories.yml
中设置路由工厂(用于 URL 生成期间的 SSL 要求,即当设置generate_ssl
安全选项时)[yml] routing: class: sfSslPatternRouting
-
清除您的缓存
$ symfony cc
快速配置
要强制模块/操作(使用 GET HTTP 方法)使用 SSL
-
将以下片段添加到模块的
security.yml
中(在 appdir/moduleName/config 下)[yml] actionName: require_ssl: true
-
完成。现在,如果您尝试通过 HTTP 访问
actionName
,您将被自动重定向到 HTTPS。
注意:此处列出的 actionName
是一个示例。请用您的实际操作名称替换。
SSL 要求配置
到目前为止,您可以强制 GET 操作使用 HTTPS。那么对于强制 HTTP、允许 HTTP 和 HTTPS 或者控制 POST 操作的 SSL 要求怎么办?您可以更精确地配置 sfSslRequirementExtra 插件以满足这些要求。
SSL 要求可以按模块或模块/操作进行配置。要配置整个模块,只需在模块的 security.yml
中的 all: 键下添加配置条目。要配置模块的操作,请在模块的 security.yml
中的 actionName: 键下添加配置条目。
security.yml
支持的配置指令及其默认值如下
[yml]
all:
require_ssl: false
allow_ssl: false
generate_ssl: false
解释
- allow_ssl: true|false - 当为 true 时,允许 HTTP 和 HTTPS。当为 false 时,请求被强制为 HTTP。
- require_ssl: true|false - 当为 true 时,请求被强制为 HTTPS。这比 allow_ssl 优先级高。
- generate_ssl: true|false - 当为 true 时,路由生成将考虑
allow_ssl
和require_ssl
配置指令的要求。请注意,您应该已经设置了正确的路由工厂,如安装步骤中所述,才能使其正常工作。
逻辑
-
在 URL 匹配期间
if (not POSTing) if (not secured [HTTP]) if (require_ssl) redirect to HTTPS elseif (secured [HTTPS]) if (not allow_ssl) redirect to HTTP elseif (POSTing and strict_post = true) if (not secured [HTTP]) if (require_ssl) throw logical exception (misconfiguration)
-
在 URL 生成期间
if (generate_ssl) if (require_ssl) if (not request has ssl) if (route absolute) write secure url (HTTPS) else throw logical exception (route should be generated absolutely) elseif (not allow_ssl) if (request has ssl) if (route absolute) write non-secure url (HTTP) else throw logical exception (route should be generated absolutely)
插件配置
可以为整个插件配置一些选项。这些在 app.yml
(应用程序或全局)中指定,如下所示(显示默认值)
[yml]
all:
sfSslRequirementExtraPlugin:
# completely disable ssf requirement plugin
disable: false
# if set, an exception is thrown when insecure post data have been POSTed
strict_post: true
注意:
-
严格的发布(默认为true)会在数据应该通过安全方式传输却已经通过普通HTTP发送时抛出运行时异常。这不能阻止数据在第一次暴露,但会将错误配置指向开发者。解决方法是使用带有require_ssl=true的安全POST操作,并以绝对方式生成URL。在模板中可以这样写:
[php] url_for('route_name', array(route_params), true) link_to('Text', 'route_name', array('absolute'=>true))
您可以通过在
app.yml
中指定strict_post=false来跳过此行为。
动态配置
到目前为止,您已经以static
方式配置了在URL匹配或生成过程中所有SSL要求方面的各个方面。但是,如果您想在用户认证时仅对操作要求SSL怎么办?(这在使用内容为认证用户提供安全服务,同时以非安全方式为访客提供相同内容时是有意义的)。或者如果您想根据请求参数控制SSL要求。(当您使用同一操作为不同目的提供内容时,例如atom源,这很有意义。在这种情况下,sf_format参数区分两种情况)
为了完成这些以及其他需要动态配置SSL要求的场合,您应遵循以下步骤:
-
在
moduleName/config
目录下创建一个名为sslDynConfig.class.php
的文件(与security.yml
相同的目录)。 -
在此文件中,声明一个名为
<moduleName>ActionsSslDynConfig
的类,该类实现了actionsSslDynConfigIface
接口。[php] /** * This interface must be implemented from <moduleName>ActionsSslDynConfig classes for each * module that needs to provided dynamic ssl requirement configuration */ interface actionsSslDynConfigIface { /** * Called from sf_ssl_requirement plugin, to dynamically configure it during URL MATCHING * * @param string $actionName * @param sfAction $actionInstance * @return array */ public function getSslRequirementMatchDynamicConfig( $actionName, $actionInstance ); /** * Called from sf_ssl_requirement plugin, to dynamically configure it during URL GENERATION * * @param string $actionName * @param array $routeParams * @return array */ public function getSslRequirementGenerateDynamicConfig( $actionName, $routeParams ); }
动态配置分别通过->getSslRequirementMatchDynamicConfig()
和->getSslRequirementGenerateDynamicConfig()
方法进行,分别对应URL匹配和URL生成。注意,对于特定的操作,不一定(且可能)需要为URL匹配和生成定义相同的动态配置。
动态配置的优先级高于静态配置(在security.yml
中定义)。
警告:动态配置可能会损害您的头脑,请谨慎使用。
-
在这些方法上实现逻辑,并返回一个SSL要求配置参数的数组(如上所述的“SSL要求配置”部分)。
-
对于URL匹配,您有$actionsName和$actionInstance参数。请注意,
getSslRequirementMatchDynamicConfig()
应始终返回一个数组,不包括generate_ssl
参数(因为在URL匹配期间这个参数没有意义)。示例:您想在sf_format不是atom(即对于正常HTML内容)并且用户认证时要求ssl_index动作。此外,您想在用户认证时要求ssl_show动作。这是如何做到的。
[php] public function getSslRequirementMatchDynamicConfig( $actionName, $actionInstance ) { $request = $actionInstance->getRequest(); $user = $actionInstance->getUser(); switch ($actionName) { case 'index': if ($request->getParameter('sf_format') !== 'atom' && $user->isAuthenticated() ) return array('require_ssl'=>true); break; case 'show' : if ($user->isAuthenticated()) return array('require_ssl'=>true); break; } // no dynamic config return array(); }
-
对于URL生成,您有$actionName和$routeParams数组参数。请注意,
getSslRequirementGenerateDynamicConfig()
应始终返回一个数组。示例:您想在sf_format是atom时,为index动作生成非安全协议的路由。也就是说,您想使atom源链接始终位于http上,无论当前请求协议如何。这是如何做到的。
[php] public function getSslRequirementGenerateDynamicConfig($actionName, $routeParams) { switch ($actionName) { case 'index': // on index, format atom, generate ssl (dissallow ssl) if (isset($routeParams['sf_format']) && $routeParams['sf_format'] === 'atom') { return array('generate_ssl'=>true); } break; } // no dynamic config return array(); }
这些都是一些经典示例,其中动态配置SSL要求非常有用。请随意使用它满足您可能需要的任何其他场景。