如何在 Zend MVC 中实现 SSL

发布于 2024-10-14 08:25:11 字数 175 浏览 1 评论 0原文

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

I have implemented secure pages before by using a specific secure folder (eg https folder vs http folder on the server). I have started using Zend Framework and would like parts of the application (eg login) to use https. I have searched on google and even here but could not find anything that explains how to handle this. Can I have https for specific controllers/actions? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬心 2024-10-21 08:25:11

最简洁的方法是为 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

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

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


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

The cleanest way is to have an .ini file for the SSL config where you can enable SSL support for model/controller/action levels, like so:

Let's say you have a module/controller/action like this:
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

You parse this either through config, or separately, and in your Bootstrap file you can include a controllerplugin, like mine here.

There are many other ways to do this, but I think you get the idea!


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();
        }
    }
}

I forgot to mention: to add it in your bootstrap:


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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文