Mam zaimplementowane strony zabezpieczone przed użyciem konkretnego bezpiecznego folderu (np. Folder HTTPS vs http na serwerze). Zacząłem używać Zend Framework i chciałbym, aby część aplikacji (np. Login) używała https. Szukałem w Google, a nawet tutaj, ale nie mogłem znaleźć niczego, co wyjaśniłoby, jak sobie z tym poradzić. Czy mogę mieć https dla określonych kontrolerów/działań? Dzięki.Jak zaimplementować protokół SSL w Zend MVC
7
A
Odpowiedz
13
Najczystsze sposobem jest posiadanie pliku ini dla konfiguracji SSL gdzie można włączyć obsługę SSL na poziomie modelu/kontroler/działania, jak w przykładzie:
Powiedzmy masz moduł/kontroler/działania, jak to:
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
można analizować to albo poprzez config lub oddzielnie, aw pliku Bootstrap można dołączyć controllerplugin, jak ja tutaj.
Istnieje wiele innych sposobów, aby to zrobić, ale myślę, że masz pomysł!
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();
}
}
}
zapomniałem wspomnieć: aby dodać go w bootstrap:
$Zend_Controller_Front->registerPlugin(new Application_Controllerplugins_Ssl());
+3
Pracował jak urok. Wielkie dzięki, bardzo cenny wkład. – jgnasser
duplikat dla [How-to-get-sslmod-rewritezend-ram-mvc-working together] (http://stackoverflow.com/questions/380050/how-to-get-sslmod-rewritezend-framework-mvc-working-together) – criticus