我正在尝试在ZF2应用程序中使用身份验证和会话。到目前为止,我有以下代码:
在我的Module.php中:
// (...) rest of code
public function getServiceConfig()
{
return array(
'factories' => array(
// (...) Other factories
// Authentication Service
'AuthService' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$dbTableAuthAdapter = new DbTable($dbAdapter,
'sec_user','login','password');
$authService = new AuthenticationService();
$authService->setAdapter($dbTableAuthAdapter);
return $authService;
},
),
);
}
// (...) rest of code
然后在我的控制器登录操作中,我有:
use Zend\Session\Container;
// (...) rest of code
public function loginAction()
{
$this->getAuthService()->getAdapter()
->setIdentity('testlogin')
->setCredential('testpass');
$auth_result = $this->getAuthService()->getAdapter()->authenticate();
if ($auth_result->isValid()) {
$session = new Container(); // Exception line
$session->login = 'testlogin';
// (...) other code
}
}
// (...) rest of code
代码工作正常,成功验证凭据但是当我尝试使用文档中找到的最简单的示例将某些值保存到SESSION时,它失败了。这一行:
$session = new Container();
触发此异常:
Session validation failed
我还能测试什么?任何关于我做错事的想法都将受到赞赏。
贝娄是完整的痕迹:
File:
/var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Session/SessionManager.php:111
Message:
Session validation failed
Stack trace:
#0 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Session/AbstractContainer.php(78): Zend\Session\SessionManager->start()
#1 /var/www/sismedical/Server/module/Security/src/Security/Controller/AuthController.php(100): Zend\Session\AbstractContainer->__construct()
#2 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractActionController.php(83): Security\Controller\AuthController->loginAction()
#3 [internal function]: Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#4 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#5 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#6 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Mvc/Controller/AbstractController.php(117): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#7 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Mvc/DispatchListener.php(114): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#8 [internal function]: Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#9 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(468): call_user_func(Array, Object(Zend\Mvc\MvcEvent))
#10 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/EventManager/EventManager.php(207): Zend\EventManager\EventManager->triggerListeners('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
#11 /var/www/sismedical/Server/vendor/zendframework/zendframework/library/Zend/Mvc/Application.php(309): Zend\EventManager\EventManager->trigger('dispatch', Object(Zend\Mvc\MvcEvent), Object(Closure))
答案 0 :(得分:4)
附加事件侦听器以侦听由所有类$sharedEvents->attach('*', '*', .. )
触发的所有事件将导致此错误消息。
你必须非常小心你将监听器附加到什么位置,因为有些监听器期望一个特定的响应(在这种情况下是指示会话是否有效的布尔值)。不是为处理事件而设计的侦听器(例如日志侦听器)通常不会返回响应,在这种情况下,该响应被视为验证失败。