2011-01-24 63 views
7

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

+1

式两份[如何对GET-sslmod-rewritezend框架-MVC-工作在一起(HTTP:/ /stackoverflow.com/questions/380050/how-to-get-sslmod-rewritezend-framework-mvc-working-together) – criticus 2011-03-02 21:41:46

回答

13

最彻底的方法是有对SSL的配置,你可以启用模型/控制器/行动水平SSL支持,像这样的.ini文件:

比方说,你有一个模块/控制器/动作像这样的:
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 
 

您既可以通过配置解析此,或单独,并在引导文件,你可以在这里包括controllerplugin,像我。

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

 

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()); 
 
+3

工作就像一个魅力。非常感谢,非常宝贵的贡献。 – jgnasser 2011-04-26 01:13:17