【问题标题】:How to implement SSL in Zend MVC如何在 Zend MVC 中实现 SSL
【发布时间】:2011-01-24 01:57:49
【问题描述】:

我之前使用特定的安全文件夹(例如服务器上的 https 文件夹与 http 文件夹)实现了安全页面。我已经开始使用 Zend Framework 并且希望应用程序的某些部分(例如登录)使用 https。我已经在谷歌甚至这里搜索过,但找不到任何解释如何处理这个问题的东西。我可以为特定的控制器/操作使用 https 吗?谢谢。

【问题讨论】:

标签: php model-view-controller zend-framework ssl


【解决方案1】:

最简洁的方法是为 SSL 配置创建一个 .ini 文件,您可以在其中启用对模型/控制器/操作级别的 SSL 支持,如下所示:

假设您有一个这样的模块/控制器/动作:
SSLModule->IndexController->testAction

## ini file (can be config.ini also) ssl.modules.SSLModule.require_ssl = true //-> entire module requires SSL ssl.modules.SSLModule.Index.require_ssl = true //-> entire controller requires SSL ssl.modules.SSLModule.Index.test.require_ssl = true //-> single action requires SSL

您可以通过配置或单独解析它,并且在您的引导文件中您可以包含一个控制器插件,就像我在这里一样。

还有很多其他方法可以做到这一点,但我想你明白了!

class Application_Controllerplugins_Ssl extends Zend_Controller_Plugin_Abstract { public function preDispatch ( Zend_Controller_Request_Abstract $request ) { $shouldSecureUrl = false; //get the config settings for SSL $options = Application_ServiceManager::getConfig()->ssl; //if config is empty, exit if (!is_object($options)) return; //simpler to use $options = $options->toArray(); //only use it production environment if ( APPLICATION_ENV == 'production' ) { if ( ( isset($options['modules'][$request->module]['require_ssl']) && $options['modules'][$request->module]['require_ssl'] ) || ( isset($options['modules'][$request->module][$request->controller]['require_ssl']) && $options['modules'][$request->module][$request->controller]['require_ssl'] ) || ( isset($options['modules'][$request->module][$request->controller][$request->action]['require_ssl']) && $options['modules'][$request->module][$request->controller][$request->action]['require_ssl'] ) ) { $shouldSecureUrl = true; } if ( $shouldSecureUrl ) { $this->_secureUrl($request); } } } protected function _secureUrl ( Zend_Controller_Request_Abstract $request ) { $server = $request->getServer(); $hostname = $server['HTTP_HOST']; if ( ! $request->isSecure() ) { $url = Zend_Controller_Request_Http::SCHEME_HTTPS . "://" . $hostname . $request->getPathInfo(); $redirector = Zend_Controller_Action_HelperBroker::getStaticHelper('redirector'); $redirector->setGoToUrl($url); $redirector->redirectAndExit(); } } }

我忘了提及:将其添加到您的引导程序中:

$Zend_Controller_Front->registerPlugin( new Application_Controllerplugins_Ssl() );

【讨论】:

  • 工作就像一个魅力。非常感谢,非常宝贵的贡献。
猜你喜欢
  • 2012-03-29
  • 1970-01-01
  • 2014-06-11
  • 2012-10-21
  • 2010-09-27
  • 1970-01-01
  • 2014-05-25
  • 2010-11-18
  • 1970-01-01
相关资源
最近更新 更多