php – 如何在Zend MVC中实现SSL
发布时间:2020-12-13 18:18:13 所属栏目:PHP教程 来源:网络整理
导读:我之前使用特定的安全文件夹(例如服务器上的https文件夹vs http文件夹)实现了安全页面.我已经开始使用Zend Framework,并希望部分应用程序(例如登录)使用https.我在google上搜索过,甚至在这里找不到任何解释如何处理这个问题的内容.我可以为特定控制器/操作设
我之前使用特定的安全文件夹(例如服务器上的https文件夹vs http文件夹)实现了安全页面.我已经开始使用Zend Framework,并希望部分应用程序(例如登录)使用https.我在google上搜索过,甚至在这里找不到任何解释如何处理这个问题的内容.我可以为特定控制器/操作设置https吗?谢谢.
最干净的方法是为SSL配置创建一个.ini文件,您可以在其中为模型/控制器/操作级别启用SSL支持,如下所示:
假设您有一个模块/控制器/动作,如下所示: ## 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 你可以通过配置或单独解析它,在你的Bootstrap文件中你可以包含一个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() ); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |